Immutability

So this is a concept that I have known for a while but have not been sure why it’s such a good idea. After going through a few documents and watching a 30 min talk I think I have a better understanding of what it’s all about.

Basically, you do not want your object to mutate. Because when you are not sure what has changed. For example,

let A = 1;
doSmth(A);
console.log(A); // What is A here?

It totally depends on what the function does and it is difficult to track how the values have changed over time. The library that I read up on was immutableJS, this library uses a Directed Acyclic Graph internally to represent the values internally. It makes sense because DAG will never point to a node causing a completed graph. The implementation is cool but shall not go into that.

So basically, the benefit of using an immutable data structure is that it allows me to compare between two objects much more efficiently. Because of the way DAG works, it can answer the question of “what changed?” very quickly. You can ensure that updating a variable do not cause weird bugs in a different portion of your application, because it’s not the “same” variable.

If it seems a bit confusing, it’s because it is and I’m dying. To the point where I actually need to write things down like these to remember what the hell I’ve been learning.

Till the next puzzle piece.

Leave a Reply