liveBook · Manning

Shotgun parsing

As explained in Data-Oriented Programming in Java

Shotgun parsing is a parsing and validation style in which interpretation and checks are scattered across code paths, allowing assumptions and incomplete data to escape their intended boundaries.

Shotgun parsing scatters interpretation and validation across code paths. A partially constructed or incompletely validated object crosses boundaries, while later code quietly assumes fields, invariants, and business meaning that were never guaranteed. section 9.6 section 9.2.4

Typical warning signs include:

  • A request is validated in one layer, converted into a mutable object in another, and interpreted again by downstream operations. section 9.2.4
  • Fields such as sellerId remain nullable even though identity and lookup logic silently requires them. section 9.2.4 section 9.6
  • A safer boundary constructs a type only after its invariants hold, making invalid states difficult or impossible to represent. section 9.6

A cart item with hidden requirements

An AddItemsRequest validates productId and quantity, but sellerId is left optional. Cart converts the request into a mutable CartItem, builds a key by concatenating productId and sellerId, and later updates metadata and inventory. The missing sellerId can therefore travel through several methods before causing incorrect identity or behavior. Move the requirement to construction: accept only data that proves the cart item is valid, then let downstream code rely on that guarantee. section 9.2.4

Listing 9.11 Where most of our problems begin
class Cart {
  private List<CartItem> active; 
  ...
  public void addOrUpdateItems(AddItemsRequest request) {
    List<CartItem> items = this.convertToCartItems(request); #A
    var activeItemsById = indexBy(this.active, this::qualifiedId)
    var savedItemsById = indexBy(this.saved, this::qualifiedId)
    for (CartItem incoming : this.active) {
      // logic elided                    
    }    
    ...
  }  
  private List<CartItem> convertToCartItems(AddItemsRequest request) {
    return request.items().stream().map(external -> {
      CartItem item = new CartItem();  #B
      item.setProductId(external.getProductId());  #C
      item.setSellerId(external.getSellerId());    #C
      item.setQuantity(external.getQuantity());    #C
      return item;                                 #C
    }).toList(); 
  }
}

Trace how external request data becomes a mutable CartItem before its full meaning and invariants are enforced. section 9.2.4

How Data-Oriented Programming in Java covers it

How deep it goes
Traces partial construction, nullable fields, hidden assumptions, and invariant-based prevention through a cart example.
Examples
Uses cart requests, nullable seller identifiers, mutable CartItems, qualified keys, and downstream metadata and inventory updates.
What it assumes
Assumes familiarity with object mutation, request validation, constructors, and invariants.
What you can do after
Diagnose/debug with it by locating scattered checks, surfacing hidden knowledge, and moving guarantees into construction boundaries.
How it connects
Connects partial construction with validation, invariants, nulls, mutation, hidden knowledge, and data types.

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.