Design Contract® is a programming method that involves defining well-defined interface parameters for all parts of a program. It aims to develop a more comprehensive program with fewer errors by using contracts for functions and classes. The method defines preconditions and postconditions, and each method and class in an object-oriented program defines a contract. However, the method can lead to longer design times and slower program execution.
Design Contract® is a method used to develop software that relies on defining well-defined interface parameters, or contracts, for all parts of a program. While Design Contract® can be viewed as a style of conceptual development that is implemented through documentation or modeling, it is more widely used to refer to a native or third-party mechanism within some programming language that actually requires the implicit coding of contracts within a program. The goal in using Design Contract® is for a program to be developed and possibly coded in a much more comprehensive way that removes the most trivial errors, while the application of the contracts by the supporting language makes it easily identifiable and caught any residual errors. A program that uses contracts for functions and classes can have a more modular design where individual classes can be reused easily in other programs due to the clear definition of their contracts.
The basis of the Design Contract® programming philosophy is the idea of real commercial contracts. In this model, each method and class in an object-oriented program defines a contract that any other method or object that interacts with it must abide by. Each class may also have a contract, called a class invariant, that it must follow internally to ensure that external contracts don’t affect its ability to function.
The two parts of a contract that are most important when designing and coding are the preconditions and postconditions. These two parts of the Design Contract® model define the state of the program before a method is called and the state of the program after the method has completed execution. Other parts of a contract may vary by implementation, but may include cross-references to other forms, synchronization conditions, and order of execution requirements.
By developing contracts for each class and method, the interaction of different parts of a program can be easily mapped out and predicted. Executing the contracts, whether through the use of assertions or other mechanisms, also ensures that the program will not attempt to execute if there is a violation of the contracts, because any output produced in that state would be technically invalid anyway. When implemented as a native language feature, Design Contract® can help ensure that a final product performs as intended with little chance of unexpected errors.
Some of the complications that come with using the Design Contract® philosophy include longer design times and more granular development of a system before coding begins, which can sometimes be difficult with a large project. More practically, the contract validation mechanism used in a programming language that natively supports contracts can slow down program execution. A poor understanding of the Design Contract® methodology can lead to a program using contracts to perform basic error checking, potentially leading to an application prone to crashes after release.
Protect your devices with Threat Protection by NordVPN