Design by Contract (DbC) WIP
2 Contract?
- Imagine: You are subcontracting out the design and construction of
subsystems (classes) or methods.
- Assigning blame across interfaces
- Details of method responsibilities
2.1 An Example Contract
- A contract can be satisfied in many ways
- a method can have many implementation satisfying the contract;
- Different performances (time, space, etc);
- A contract hides the implementation details
- We can change implementations.
- Caller's code Will work for any implementation that satisfies the
contract
3 Techniques
3.1 Public Methods
- Weakest Pre-conditions
requires
for public methods
- The called method assumes precondition holds
- Avoiding constantly checking arguments
- The caller guarantees precondition holds
- Strongest Post-conditions
ensures
for public methods
- The called method guarantees postcondition
- The caller method is guaranteed postcondition
3.2 Class Invariants
- Consider only the public methods Their entry- exit-
assertions. Rewrite, if nec, using and's (at the top level)
- The common portion across all the above is the class invariant
- Strongest Class invariant
Copyright © 2020
cecs.wright.edu/~pmateti 2020-09-15