liveBook · Manning

Precondition

As explained in Simple Object-Oriented Design: Create clean, maintainable applications

A precondition is a requirement that must hold before an operation can execute correctly.

A precondition is the minimum requirement an operation needs to execute correctly. Make valid inputs and the consequences of invalid inputs explicit to prevent inconsistent execution and failures that surface far from their cause. section 3.2.1 section 3.2

For an operation such as addEmployee, accepting a null employee is a design decision, not an accident. Rejecting it with an exception stops execution when no safe next step is known, but requires clients to handle failure. Alternatively, handle null as an accepted case and return early; this reduces client work but makes the operation more tolerant of bad data. section 3.2.1

Keep preconditions distinct from business validation rules:

  • Use preconditions for code-level minimums, such as a required non-null attribute or a value restricted to A, B, or C. section 3.2.2
  • Use validation rules for business constraints, such as limiting how often an employee may take a course. section 3.2.2
  • Let services control flows that require validation and consistency checks before an action; let entities perform the checks they can. section 3.2.2

Choosing a null policy

Before adding an employee to a course, decide whether null violates the operation’s contract. Throw an exception when continuing would be unsafe; return without action when null is intentionally supported. State that choice clearly so callers do not have to infer it from later crashes. section 3.2.1

Figure 3.3 Validation components help maintain consistency.

Use the diagram to see how services, validation components, and entities divide responsibility for consistency checks before an action. section 3.2.2

How Simple Object-Oriented Design: Create clean, maintainable applications covers it

How deep it goes
Explains invalid-input choices and distinguishes preconditions from business validation rules without detailing implementation mechanics.
Examples
Uses adding an employee to a training course and handling a null employee input.
What it assumes
Assumes familiarity with methods, clients, classes, services, and entities while defining preconditions from zero.
What you can do after
Evaluate/understand whether invalid input should halt execution or be safely tolerated by an operation.
How it connects
Connects preconditions with postconditions, validation rules, service classes, entities, and the Specification pattern.

Also explained in

Compare how 8 books explain precondition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Simple Object-Oriented Design: Create clean, maintainable applications. The section links point to the parts of the book it draws on.