We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What Is a Design by Contract®?

By Eugene P.
Updated: May 16, 2024

Design by Contract® is a method used to develop software that relies on the establishment of well-defined interface parameters, or contracts, for all parts of a program. Although Design by Contract® can be viewed as a conceptual development style that is implemented through documentation or modeling, it more widely is used to refer to a native or third-party mechanism within some programming languages that actually requires the implicit coding of the contracts within a program. The goal in using Design by Contract® is that a program will be developed and, eventually, coded in a much more complete fashion that removes more trivial errors, while enforcement of the contracts by the supporting language makes any residual errors easily found and captured. A program that uses contracts for functions and classes can have a more modular design in which individual classes can be reused easily in other programs because of the clear definition of their contracts.

The basis for the Design by Contract® programming philosophy is the idea of real-world business contracts. In this model, each method and class in an object oriented program defines a contract by which any other method or object interacting with it must abide. Each class also can have a contract, called a class invariant, that it must follow internally to ensure that outside contracts do not compromise its ability to function.

The two parts of a contract that are most important during design and coding are the preconditions and the post-conditions. These two parts of the Design by Contract® model define the state of the program before a method is called and the state of the program after the method has completed executing. Other parts of a contract can vary by implementation but might include cross-references to other modules, synchronization conditions and order-of-execution requirements.

By developing contracts for each class and method, the interaction of the different parts of a program can easily be mapped and predicted. The enforcement of the contracts, either through the use of assertions or other mechanisms, also ensures that the program will not attempt to execute if there is a violation of contracts, because any output produced in that state would technically be invalid anyway. When implemented as a native language feature, Design by Contract® can help to ensure that a final product will operate as expected with little chance for unforeseen errors.

Some of the complications that come with use of the Design by 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 by Contract® methodology can lead to a program that uses contracts to perform basic error checking, potentially leading to an application that is prone to sudden crashes after release.

EasyTechJunkie is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.
Discussion Comments
Share
EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.

EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.