liveBook · Manning

Mutation

As explained in Data-Oriented Programming in Java

Mutation is the modification of an existing program value or state in place.

Mutation changes an existing value or state in place. It can make an object appear continuous over time, but continuity can also be represented as a succession of values: frames of state played back in order. The observed result can be the same without updating one object in place. section 2.4

Figure 2.4 A succession of values laid out on a timeline

A timeline of state frames separates the identity being modeled from the particular mutable object used to hold its current state. section 2.4

The practical question is not whether mutation is always bad, but whether its scope is controlled. Mutable references shared across a legacy object graph let distant code alter state, bypass invariants, and make behavior depend on hidden sequencing. Mutation can contribute to invalid partially constructed states, though it is not their only cause. section 9.2.2 section 9.2.4

Build data with local mutation

Cart balancing can use mutable maps and a for-loop to move unavailable items into saved items and split insufficient quantities. When those maps are created inside the method, never escape it, and immutable data enters and leaves the method, the operation remains pure and deterministic despite its internal mutation. section 9.3.5

When mutation mostly performs accumulation, an alternative is to compute new immutable data with an addition-like operation. List concatenation and map merging make collision policy explicit; map and reduce can then express grouping as data combination rather than manual state management. This is a tradeoff, not a ban on loops or mutable collections. section 7.3 section 9.3.5

Keep mutation useful by making its boundaries explicit:

  • Confine mutable collections to a method or similarly narrow scope; do not let callers retain references to them. section 9.3.5
  • Return immutable results after using mutation as an implementation detail. section 9.3.5
  • Protect invariants with data types and construction rather than relying on every later mutation site to remember checks. section 9.6 section 9.2.2
  • Choose algebraic combination when it makes the intended data transformation clearer than manual state management. section 7.3

How Data-Oriented Programming in Java covers it

How deep it goes
Contrasts state timelines, scoped implementation mutation, and algebraic replacements without prescribing mutation’s elimination.
Examples
Uses a tossed ball’s state frames, duplicate-record grouping, and a shopping cart balancing routine.
What it assumes
Assumes intermediate Java familiarity with collections, loops, maps, and immutable data; teaches mutation through focused refactorings.
What you can do after
Evaluate/understand when mutation is safely scoped and when immutable combination better exposes intended transformations.
How it connects
Connects mutation to identity, immutability, pure functions, invariants, encapsulation, algebraic operations, map, reduce, and shotgun parsing.

Also explained in

Compare how 13 books explain mutation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.