Immutability
Why immutable data makes programs easier to reason about: what immutability means, the bugs shared mutable state causes, immutable values in Java, Python, JavaScript and Kotlin, shallow versus deep immutability, copying with changes, persistent data structures, performance trade-offs, and where mutation still makes sense.
CodexBash, , 6 min read
An immutable object cannot change after it is created. To "change" it, you create a new object with the differences, and the original stays exactly as it was. It sounds wasteful, yet immutability removes whole categories of bugs: values that change unexpectedly because another part of the program held a reference, data races between threads, and caches or map keys that silently break. Strings in Java, Python and JavaScript are immutable for exactly these reasons. This article explains the idea, shows how to use it in common languages, and discusses when mutation is still the right choice.
Sign in to read the rest
The whole 6 minute article is free with an account, along with every problem, lesson and tutorial. No card.