| Design By Contract |
Article Index for Design |
Website Links For Design |
Information AboutDesign By Contract |
| CATEGORIES ABOUT DESIGN BY CONTRACT | |
| programming language topics | |
|
Because "Design by Contract" is a trademark of Eiffel Software, the maintainers of Eiffel , many developers will refer to it only as programming by contract or '''contract-first development'''. HISTORY The term was coined by . Around this time, Eiffel Software trademarked the term Design By Contract. Design by contract has its root in work on Formal Verification , Formal Specification and Hoare Logic . The original contributions include:
DESCRIPTION The central idea of DBC is a metaphor on how elements of a software system collaborate with each other, on the basis of mutual ''obligations'' and ''benefits''. The metaphor comes from business life, where a "client" and a "supplier" agree on a "contract" which defines for example that:
Similarly, if a routine from a Class in Object-oriented Programming provides a certain functionality, it may:
The contract is the formalization of these obligations and benefits. One could summarize design by contract by the "three questions" that the designer must repeatedly ask:
Many Languages have facilities to make Assertion s like these. However, DBC is novel in recognizing that these contracts are so crucial to software correctness that they should be part of the design process. In effect, DBC advocates writing the assertions first. The notion of a contract extends down to the method/procedure level; the contract for each method will normally contain the following pieces of information:
Using the DBC methodology, the program code itself must never try to verify the contract conditions; the whole idea is that code should "fail hard", with the contract verification being the safety net. DBC's "fail hard" property makes debugging for-contract behavior much easier because the intended behaviour of each routine is clearly specified. The contract conditions should never be violated in program execution: thus, they can be either left in as debugging code, or removed from the code altogether for performance reasons. All class relationships are between Client classes and Supplier classes. A Client class is obliged to make calls to Supplier features where the resulting state of the Supplier is not violated by the Client call. Subsequently, the Supplier is obliged to provide a return state and data that does not violate the state requirements of the Client. For instance, a Supplier data buffer may require that data is present in the buffer when a delete feature is called. Subsequently, the Supplier guarantees to the client that when a delete feature finishes its work, the data item will, indeed, be deleted from the buffer. Other Design Contracts are concepts of "Class Invariant". The Class Invariant guarantees (for the local class) that the state of the class will be maintained within specified tolerances at the end of each feature execution. Unit Test ing tests a module in isolation, to check that it meets its contract assuming its subcontractors meet theirs. Integration Testing checks whether the various modules are working properly together. Design by contract also fosters code reuse, since the contract for each piece of code is fully documented. The contracts for a module can also be regarded as a form of Software Documentation for the behavior of that module. NON-TECHNICAL ANALOGY A process in which a number of objects (people or software components, for example) interact to satisfy a goal is called a Collaboration . When two objects collaborate together, one (the client) requests the services of the other (the supplier). The supplier in turn may request the sevices of other objects, and in those collaborations it is the client and they are the suppliers. The process only works correctly if all these individual collaborations work correctly. In a very real sense, the chain is only as strong as its weakest link. Take the process of going on holiday, for example. Bertrand wants to spend two weeks in Florida. He books the holiday through DBC Holidays Inc., who specialise in U.S. package holidays. When he makes the booking (collaboration #1), Bertrand is the client and DBC Holidays are the supplier. DBC Holidays then arrange flights through Assertair Corp. (collaboration #2), and book a room at the Precondition Plaza Hotel in Miami (collaboration #3). In collaboration #2, DBC Holidays are the client and Assertair is the supplier, and in collaboration #3, the hotel is the supplier. And the chain of collaborations goes deeper and deeper (e.g., who does Assertair pay to service their jets?) If any link in this chain of collaborations breaks, then the result could be that Bertrand's holiday is ruined. It's vital, therefore, that every player in the collaboration does what they're supposed to do. In any collaboration, client and supplier have certain obligations. These obligations (or "responsibilities", if you like) fall into three distinct types: #Things that the supplier promises to do as part of the service it offers to the client (e.g., Assertair promises DBC Holidays that Bertrand will be in Miami at a certain date and time) #Things that the client promises to do before using the service (e.g., DBC Holidays must ensure that Bertrand has his passport and tickets when he checks in for his flight) #Things that the supplier promises will always be true no matter what happens (e.g., The airline will always have adequate insurance to cover any accident) Things that the supplier promises to do as part of the service are described as a special kind of rule called a "). If Bertrand turns up at the check-in desk without his passport, of course, then the airline can't live up to its side of the contract: he will not be allowed to board the plane without it. A rule that the client must satisfy before using a service is called a Precondition . A rule that states what must always be true is called an Invariant . If the airline doesn't have adequate insurance then nobody is going anywhere! Design By Contract is a discipline for building software such that the collaborations between objects are correct. A formula for correctness when a client uses the services of a supplier is given as: If the invariant AND precondition are true before using the service, then the invariant AND the postcondition will be true after the service has been completed. In DBC, the responsibilities are clear: the client must satisfy the precondition. This distinguishes it markedly from a related practice known as Defensive Programming , where the supplier is responsible for figuring out what to do when a precondition is broken. More often than not, the supplier throws an exception to inform the client that the precondition has been broken, and in both cases - DBC and defensive programming - the client must figure out how to respond to that. DBC makes the supplier's job easier. LANGUAGE SUPPORT Languages with native support Languages that implement most DBC features natively include:
Languages with third-party support Various libraries, preprocessors and other tools have been developed for existing programming languages without native design by contract support:
Generic tools
SEE ALSO
BIBLIOGRAPHY
EXTERNAL LINKS
|
|
|