liveBook · Manning

Defensive programming

As explained in Data-Oriented Programming in Java

Defensive programming is a programming technique that anticipates invalid conditions and adds checks or safeguards to prevent their harmful effects.

Defensive programming adds checks around data whose representation permits invalid combinations. It can prevent immediate failures, but widespread defensive code often signals that the model has lost the meaning of its data. section 4.1.4 section 4.9

A common defensive pattern appears when a record combines status attributes with nullable details:

  • Validate completed records by requiring both the completing user and completion time. section 4.1.4
  • Reject incomplete records that nevertheless contain completion details. section 4.1.4
  • Repeat these assumptions wherever the record is consumed, or risk null-pointer failures and mistaken interpretations. section 4.1.6

Guarding a nullable Status representation

A Status record may contain an instance, a step, a completion flag, a completedBy user, and a completedOn time. Constructor checks can reject a completed status without both details, or an incomplete status with either detail populated. This patches the immediate invalid states, but each later method still has to determine which fields are safe to read. Adding states such as STARTED or BLOCKED increases the validation burden and the chance of error. section 4.1.4 section 4.1.6

Listing 4.17 Guarding against missing (or not missing!) attributes during construction
record Status(
  Instance instance,     
  Step step,              
  boolean isCompleted,
  User completedBy, 
  Instant completedOn
) {
  Status {  
    if (isCompleted && (completedBy == null || completedOn == null)) {
      throw new IllegalArgumentException(                    #A
          "completedBy and completedOn cannot be null " +    #A
          "when isCompleted is true"                         #A
      );                                                     #A
    }
    if (!isCompleted && (completedBy != null || completedOn != null ) {
      throw new IllegalArgumentException(                     #B
         "completedBy and completedOn cannot be populated " + #B
          "when isCompleted is false"                         #B
      ); 
    }
  }
}

Inspect the constructor guard that rejects missing details for completed statuses and populated details for incomplete statuses. section 4.1.4

How Data-Oriented Programming in Java covers it

How deep it goes
Works through constructor guards and use-site hazards, but stops short of replacing the flawed representation.
Examples
Demonstrates nullable completion fields, constructor validation, and growing conditionals for additional statuses.
What it assumes
Assumes familiarity with Java records, nullable fields, constructors, and conditional validation.
What you can do after
Diagnose defensive code as evidence that a data model may permit illegal states or obscure domain meaning.
How it connects
Connects defensive programming with data-oriented design, domain modeling, records, null pointer exceptions, and algebraic data types.

Also explained in

Compare how 3 books explain defensive programming →

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.