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


