liveBook · Manning

Postcondition

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

A state that is expected to hold when a use case or function finishes or returns.

A postcondition is a Boolean condition that must be true when a function returns or a use case finishes. In programming by contract, callers establish preconditions; the operation is responsible for meeting its postcondition before returning. section 6.3.3 section 3.7.2

Use postconditions to make successful completion explicit:

  • For a CircularBuffer add(), a successful return means the value was added and the buffer is not empty; count must be greater than zero. section 6.3.3
  • For remove(), a successful return means a value was removed and the buffer is not full; count must be less than capacity. section 6.3.3
  • A failed postcondition may abort during an example, but an application can throw an exception; the contract then becomes success or exception. section 6.3.3

Shipping days

Shipment.calculate_days() promises days between 0 and 14. Expedited narrows that promise to 1 or 2 days, while International requires more than 12 days. Subclass postconditions must remain compatible with the superclass promise, or callers using Shipment can receive an invalid result. section 7.5

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

How deep it goes
Explains Boolean conditions, checked CircularBuffer invariants, exceptions, and subclass compatibility; it stops short of general verification techniques.
Examples
Uses CircularBuffer operations, Shipment subclasses, shipping-day ranges, and a catalogue-search use case.
What it assumes
Assumes basic functions and use cases, while introducing contract terminology alongside preconditions and postconditions.
What you can do after
Evaluate whether functions and use cases leave their promised state, and diagnose incompatible subclass contracts.
How it connects
Connects postconditions to programming by contract, preconditions, use cases, inheritance, and subclass method overriding.

Also explained in

Compare how 2 books explain postcondition →

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.