liveBook · Manning

State

As explained in Data-Oriented Programming in Java

The data values that describe the current condition of an object or entity.

State is the data describing an object or entity’s current condition. In Java, its meaning often hides behind combinations of fields: one flag set, another unset, or several fields changed together. That implicit encoding makes invariants difficult to see and maintenance difficult for anyone who did not write the code. section 1.3

State is not the same as identity. A variable or object provides a place with continuity over time, while its stored values change. Data-oriented design can instead represent identity through values and model change as a succession of states. section 2.1 section 2.5

A useful state model makes these distinctions explicit:

  • Treat a value as a single immutable state with no semantically meaningful identity; equal state means equal value. section 2.2
  • Represent change by passing the current state into a function and returning the next state rather than mutating the existing data. section 2.5
  • Model behavior as a discrete transformation between named data states, even when the behavior resembles an external action such as an API call. section 5.3
  • Use state coverage in tests to explore combinations and rarely exercised cases, while avoiding hard-coded values that create hidden coupling. section 11.8

Moving a ball without mutation

A Ball begins at position (0, 0) with velocity (1, 0). Applying step for one second returns a new Ball at (1, 0); applying it again returns (2, 0). The earlier states remain available as data. section 2.5

Listing 2.27 Computing the next state from the current one
import static com.dop.chapter02.step
 
float simulatedDuration = 1.0f;
Ball ball = new Ball(new Point(0.0, 0.0), new Vector(1.0, 0.0)); #A
Ball updated = step(ball, simulatedDuration);
// [out] Ball(position=(1.0, 0.0), vector=(1.0, 0.0));
Ball updatedAgain = step(updated, simulatedDuration);
// [out] Ball(position=(2.0, 0.0), vector=(1.0, 0.0));

Trace how a transition function computes each next Ball state from the preceding one. section 2.5

How Data-Oriented Programming in Java covers it

How deep it goes
Explains state through identity, immutable values, and transitions, while deferring implementation details during early domain design.
Examples
Uses Java fields, Customer objects, Vector values, Ball frames, and state-driven test data.
What it assumes
Assumes working Java knowledge and teaches the distinction between object identity, mutable state, and value semantics.
What you can do after
Build it from scratch as immutable values and functions that transform explicit current states into next states.
How it connects
Connects state with identity, values, immutability, functions, domain modeling, and testing.

Also explained in

Compare how 52 books explain state →

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.