Functional Programming: Immutability

Functional Programming: Immutability

Learn the concept of Immutability & how it's useful in functional programming.

Assalam u Alaikum & Hello everyone! ๐Ÿ‘‹

Hope you all are fine โœจ

In the last article, we learned about what is Functional Programming & some of the programming paradigms around it.

In this article, we will learn about Immutability that is an important concept to understand FP.

Contents

  • โšก What is Immutability.
  • โšก Is Mutability a bad thing?
  • โšก JavaScript & Immutability.
  • โšก Immutability & Functional Programming.

๐Ÿ’ก What is Immutability?

Immutable means "YOU CAN NOT CHANGE IT". Any object that has never changed its state after creation, is called an immutable object. It's the inverse state of Mutability which means you can change the state of the object after its creation as well.

Immutability is always preferred in the world of functional programming because it helps us to write pure functions & safer code.

Let's understand both of these concepts by a simple example.

Suppose, we have an array and we want to add another item to it.

const arr = [1, 2, 3, 4];
arr.push(5);

console.log(arr); //  [1, 2, 3, 4, 5]

Simple right? Now let's just add another array and copy the first one into it. And add another item in the second array.

const arr = [1, 2, 3, 4];
arr.push(5);

// โŒ Reference is also copied.
const arr2 = arr;
arr2.push(6);

// โŒ Both the arrays are mutated.
console.log(arr, arr2); // [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]

Okay, what just happened? This is a typical problem related to mutability. When the references are not broken, it leads to unexpected things. To solve this problem we can use the slice method, or the ... (spread) operator to copy the array and then add a new value.

const arr = [1, 2, 3, 4];
arr.push(5);

//๐Ÿ’ก Create a new array & copy the previous one (break the reference).
const arr2 = [...arr];
arr2.push(6);

// โœ… Both arrays are different & immutable.
console.log(arr, arr2); // [1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 6]

Now you will understand that how immutability can help us to write safe code. It helps us to maintain separate boundaries that do not lead to unexpected bugs.

Is Mutability Bad?

image.png

Since immutability helps us to write safer and clean code doesn't mean that mutability is bad. There are tons of tons of use cases where mutability helps us the most instead of immutability.

Mutable objects can be modified and transformed into other data objects which is a power on its own. And for a large software system, creating a new object every time is not a good option.

Suppose that you have a game and there is an object of character in that game which has a property speed. Now, for every one second, the speed is changed 100 times. Ask yourself, if you are going with the immutable approach. You will be creating 100 new objects per second. Not a good idea!

Instead, you can use the power of mutability and update the speed and maintain a single object. More performant and secure.

JavaScript & Immutability

Immutability is highly admired in the world of javascript nowadays. Many of the famous libraries like React & Redux are using the power of immutability. Also, the core language itself has features like the const keyword that helps to not mutate the state after it's created.

image.png

There are a bunch of libraries in the javascript ecosystem as well that provide many easy ways to work with objects, arrays, and other data types with immutability. One of the famous ones is Immutable.js which has almost 31k+ stars as of now.

Immutability In Functional Programming

Immutability is one of the most important concepts which is highly used in the world of FP. It helps us to write pure functions, drive states from one state to another state.

It helps us to build functions and compose them with new inputs without mutating the original objects. We will learn about pure functions in our next blog in detail.

I highly recommend you to learn the basics of Immutability in Javascript (or any other language in which you want to write FP).


That's it, folks! hope it was a good read for you. Thank you! โœจ

๐Ÿ‘‰ Follow me: Github Twitter LinkedIn Youtube

ย