A constraint states what valid data or program state must look like. It turns domain meaning into a rule: if a sample identifier has a required form, elapsed days cannot be negative, and a contact angle has a permitted range, values outside those conditions do not belong in the model. Constraints are the bridge from “what this data means” to invariants and executable checks. section 3.2.2 section 3.6
Constructor validation is a direct first defense. A record can reject malformed identifiers, negative durations, and out-of-range measurements before an instance exists. That prevents invalid values from entering through ordinary construction, even when callers overlook documentation or names. The limitation is that a validated field stored as a plain double is still just a double elsewhere: assignments, transformations, and method calls can lose its domain meaning. section 3.2.2
Listing 3.13 Defending invariants during construction
record Measurement(
String sampleId,
int daysElapsed,
double contactAngleDegrees) {
Measurement { #A
if (!sampleId.matches("(HEAT|AIR|UV)-\\d+")) { #B
throw new IllegalArgumentException(
"Must honor the form {CuringMethod}-{number}" +
"Where CuringMethod is one of (HEAT, AIR, UV), " +
"and number is any positive integer"
);
}
if (daysElapsed < 0) { #C
throw new IllegalArgumentException(
"Days elapsed cannot be less than 0!");
}
if (!(value >= -180.0 && value < 180.0)) { #C
throw new IllegalArgumentException(
"Contact angle must be between -180 and +180");
}
}
}Use the record listing to see constraints expressed as construction-time guards: identifier format, nonnegative elapsed days, and a bounded contact-angle range. section 3.2.2
Treat constraints as design feedback, not merely defensive checks:
- Ask what an entity must not be as well as what it is; those exclusions expose its invariants. section 3.6
- Question loose fields, especially unconstrained strings and flags, when the domain requires bounded names, unique items, or a particular relationship. section 4.1.3
- Prefer a model that removes a bad state over repeatedly repairing or validating that state after it appears. section 4.1.3 section 3.6
- Put enforcement where concurrent writers cannot bypass it; database transition rules need database enforcement to avoid race conditions. section 12.3
Checklist steps and task lifecycles
A checklist template should not carry completion status if completion belongs to a particular run of that template. Separating Step, Template, Instance, and Status makes the relationship explicit, while uniqueness and acceptable names remain constraints to enforce. For asynchronous tasks, valid transitions are also constraints: an empty state may become any state, written states are immutable, and Completed or Failed cannot transition further. Modeling those transitions as an ordering makes the allowed and forbidden cases testable against the database. section 4.1.3 section 12.3
Figure 12.3 Visualizing the lifecycle as a simple state machine

Use the state-machine figure to distinguish lifecycle rules from ordinary field validation: the constraint governs which next states are allowed. section 12.3
Constraints can live at different layers. Runtime constructor checks defend individual values; type choices can move protection earlier by making illegal states impossible to represent; and dependency-level conditions protect shared state where application code alone is insufficient. The goal is not to replace every object with data structures, but to make the model carry the meanings that correctness depends on. section 3.6 section 12.3




























