A precondition is a safeguard requiring a condition to hold before an operation proceeds. It protects an object's valid state by failing fast when code would otherwise create invalid data. section 7.3 section 7.2
The condition can enforce a domain invariant, such as preventing a company's employee count from becoming negative. Violations usually indicate bugs rather than ordinary business cases. section 7.3
Use preconditions selectively when deciding what deserves unit-test coverage:
- Test safeguards with domain significance because they express requirements that must remain true. section 7.3
- Avoid spending test effort on incidental checks, such as requiring a factory input array to contain three elements. section 7.3
- Keep required conditions at construction boundaries when they maintain encapsulation, including private constructors used by persistence mechanisms. section 11.1
Preventing an invalid employee count
Company.ChangeNumberOfEmployees requires NumberOfEmployees + delta >= 0 before updating the property. A negative result throws immediately instead of allowing invalid state to spread or reach the database. section 7.3
