liveBook · Manning

Missing value

As explained in The Art of Code

In Java, null is the literal value denoting that a reference does not refer to an object.

A missing value is easy to model badly: Java’s null can mean that a reference leads nowhere, but that possibility often spreads defensive checks through otherwise simple business logic. The real concern—such as choosing a discounted price or a base price—can disappear beneath repeated checks and a delayed NullPointerException risk. section 1.2

Listing 1.1 Getting the final price of a product
public BigDecimal getFinalPrice(Product product) {
  if (product != null                               #1
     && product.pricingDetails() != null) {         #1
    if (product.pricingDetails()                    #1
       .discountedPrice() != null) {                #1
      return product.pricingDetails()        #2
         .discountedPrice().amount();        #2
    } else if (product.pricingDetails()      #3
       .basePrice() != null) {               #3
      return product.pricingDetails()    #4
         .basePrice().amount();          #4
    } #4
  }
  return null;     #5
}

This direct traversal protects every nullable link, yet repeats the same paths and returns null when no price is available, passing absence back to the caller. section 1.2

Model absence explicitly when it is part of a method’s result. Java 8 Optional can contain either a value or nothing, so an Optional<BigDecimal> tells callers that a price may be unavailable. That is clearer than an unqualified null return, although merely wrapping imperative null checks does not remove their control-flow noise. section 1.2.1

Listing 1.3 Getting the price of a product using a functional approach
public Optional<BigDecimal> getFinalPrice(Product product) {
  return Optional.ofNullable(product)     #1
      .map(Product::pricingDetails)     #2
      .map(this::selectPrice)     #3
      .map(Price::amount);    #4
}

private Price selectPrice(PricingDetails details) {
  return Objects.requireNonNullElse(     #5
      details.discountedPrice(),         #5
      details.basePrice());              #5
}

Optional.ofNullable starts a nullable traversal; each map transforms only a present value and leaves an empty Optional unchanged. A separate selection step expresses the discounted-price fallback. section 1.2.1

Choose the representation that keeps absence visible without making it dominate the code:

  • Use nullable types and null-safe access where the language encodes nullability, such as Kotlin’s ? type, ?. safe call, and ?: fallback. section 1.2
  • Use a Null Object when a safe default object expresses the domain better than an absent reference. section 1.2.1
  • Use Optional in Java when a result may be absent; chain map operations to traverse safely and choose orElseThrow when absence is exceptional. section 1.2.1

Read the business rule, not the plumbing

For product pricing, first navigate from Product to PricingDetails, then select discountedPrice when present or basePrice otherwise, then obtain the amount. Modeling the potentially absent traversal with Optional lets that sequence remain visible; an empty result can be handled with ifPresent or turned into a MissingPriceException. section 1.2.1

How The Art of Code covers it

How deep it goes
Works through defensive checks, Optional returns, map-based traversal, and exceptional absence without detailing Optional internals.
Examples
Uses user address-to-city retrieval and product discounted-versus-base price lookup as nullable object chains.
What it assumes
Assumes familiarity with Java object access while introducing Optional, method references, and null-safe traversal patterns.
What you can do after
Apply it via Java Optional to make absent results explicit and traverse nullable object chains with map.
How it connects
Connects null handling with Optional, Null Object, nullable types, safe-call operators, and NullPointerException.

Also explained in

Compare how 35 books explain missing value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.