DEV Community

Discussion on: What's the big deal about immutability?

Collapse
 
aminmansuri profile image
hidden_dude

While I agree with your programming principles in the OO context of things. Its not really what the immutability game plays out in the functional or concurrent/parallel worlds.

The big deal about immutability is when you deal with parallelism. If you don't allow mutation in your code then you can use many threads to do the same work. In functional code its also about eliminating side effects that don't allow you to make optimizations such as f(x) + f(x) = 2 f(x).

What you are discussing is just good OO programming practice so no inadvertent things happen to your encapsulated objects. In commercial programming you should always make it hard for other programmers (including yourself) to break your program. I've been known to add traps in the code so if you do changes to the code it will explode. Like

counter = 0;
while (new = transform(old) ) {
// lots of logic
// trap
if (++counter > 10_000) throw new AssertionFailedError("too many iterations");
}

(you can also change the while into a for statement but that's besides the point)

Collapse
 
shalvah profile image
Shalvah

Hehe. I'm aware that immutability helps with thread-safety. Just didn't want to go there in this post (plus I haven't really had many such issues). This is not a post about functional programming or about the whole immutability philosophy. Just wanted to give folks who've heard the term but don't know what it is an understanding. That's why I linked to those other posts at the end.