Programming by contract
Programming by contract is a software design technique that specifies component obligations through preconditions, postconditions, and invariants.
Object-Oriented Software Design in C++
Programming by contract is a software design technique that specifies preconditions, postconditions, and invariants for program components and may check them during execution.
- How it is used
- Uses explicit contracts to prevent surprising behavior in classes, member functions, and subclass hierarchies.
- How deep it goes
- Explains responsibilities, runtime assertions, a CircularBuffer implementation, and inheritance constraints, but does not provide native C++ contract syntax.
- Examples
- Demonstrates contracts with CircularBuffer operations and Shipment, Expedited, and International subclasses.
- What you can do after
- Apply it by documenting and asserting class contracts, checking caller obligations, and evaluating subclass overrides for compatibility.
Read how Object-Oriented Software Design in C++ explains programming by contract →
Software Design for Python Programmers
Programming by contract is a software development technique that specifies component obligations through preconditions, postconditions, and invariants.
- How it is used
- Uses explicit contracts to prevent surprising runtime behavior in classes and methods.
- How deep it goes
- Defines three contract elements and demonstrates assertion-based implementation, while deferring native Python support and deployment strategy.
- Examples
- Demonstrates a CircularBuffer and Shipment subclasses with cost preconditions, day postconditions, assertions, and custom exceptions.
- What you can do after
- Apply it via Python assertions, predicate methods, and exceptions to specify and enforce critical class behavior.
Read how Software Design for Python Programmers explains programming by contract →
Functional Design and Architecture: Examples in Haskell
A software-design technique that expresses behavioral contracts explicitly and checks implementations against them.
- How it is used
- Uses explicit contracts to make object-oriented subsystem behavior predictable and implementations accountable.
- How deep it goes
- Defines explicit contracts and illustrates a violation, but does not detail a specific contract-checking tool or formal notation.
- Examples
- Uses ILampSwitcher, DaylightLamp, TableLamp, exceptions, global state, absent files, and memory operations as contract cases.
- What you can do after
- Diagnose implementations that violate client-facing behavioral contracts and recognize why substitutions become unsafe.
Read how Functional Design and Architecture: Examples in Haskell explains programming by contract →
What to know first
- polymorphismPolymorphism is a programming-language capability that lets a common operation or interface work with values of different types, with behavior or implementation determined by the type or value.
- code to the interface principleA design principle in which code depends on an interface and polymorphic behavior rather than on a specific implementation or subclass.
- exceptionAn exception is a runtime signal or condition representing an abnormal event that interrupts or alters ordinary program flow and can be handled or propagated.
Related concepts
- C++contrasts with · C++ is a statically typed, general-purpose programming language that supports procedural, object-oriented, generic, and low-level systems programming.
- ring bufferuses · A ring buffer is a fixed-capacity storage structure whose positions are reused cyclically as elements are added and removed.
- postconditionhas part · A postcondition is a condition or property that an operation or computation guarantees will hold when it completes successfully.
- preconditionuses · A precondition is a condition or requirement that must hold before an operation, process, or activity can proceed correctly.
- exceptionhas part · An exception is a runtime signal or condition representing an abnormal event that interrupts or alters ordinary program flow and can be handled or propagated.
- inheritanceused by · Inheritance is a mechanism in which a derived type or descendant element receives properties or behavior from a parent, subject to rules that may allow specialization or overriding.
Questions about programming by contract
What is programming by contract?
Programming by contract is a software design technique that specifies component obligations through preconditions, postconditions, and invariants.
Which Manning books explain programming by contract?
Object-Oriented Software Design in C++, Software Design for Python Programmers and Functional Design and Architecture: Examples in Haskell.
What should I know before learning programming by contract?
Manning books that teach programming by contract usually introduce polymorphism, code to the interface principle, Eiffel and exception first.
What is the difference between programming by contract and C++?
Programming by contract is a software design technique that specifies component obligations through preconditions, postconditions, and invariants. C++ is a statically typed, general-purpose programming language that supports procedural, object-oriented, generic, and low-level systems programming.
What are the parts of programming by contract?
Programming by contract includes postcondition and exception.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


