In the realm of computer programming and computer science, operation contracts are a way to specify or document how a particular function will change different elements within a program. Unlike an abstract programming interface (API) or an interface control document, operation contracts are used to help during the development and modeling phase of creating a computer application or system. At the most basic level, the contract defines four features of the operation, specifically the name of the operation, any references to the operation in other areas of the design, any requirements for input or state before the operation is performed, and the state of the system or variables after the operation has been performed. The contract does not define anything specific about how the operation functions internally, and instead only deals with how the state of a program is affected by its use.
Operation contracts, in general, are not built for every operation within a program model. Instead, they are reserved for operations that are particularly complex or difficult to track. Several computer modeling languages, such as the Unified Modeling Language (UML), support operation contracts and have ways to help visualize how the state of a program can change once the operation has been performed.
The first two definitions required to create operation contracts are the name of the operation, which can be anything, and any cross-references. A cross-reference is a list of other operations or areas of a program model that use the operation being defined or that the operation will use in its processing. This aids in seeing how an overall design interacts and is especially useful in seeing how changes to one area of the model will affect other areas.
Operation contracts next define the preconditions necessary to run the operation. This can involve requiring certain variables to be loaded with appropriate values, or it can require that certain parts of the program are in a particular state. If the preconditions are not met when the operation is executed, then the operation will not take place or might fail completely. The contract is used as an abstract tool, so the preconditions usually are fairly general and more involve the state of the program than specific variables.
The final portion of operation contracts defines any post-conditions. Post-conditions are a list of items within the program model that have been changed because of the execution of the operation. This can specify changes to a data structure or modifications to the state of the program, such as shifting control to a separate module. Through the use of well-defined operation contracts, programs can be modeled and modified effectively before actual implementation begins.