liveBook · Manning

Chapter 7: Reasoning About Correctness

What you will learn in chapter 7 of Data-Oriented Programming in Java

The introduction proposes algebraic reasoning about values and their relationships as a way to judge whether code is simpler.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction proposes algebraic reasoning about values and their relationships as a way to judge whether code is simpler.

Key terms: algebraic properties, set of values, relationships between values, simpler

7.1 We begin with an exercise

An ETL exercise with conflicting customer records introduces the need to define correctness independently of an implementation, using laws about what the data should mean.

Key terms: ETL, conflicting records, nulls, favors not billing the customer, coverage, correctness, implementation, thinking algebraically, equations, laws

7.2 Thinking Algebraically

Introduces algebraic thinking as a way to define operations on data and reason about correctness independently of implementation.

Key terms: algebra, operations, deterministic functions, equational reasoning, laws, all possible inputs, conflicting records, add, correctness

7.2.1 Standing on Shoulders, Not Toes

The section introduces closed binary operations as a useful algebraic shape and shows why recognizing that shape in Java code helps programmers reason about it.

Key terms: binary operation, closed, BiFunction, unary, algebraic properties, algebra, algebraic shape, laws and tools for reasoning

7.3 Noticing algebraic operations in every day code

The section reveals binary operations hidden in imperative Java code and uses them to simplify a duplicate-record processing pipeline.

Key terms: imperative code, binary operations, side-effects, state updates, List.add(), Lists, binary operation, Map, putAll(), collisions

7.4 The algebraic properties of binary operations

The section shows why a binary operation’s behavior across different argument orders, groupings, and inputs matters when judging its correctness.

Key terms: binary operations, argument order, grouping, Stream.reduce(), algebraic operation, properties, inputs, outputs

7.5 Invariants, Properties, State, and an upside-down capital A

The section shows how to turn the values a type can represent into exhaustive assertions about individual states and relationships between states.

Key terms: algebraic properties, invariant, all possible states, Product Types, cartesian product, Optional, empty state, for all, relationships between states, exhaustive set

7.5.1 Every possible state? Surely too inefficient to be practical!

The section counts the domain’s possible states and argues that generating every state is often practical, while acknowledging limits.

Key terms: Policy, AuditFinding, Boolean, Optional, empty(), states, for-loops, benchmark, state spaces

7.5.2 Expressing these ideas without code

The section introduces “for all” notation as a concise, precise way to express claims about every state and to think more carefully about invariants.

Key terms: ∀, for all, variable, set, states, invariants, formal notation

7.6 Associativity

The section explains associativity as a law of regrouping binary operations that enables flexible execution and gives a concrete criterion for correctness.

Key terms: associativity, binary operations, grouping, Streams API, reduce, parallel, concrete law

7.7 Noticing more properties

The section shows how looking for a property behind complicated selection logic reveals an ordering problem that Java’s Comparator can express.

Key terms: DRY, properties, binary operation, RawData, favorability, ordering, sort, lexicographically, Comparator

7.7.1 Ordering as a property

This section uses ordering properties to describe correctness independently of an implementation and shows how to test a domain ordering.

Key terms: ordering, total order, Comparable, reflexive, transitive, antisymmetric, favorability, properties, states, permutation

7.7.2 Monotonic Relationships between inputs and outputs

The section explains why combining favorable data must preserve its ordering and how Comparator-based operations provide that monotonic behavior.

Key terms: monotonicity, favorability, ordering, Comparator, add, BinaryOperator, maxBy

7.7.3 The properties that define what it means to be correct

The section explains how properties of every possible state can specify what it means to combine conflicting domain data correctly, and suggests ways to find those properties.

Key terms: properties, states, invariants, test suite, pattern recognition, Associative, ordered, Monotonic

7.8 Optional as a parallel world

The section explains why an algebra designed for present values needs to work with Optional, using a parallel-world analogy to introduce lifting behavior.

Key terms: binary operator, Optional, algebra, Optional.of, Optional.empty(), parallel world, lifting, behavior

7.8.1 Dealing with the IDE telling us we're doing it wrong

This subsection explains when accepting Optional as a function argument can be reasonable despite IDE warnings and common advice.

Key terms: Optional, generic type variable, linter warnings

7.8.2 Lifting Binary Operator into the world of Optional

The section derives a general-purpose way to lift a BinaryOperator so it can be used with Optional data.

Key terms: lifter, lift, BinaryOperator, Optional, lifted functions, Optional data, plumbing

7.8.3 Moving our algebra into the world of Optional

The section applies lifted binary operators to make the existing algebra work with Optional values, while leaving its correctness to be checked against the chapter’s properties.

Key terms: lift, binary operators, algebra, Optional, properties

7.9 The finished implementation

The section presents the finished implementation and emphasizes that its algebraic properties matter more than its particular code.

Key terms: algebraic design, algebraic properties, implementation

7.10 A final note on representing algebraic ideas in code

The section explains why the chapter’s algebraic ideas are implemented with the regular Java standard library rather than new specialized interfaces.

Key terms: special interfaces, Java Collection classes, Java standard library

7.11 Wrapping up

The section closes by emphasizing the practical value of recognizing algebraic properties in everyday programming.

Key terms: properties, algebraic world

7.12 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 6: Implementing the Domain Model Chapter 8: Business Rules as Data →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.