liveBook · Manning

Algebraic law

As explained in Data-Oriented Programming in Java

An algebraic property is a characteristic describing how an operation behaves under operand order, grouping, and relationships between inputs and outputs.

An algebraic law states what an operation must mean for every valid input, rather than merely describing a few expected cases. For a set of values and deterministic, side-effect-free operations, laws make correctness discussable without tying it to a particular implementation. They ask practical questions: does operand order matter, can calls be regrouped, and which relationships between inputs and outputs must remain true? section 7.2 section 7.4

Start with the domain operation that needs an unambiguous meaning. A merge of two conflicting customer records can be written as a + b before its implementation exists. The useful work is then to specify equations that hold for all records: perhaps reversing inputs produces the same result, or regrouping many merges changes nothing. Those equations become a compact contract for both design and tests. section 7.1 section 7.2

Common laws expose different kinds of behavior:

  • Associativity permits regrouping: a + (b + c) = (a + b) + c. It decouples a computation from one fixed sequential grouping and can enable parallel computation. section 7.12 section 7.4
  • Commutativity makes argument order irrelevant: a + b = b + a. Subtraction demonstrates the opposite: reversing operands changes the result. section 7.12 section 7.4
  • Ordering laws define consistent comparisons through reflexivity, antisymmetry, and transitivity. Monotonicity then describes functions that preserve an ordering. section 7.12 section 12.3

Test laws over the states your types denote

A law normally begins “for all states.” For a finite domain, generate every value a type can represent and assert the invariant across each value or combination of values. A RawData record, for example, can be enumerated by combining every Policy, AuditFinding, premium flag, and each Optional empty state. This turns the denotation of a type into exhaustive test data. Formal ∀ notation helps state the same intent without loop noise, forcing a decision about which states and invariants actually matter. section 7.5 section 7.5.2

Laws guide implementation but do not dictate a fashionable abstraction or interface. A duplicate-record merge can use Optional-aware binary operators, comparisons, and Boolean OR while its properties remain valid for any implementation. Preserve clear domain semantics first; do not force an elegant property onto an operation whose meaning does not support it. section 7.9 section 7.12

The same reasoning applies beyond reductions. Model allowed lifecycle transitions as an ordering: empty precedes non-empty states, a state cannot transition to itself, and terminal states have no greater successor. The resulting laws can drive integration tests that check whether a database dependency enforces the intended transitions. section 12.3 section 12.8

Listing 7.32 Writing down what we know
∀a,b ∈ ConflictingRows  
 
Properties: 
  a + b = "a more favorable record" (whatever that means) #A

Use the compact “for all conflicting rows” form to separate an incomplete business meaning from the implementation that must eventually realize it. section 7.5.2

How Data-Oriented Programming in Java covers it

How deep it goes
Explains universal assertions, associativity, commutativity, ordering, monotonicity, and exhaustive Java checks; defers no single required implementation.
Examples
Uses conflicting RawData merges, subtraction and floating-point addition, generated Optional states, cart invariants, and task lifecycle transitions.
What it assumes
Assumes intermediate Java familiarity while introducing the needed logic, set-based state reasoning, and universal notation from zero.
What you can do after
Evaluate/understand operations by stating laws precisely and verifying invariants across generated states or external dependencies.
How it connects
Connects deterministic functions, invariants, Optional, product types, Comparator, Stream.reduce, algebraic data types, and integration testing.

Also explained in

Compare how 3 books explain algebraic law →

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.