Be functional!

Talking to an adult — FP is Fun and Practical 2.

Gábor Manner
Emarsys Craftlab

--

For part 1 of the series, click here.

Imperative vs. declarative

There’s a saying Venkat Subramaniam uses a lot for comparing imperative vs. declarative programming style and I like it very much as it is so expressive:

Imperative style is where you tell WHAT to do AND HOW TO actually do that.
While declarative style is where you tell WHAT to do AND NOT HOW TO do that.

Sometimes he goes further and says: “Imperative style is like talking to children. You have to say everything — and repeat it on the next day”

So let’s see them by some real life examples!

How to ask for some milk?

Imperative

You specify every single step:

1. Open the fridge
2. Take the box out of the fridge
3. If it is not milk (can be orange juice as well) put it back and take another one.
4. Repeat it, until you get the milk or there are no more boxes
5. Pour it in a glass
...
n. Give it to me

Declarative

You declare pieces of information/functions and pronounce your goal. All you have to do is to combine (compose) them:

- The milk is in the fridge.
- Milk is a thing which is in a box with the text "Milk" on it. Hopefully :)
- I want (a Glass of*) milk
Here you are.

* if we are in a type-safe environment we can actually say we want a glass of milk, since it is also known that Glass is a format (*cough… type) which makes milk transferable and containable for our hands and easily consumable for us.

Okay, but I’m not a baby, I’m not here for milk! Got code?

Okay, let me show you the above situation by p̶s̶e̶u̶d̶o̶-̶c̶o̶d̶e… j̶a̶v̶a̶s̶c̶r̶i̶p̶t… some pseudo-code that resembles Javascript.
I like the KISS principle, so I made it really simple and really-really stupid, just to focus on the way how you implement these different approaches. I lowered my desires in quality and dropped the fridge. Now there are just some boxes of drinks, and I don’t need glasses, but I want all that (likely) contains milk!

const boxes= [
{text: "orange", content: "orange"},
{text: "tomato", content: "tomato"},
{text: "milk", content: "eggnog"},
{text: "milk", content: "milk"},
{text: "best before: 2020", content: "yikes"},
{text: "milk", content: "nothing"}
]

// Be imperative!

function findMilkImperative(boxes) {
const boxesToGive = [];
for (const box of boxes) {
if (box.text == "milk") {
boxesToGive.push(box);
}
}
return boxesToGive;
} /* So you say:
iterate through each of the boxes
and if it says "milk" then collect it
and then deliver them to me */



// Be functional!

function findMilkDeclarative(boxes) {
return boxes.filter(box => box.text == "milk")
} // Got it? Gimme! ... Got it?

Both these functions will return you a collection of all the boxes with the text “milk” on them. So WHAT they do is the same.

[
{text: "milk", content: "eggnog"},
{text: "milk", content: "milk"},
{text: "milk", content: "nothing"}
]

But HOW you implement them is way too different.

Get to the fridge!

I know now you say: “It’s nice, but it’s just syntactic sugar”.

And you are kinda right. Actually the declarative approach can show its real power in more complex situations.

Like when these boxes are in the fridge in the kitchen.

If we are ‘imperators’ we have to tell that “child” more and more step by step instructions: how to get to the kitchen and that they have to open the fridge door to get that milk. Not forgetting about to close it after all.

And what if the fridge door remains open?

A side effect happens…

A side effect means that something else is being changed other than what was intended. In programming it is a change in the state of something outside its scope (eg. a value of a variable, writing to a disk or to the console, etc.)

As in real life in your fridge — where terrible things can arise in your delicacies — , it can make your code indeterministic, or strange bugs can pop up.

While we just wanted some milk.

However, when we live in a declarative world it can happen the adult you ask is a superhero and can take the milk out of the fridge without opening it, what’s more, it is possible they can fetch it for you by walking straight through the walls!

You just have to know what you want, and not how it can be achieved.

And you don’t even have to take care about that damn fridge door, so you need have no fear of side effects.

Cheers!

--

--