You don’t need ImmutableJS

Image for post
Image for post

Before I start working on a front-end project I didn’t know much about immutability and functional programming. But after spending time writing React/Redux I start to mention that practice to make everything immutable make your life simpler because you do not bother about the state of your objects anymore and when you make operations over them you don’t think about whether or not it will change some props of it. And in a first front-end project, I work in was ImmutableJS. I was not good at JS and React/Redux, so I decided to ask why the team using the library, they told me, it easier to manage state when you use ImmutableJS. But it was not true. After spending time working on the project I start to understand that there is no so much help in using ImmutableJS.

When you decide to use a library better think — is it worth to include it in your tools box. As more libraries you use as more times you will find yourself in documentation pages. For example, the redux-act library is very helpful in large projects where a lot of actions and reducers — it makes your programming life easier because you don’t need to type redundant code. Otherwise, the axios library will not help you much because all this functionality can be replaced by plain JS fetch.

ImmutableJS have large API for making your objects immutable, but it is simple to keep objects immutable without ImmutableJS.
Also, ImmutableJS can affect the entire code base. When you make state immutableJS object you need to convert it to plain JS object for components or use it everywhere. And there is mess occurs, in one place you convert the Immutable object to plain JS, and in other — not.

One of the biggest point of using ImmutableJS it is a performance, but I am sure in your day to day js programming performance is the last thing you think about.

How not to use ImmutableJS

At first, you should understand that the project written with not typed language need some conventions that will be respected by the team. First, is make everything immutable. For arrays, you should forget about loops, push()-like methods and mutate by index. If you see yourself writing a loop try to think about how it can be done using array methods such as map/reduce. If you want to push something — create a new array:

If you work with objects you stick to this:

If you work with complex objects you will most likely use classes and it is easy to keep them immutable too. You just need to return a new object or immutable type in each method. For example point class:

Immutability is your choice and writing immutable JS code is simple:)

Reach the next level of focus and productivity with

Image for post
Image for post

Written by

Software engineer, creator of More at

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store