liveBook · Manning

Missing value

As explained in Data-Oriented Programming in Java

A special Java reference value indicating the absence of an object rather than an instance of the referenced type.

A missing value is Java’s null reference: it signals the absence of an object, not an instance of the declared type. That makes a field typed String less trustworthy than it looks—unless the model states whether absence is allowed. In data-oriented code, optionality is domain information, not an implicit escape hatch. section 2.6.3

Consider a procedure step that records who completed it and when. Those facts are meaningful only after completion. Adding nullable completedBy and completedOn fields to one Status record makes its meaning depend on combinations of a boolean and nulls. It also permits impossible combinations: an incomplete step with completion details, or a completed step without them. section 4.1.4

Defend a required value at construction

For data that must always exist, reject null at the boundary. A compact constructor makes the requirement visible and prevents a record that is structurally populated but semantically empty. record Person(String firstName, String lastName) { Person { Objects.requireNonNull(firstName); Objects.requireNonNull(lastName); } } section 2.6.3

Constructor checks are a practical safeguard, especially while refactoring code shaped by older habits. They can harden known invariants and stop invalid data from propagating. But checks around a nullable field and a status flag are also evidence that the representation is carrying several states whose distinctions should be made explicit. section 4.1.4 section 9.3.1

Use missing values deliberately rather than as unspoken state:

  • Treat a non-null field as a domain claim: construction must provide the value. section 2.6.3
  • Identify fields whose relevance changes with lifecycle state; do not let null combinations define that lifecycle implicitly. section 4.1.4
  • During a transition, explicitly reject nulls where an invariant requires presence instead of relying only on team convention. section 9.3.1
Listing 2.44 defending against nulls in the constructor
record Person(String firstName, String lastName) {
    Person {
        Objects.requireNonNull(firstName);  #A
        Objects.requireNonNull(lastName);   #A
         
    }
}
 
 
Person person = new Person(null, null); // ERROR  #B

This compact constructor turns required names into an executable rule: attempts to construct Person with null values fail immediately. section 2.6.3

How Data-Oriented Programming in Java covers it

How deep it goes
Explains nulls through record constructors and inconsistent lifecycle fields, while deferring broader techniques for evicting them.
Examples
Uses Person with null names, Status completion fields, and Cart identifiers and item fields.
What it assumes
Assumes intermediate Java familiarity and develops null handling through records and constructor checks.
What you can do after
Apply it via Objects.requireNonNull to enforce required fields during construction and expose hidden optionality.
How it connects
Connects null handling to records, constructor validation, invariants, optionality, and invalid states.

Also explained in

Compare how 39 books explain missing value →

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.