Glossary termConcept

Mutability

Also known as: Immutability, Mutable and immutable objects

The property that determines whether an object or value can be changed after it is created; immutable values stay fixed once made.

Mutability describes whether a piece of data can be modified in place. A mutable object, such as a list in Python or an array in JavaScript, can have its contents changed after creation: items can be added, removed or replaced while it remains the same object. An immutable object, such as a string in Java or Python, cannot be altered. Operations that appear to change it actually produce a fresh value and leave the original untouched. Many languages offer both kinds and let programmers choose which to use.

The distinction matters most when several parts of a program hold references to the same object. If one part changes a shared mutable object, every other part sees the change, which can be useful but is a frequent source of surprising bugs. Immutable values avoid this: once created, they always represent the same value, so they can be shared freely, used safely as keys in a hash table and passed between threads without locks. Defaults differ between languages. Rust makes variables immutable unless they are marked mut, and Clojure's core collections are immutable by design.

Immutability is central to functional programming, where programs are built from pure functions that compute fresh values rather than modifying existing ones. Persistent data structures make this efficient by sharing unchanged parts between versions instead of copying everything. The cost can be extra memory allocation and some loss of raw speed in tight loops, so performance-sensitive code often still uses carefully contained mutation. The same idea appears at a larger scale in immutable infrastructure, where servers are replaced with fresh ones rather than modified in place.

Key points

  • Mutable objects can be changed in place, while immutable objects keep the same value for their entire lifetime.
  • Shared mutable state is a common source of bugs, especially in concurrent programs where several threads touch the same data.
  • Immutable values can be shared, cached and used as hash keys without defensive copying.
  • Persistent data structures reduce the cost of immutability by reusing unchanged portions of earlier versions.

From the source

...some objects are immutable: once created, they always represent the same value. Other objects are mutable: they have methods that change the value of the object.

Source: MIT (opens in a new tab)

Related entries

Glossary term

Pure Function

Programming

Function that always returns the same output for the same inputs and has no side effects, such as modifying outside state or performing I/O.

Glossary term

Programming paradigm that builds software by composing pure functions, treating computation as evaluating expressions rather than changing state.

Glossary term

Concurrency

Programming

Ability of a program or system to make progress on several tasks during overlapping time periods, whether or not they literally run simultaneously.

Glossary term

Race Condition

ProgrammingCybersecurity

Flaw in which a system's behavior depends on the uncontrolled timing or ordering of events, such as two threads updating the same data at once.

Glossary term

Hash Table

Programming

Data structure that stores key-value pairs and uses a hash function to compute where each item lives, giving very fast average lookups.

Glossary term

Approach in which servers and other components are never modified after deployment; changes are made by replacing them with freshly built versions.

Technology

Rust

ProgrammingFOSS

Systems programming language that guarantees memory safety without a garbage collector through its ownership model, first stable in 2015.