liveBook · Manning

Precondition

As explained in Object-Oriented Software Design in C++

A design principle stating that a function caller must establish the function's precondition before invoking it.

A precondition is a Boolean condition that must be true before an operation begins. The caller must check it; the operation must behave properly when it is satisfied. section 6.3.2

Apply the idea by separating responsibility from guaranteed behavior:

  • Expose a way to check a member function’s precondition, such as checking a buffer’s count before adding or removing an item. section 6.3.2
  • Treat a false precondition as a call made outside the function’s obligation to behave conveniently for the caller. section 6.3.2
  • Preserve substitutability in subclasses: an overriding function must not require a stronger precondition than its superclass function. section 7.5

Shipping cost

A Shipment accepts a cost greater than 0, while Expedited requires a cost greater than 5. Calling set_cost(2.00) through a Shipment pointer satisfies the superclass contract but violates the subclass contract, exposing a flawed design. Use-case descriptions apply the same idea outside code by recording what must already be true before an action starts. section 7.5 section 3.7.2

How Object-Oriented Software Design in C++ covers it

How deep it goes
Defines Boolean conditions, demonstrates checks in CircularBuffer, and examines inheritance constraints without developing formal contract machinery.
Examples
Demonstrates buffer capacity checks, Shipment cost validation, polymorphic shipping tests, and Search Catalogue use-case descriptions.
What you can do after
Evaluate function and subclass contracts by identifying caller checks and detecting strengthened overriding preconditions.
How it connects
Connects preconditions to programming by contract, postconditions, subclassing, polymorphism, and use cases.

Also explained in

Compare how 8 books explain precondition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.