You’re reading an excerpt of my upcoming book on clean code, “Washing your code: write once, read seven times.” Preorder it on Leanpub or read a ...
For further actions, you may consider blocking this person and/or reporting abuse
While Immutability can make it easier to reason about your code and avoid errors when using reference comparison, it can also come with a hefty performance cost that may as well turn it into an anti-pattern.
Consider the following code:
Multiple arrays are created on every iteration. Contrast it with the following:
The latter is significantly faster.
Hey Alex, great point! And I mention performance in the newer version of this post: blog.sapegin.me/all/avoid-mutation/
I think this could be a bit more nuanced. I like that you make clear that you should never mutate values without making it obvious. I still think that immutability as a pattern is somewhat overrated, especially in narrow scopes.
As a beginner, avoiding mutation makes sense. Once you become more advanced, you can reason about why to use mutations in certain cases. Though I definitely agree that mutating arguments is an anti-pattern.
Also, you should consider not using arrays at all if you don't explicitly need them; iterators and generators might be a better choice in some cases.
I agree that immutability is not a pattern, and I'd much prefer to have a language that's immutable by default, so you don't have to think about it. And I like the idea about the scope.
I think the main problem with mutation is that the problems caused by mutation are often very tricky to debug. Mutation of a function parameter, even if it's a very small function, could cause problems very far from this function.
Lots of great ideas! Thanks for sharing!