Software design can mirror business law through formal, verifiable contracts
In short: In software engineering, a "client" and a "supplier" don't just exchange data; they enter into binding agreements. This approach, known as Design by Contract, moves beyond simple testing by defining precise obligations that every component must meet to ensure total system correctness.
Coined by Bertrand Meyer during the development of the Eiffel programming language, Design by Contract (DbC) treats software components as parties in a legalistic arrangement. Instead of relying solely on external testing, DbC integrates formal specifications directly into the design process. This involves three core elements: preconditions, which are obligations the client must meet before calling a method; postconditions, which are guarantees the supplier must fulfill upon completion; and invariants, which are properties that must remain true throughout the execution.
This methodology relies on a specific metaphor: a client is entitled to a benefit (like a return value) only if they fulfill their obligation (like providing valid input). This differs from defensive programming; while defensive programming requires the supplier to handle every possible error, DbC assumes that in a bug-free program, contract conditions are never violated. This allows for a 'fail hard' philosophy, where errors are caught immediately during development, simplifying the debugging process.
In complex environments like distributed computing, where assumptions are risky, the approach is inverted: the server explicitly tests preconditions and returns error messages if they are unmet. To maintain performance, these contract checks—often implemented via assertions—are typically active during development and 'debug mode' but are compiled away in production release modes to eliminate run-time costs.
While DbC does not replace unit or integration testing, it complements them by providing internal self-tests and serving as a form of automatic, precise documentation for how modules collaborate.
Opens the Intellect+ feed with this card first. Swipe up for the next one.
Sources
- Summary by Intellect · source: Design by contract
To cite this card, link to https://intellectplus.co.uk/app/technology-design-by-contract.
How we source, check and correct cards: Editorial standards.
General information, not professional advice. Spotted an error? Flag the card or see corrections and complaints.
Intellect Plus is a visual educational discovery platform for exploring fascinating facts and ideas. Brain Food, Not Brain Rot. What is Intellect Plus?
Related
More in Inventions · More in Technology · Latest · A–Z · All topics