» Glossary » Immutability
An element is considered immutable if it cannot be modified once created. Data changes are encapsulated as operations that take the old element/state and return a new derived element/state.
For example, if the i variable was a mutable element, we’ll do the following:
int i = 0; ... i = 1; ... printf(i);
However, if the i variable was immutable, to get the same result, we’ll do:
int i = 0; ... int j = i + 1; ... printf(j);
Immutability is a core concept in Functional Programming. Its usefulness comes from better code readability, optimisation, and testability.
Lee Byron –Immutable.js author- summarise the fundamental concepts of immutable structures and how they fit in the React ecosystem in the following talk:
- Pros and Cons of using immutability with React.js
- Purely functional data structures, published in 1998 by Chris Okasaki. The original thesis (PDF) becomes a book that publishes efficient implementations of immutable structures.
- Practical undo, an example on how to use immutable structures to build the undo/redo feature.