Functional Programming Programming

Immutability in functional programming | The Path to less headache

December 27, 2018
Immutability - Functional Programming

What it is?

One of the fundamental topics in Functional Programming is Immutability and one who is learning or trying to write his/her code in a functional way needs to learn what immutability means. If we search it on Wikipedia, we will see that it says, an immutable object is an object whose value can’t be changed after it is created. In short, it’s a variable that can’t change its value. That variable is unchangeable like a non-removable marker.  As in the previous part, we’ve said the dangerous side effects that cause the state/value of a program’s variable resulting in an unexpected bug. Because of that, we should always try to use immutable variables.

Misconception

Now a common misconception is, value immutability doesn’t mean we can’t have values change over the course of our program. The first time I read about immutability I was really confused! How can you make a program where it’s variable will not change its value? To me, it was difficult to understand.

There is nothing easier than giving an example

Take a look at this code segment. This is written in a functional programmers way.

Notice here, we did not change any variable at all. Instead, create a new array inside the function and return it. So the caller of this function did not get any previous array being mutated on the other hand he actually got a new array. Why should we do this? Let’s look at the same example written in a non-functional way.

In this example, we’ve created a global variable that works as a shared variable. Instead of returning a new variable we got the main variable as mutated. Now whether you are working alone or in a team, when you write this block of code you already know what you are trying to achieve but your team might not! Even you will also forget about this code segment after a few months of writing this code. Trust me. You will forget. I’ve been there. Thus you might see a few unexpected bugs are being created that are hard to detect!

References passed not the value

Non-primitive values are held by reference so when we pass a variable through an argument of a function it’s reference is actually passed. Here look at this function

How can you give a guarantee, that arr[0] will have 1 as its value? You don’t know for sure. Cause foo() function might be changing its values. Then it becomes a new headache for the coder about which function is running before what and who is actually responsible for mutating the variable.

Whom to trust?

Now look at the first code segment and ask yourself. Is it pure function? Does this function have any side-effects? Yes, it is pure function and does not have any side-effects. It gives the same output, given the same input. So we can safely execute this function as much as we want and no other part of our program’s state will be changed. By being strict about creating immutable variables and writing pure functions we are able to reduce the amount of ‘surprise’ bugs and use our code with ultimate trust.

The “const”

Starting from ES6 we can now use the ‘const’ keyword. By using this keyword we are able to create a variable that can’t be reassigned. So for this, we can now use const instead of var keyword and we won’t have to worry about the variable being mutated by some other function. (If you never heard of it, check this link)

P.S: Although you can’t change a variable defined with const you can push an object or data inside a const array.

You should keep this in mind when you are working with arrays in JavaScript.

That’s it for today. In the next post I’ll talk about four functions that follows immutibility and their real life uses. They are one of the most used functions in JavaScript community. Here is my next post.

You Might Also Like

No Comments

Leave a Reply