Heron-Centric: Ruminations of a Language Designer Programming with Contracts by Christopher Diggins October 15, 2004
Programming with Contracts is a sofware development technique rapidly gaining in popularity but still somewhat misunderstood. I have been spending a lot of time lately writing about contracts and using them in the Heron standard library.
Programming with Contracts (PwC) is a technique of using contracts in software in order to detect and reduce the number of
defects in software. This is done by explicitly stating design specifications in a manner which can be verified by the
compiler. This is related to but is not the same thing as Design by Contract tm which is a technique for designing software. The difference can be likened to the differences between OOP and OOD, one compliments the other but either one can exist without the other.
The role of a contract is to explicitly express the assumptions, requirements and obligations of a class or function.
A contract is made up of one or more clauses. There are three kinds of clauses:
preconditions are clauses that must evaluate to true before entry of a function
postconditions are clauses that must evalute to true after exit from a function
invariants are clauses that must evalute to true before entry and after exit from every public function in a class
A contract should never be violated in correct software. If a clause is ever violated, there is a defect in your
software (or possibly your contract).
The sticky point for a lot of people is understanding precisely what constitutes a contractual violation. Exceptional conditions
and system failures in of themselves are not bugs but simply error conditions. A bug would occur if the these conditions where
not handled in a manner as specified in the design specification. If no specification exists, whether explicit or assumed, that alone in of itself is a defect of the gravest kind.
For more information on contracts I suggest starting with the following:
Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com.