Hacker News new | ask | show | jobs
by Kiro 4399 days ago
I don't understand the obsession with mutability. When is it a problem that a variable is mutable?
4 comments

It is a very different way of thinking about programming, but immutable inputs remove a whole class of bugs that can happen.

It is analogous to global variables. We know they are bad, because any other piece of code can change them and break our code.

A variable having state is similar. When you think about what a function does or write tests... having the variable be able to have many unknown states increases the complexity.

In code we write everyday, a variable might be undefined, null, a valid phone number as a string, etc. But in immutable code, if your input comes from a function that returns either None or a valid phone number as a string and no other code can tweak this... then your code becomes much easier to think about and write tests for.

Check out Rich Hickey's (the creator of Clojure) talk The Value of Values. He makes a good case for immutability.

http://www.confreaks.com/videos/1830-jaxconf2012-keynote-the...

When it surprises you. It is easier to reason about code when you know that values will not change - an immutable value cannot change so you don't have to worry about passing it around, using it in asynchronous processes, changing it without persisting the change, etc.
Probably because is hard to track the chain of calls that could have change the data. If inmutable, you are sure what exactly you have at hand.

But the article point to a inconsistent mutable/inmutable behavior that is more problematic.