3 ms·
I don't understand the obsession with mutability. When is it a problem that a variable is mutable?
by Kiro 12y ago
I don't understand the obsession with mutability. When is it a problem that a variable is mutable?
- mamcx 12y agoProbably 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.
- perishabledave 12y agoCheck 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-value-of-values http://www.confreaks.com/videos/1830-jaxconf2012-keynote-the...
- netcraft 12y agoWhen 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.
- ozten 12y agoIt 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.
- deleted 12y ago[deleted]
- colanderman 12y agoMost modern compilers transform source code into a single-static assignment form, rendering (im)mutability notations useless from an optimization standpoint. (Ignoring for now the case of non-local assignment, e.g. through aliased pointers.) What you actually gain from (im)mutability notation is documentation and sanity checks: it's a huge win to tell the compiler "yell at me if I ever try to modify this variable", which unambiguously indicates to a human "don't worry, this variable is never modified in this code".