liveBook · Manning

Mutation

As explained in Data-Oriented Programming: Reduce software complexity

A mutation is an operation that changes the state of a system or data structure.

A mutation changes system state. The tempting design is to modify a hash map in place, but that makes invalid states harder to prevent and erases previous versions. A multi-version design instead advances a state reference from one immutable data version to the next, much like Git tracks commits. section 4.1 section 4.0

The crucial distinction is simple: the data is immutable, while the state reference is mutable. This confines statefulness to a small, common commit operation. section 4.1 section 4.0

Handle a mutation in two phases:

  • Calculation computes the next data version with immutable functions. It is stateless and specific to the mutation. section 4.0
  • Commit moves the system state forward to that version. It is stateful, common to all mutations, and can validate the complete data set before the move. section 4.0 section 4.8
  • Structural sharing keeps unchanged parts between versions shared rather than copied, limiting memory and computation costs. section 4.0 section 4.8

Changing one map field

Instead of using a native hash map setter, use an immutable operation such as Lodash FP's set(map, path, value). The operation returns a new version, leaving the received data unchanged; the commit phase then makes the state reference point to it. section 4.8

Table 4.1 The two phases of a mutation (view table figure)

Phase

Responsibility

State

Implementation

Calculation

Computes the next version of system data

Stateless

Specific

Commit

Moves the system state forward

Stateful

Common

Compare calculation and commit by responsibility, statefulness, and implementation scope. section 4.0

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Explains phases, structural sharing, validation, and history, while deferring concurrent mutations.
Examples
Uses Git version history, hash-map updates, version diagrams, and Lodash FP's set function.
What it assumes
Assumes queries, hash maps, and basic system-state concepts while distinguishing immutable data from mutable references.
What you can do after
Apply it via Lodash FP to calculate immutable versions and commit validated state transitions.
How it connects
Connects mutation with immutable data, structural sharing, version control, state management, and concurrency control.

Also explained in

Compare how 12 books explain mutation →

Related concepts

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