Immutability
Updated 2026-08-05
Summary
Immutability means data is never modified after creation, so any change produces a new object and the original stays intact, eliminating shared-state races.
Definition
A property of data and program state in which a value cannot be altered after it is created, so updates return new values and leave prior ones untouched. Immutable structures make reference transparency possible, since an expression always yields the same result, and they remove the shared mutable state that produces race conditions in concurrent programs. The core claim is that forbidding in-place mutation buys reasoning simplicity and safety at some memory cost. The qualification is that copying carries a performance price, so hot paths may still need localized mutability.
Introduction
How to use
- Use Default Immutable: Choose immutable types for shared data unless a measured reason exists. - Use Replace Not Edit: Return a new value for each change and keep old references for comparison. - Use Lock Reduction: Rely on immutability to remove races instead of adding locks around shared state.
Mechanism
Concurrency bugs arise when two execution paths read and write the same memory with no agreed order, so one path sees a partially updated state. If values cannot change after creation, every reader observes a complete and consistent object, and no ordering discipline is required. The cost shifts from coordination to allocation, since each change creates a new structure, and that trade is usually favorable except where allocation dominates.
Practice
In progress 0/1Practice this model in one real situation:
Cases
- Functional languages such as Haskell and Elm make immutable values the default.
- React state updates create new objects so rendering can compare references safely.
- Version-controlled documents keep every prior revision intact rather than overwriting the original in place.
Common pitfalls
- Pursuing purity while ignoring performance hot spots makes every update allocate unnecessarily.
- Reading immutability as an inability to evolve misses that change happens through new versions.
- Treating it as only a coding practice overlooks its value in stable interfaces and contracts.
Sources
Source support: Explicit
References currently contain only Wikipedia links; primary sources still need review.
- en.wikipedia.orghttps://en.wikipedia.org/wiki/Immutable_objectverified
account_treeGenealogyexpand_more
PRIVATE NOTES · Only visible to you
SAVED Q&A
ENTRY Q&A · Private saving available
Ask with a clear boundary
thinkingmodels answers from published entry context only.
Your question is sent to thinkingmodels. The answer uses public entry context only.