liveBook · Manning

Optionality

As explained in Data-Oriented Programming in Java

The property of domain information that may be present or absent and whose absence has explicit meaning.

Optionality means that domain information may be present or absent, and that absence carries an explicit meaning. A nullable field hides this distinction; a deliberate model makes it visible and reviewable. section 2.6.3

Make optionality explicit rather than letting null silently bypass the model:

  • Use a type representing either “something” or “nothing”; algebraically, the empty case has one value and the present case has the values of type A. section 4.6.4
  • Treat fields that must always exist as invariants and reject nulls during construction when explicit protection is useful. section 2.6.3 section 9.3.1
  • Refactor legacy models by identifying hidden optionality and encoding only the cases the domain actually permits. section 9.3.1

An optional seller identifier

A request can distinguish a missing seller identifier from an invalid required product identifier: `@NotBlank @NotNull String productId; Optional<String> sellerId;`. The type communicates that seller information is allowed to be absent, while validation keeps product identity mandatory. section 9.3.1

Combining optional values

Optional-aware operations can lift an ordinary binary operator into the Optional type. The same pattern combines optional policies, audit findings, and Boolean statuses while preserving the absent case. section 7.8.3

Figure 4.16 Sophisticated ideas can like optionality can be encoded as Product and Sum Types

See optionality represented directly through product and sum types. section 4.6.4

How Data-Oriented Programming in Java covers it

How deep it goes
Develops optionality from null avoidance through algebraic representation and lifted operations, while deferring full correctness proofs as exercises.
Examples
Uses nullable Person records, Optional algebra, optional policy aggregation, and a cart request with an optional seller identifier.
What it assumes
Assumes intermediate Java and introduces algebraic reasoning gradually through values, records, constructors, and Optional.
What you can do after
Build explicit optional representations and refactor legacy fields by separating permitted absence from required invariants.
How it connects
Connects optionality to nulls, records, invariants, algebraic data types, product types, sum types, and Optional.

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.