31. Design by Contract (DbC)
Every component states what it requires of its callers and what it guarantees in return, and fails loudly when either side is broken. A broken requirement is the caller's bug; a broken guarantee is the component's own. Either way, the program stops where the fault is, rather than carrying on and failing somewhere else.
Related: Why MVT · Rules · Bindings · Dependency Inversion Principle (DIP)
31.1 The Principle
Bertrand Meyer named the principle in 1986, for his Eiffel language, and built it on a metaphor from business: a contract between a client and a supplier. Each side has obligations, and each side benefits from the other's.
- A requirement (Meyer's precondition) is what must be true before a call. Meeting it is the caller's obligation. In return, the component doesn't have to handle anything outside it.
- A guarantee (a postcondition) is what will be true after the call. Keeping it is the component's obligation, and it is what the caller gets.
- An invariant is what is always true of the component, between calls.
This chapter takes them in that order, requirements before guarantees, because that is the order they apply in time.
The contract is part of the component, not just documentation. Writing it down before coding means deciding exactly what a component requires and guarantees. Checking it in code turns a broken contract into an immediate, specific failure, at the point where it happened.
31.2 Symptoms
The easy way to handle a call that comes at the wrong time is to ignore it. Suppose the fruit machine's spin() did that:
spin() {
if (phase !== 'idle') return; // returns quietly, without doing anything
// ...decide the result, and start the reels
}The game never crashes. Now try a few ordinary requests:
- Find out why the spin button sometimes doesn't work. A player taps it during the win display, and the game doesn't respond, or report anything. Finding the cause means reading every caller.
- Let a spin during the win display skip it and spin again. Some view calls
spin()on every tap, and depends on extra taps being ignored. Changing the model breaks it, though the model never guaranteed the quiet return. - Grey out the spin button when it won't work. The view has to work out
phase === 'idle'for itself. The rule now lives in two places, and they drift apart when the model's rule changes.
The same pattern across a game shows up as three symptoms:
- Faults surface far from their cause. A call that should have failed carries on, and the state it leaves behind fails later, somewhere else.
- Accidents become guarantees. Callers rely on whatever the code happens to do. Hyrum Wright put this as Hyrum's Law: "With a sufficient number of users of an API, it does not matter what you promise in the contract: all observable behaviors of your system will be depended on by somebody."
- Callers are written from guesses. Without a stated requirement, each caller works out when a call is safe, and each works it out differently.
31.3 Applied to MVT: Contracts Everywhere
31.3.1 Requirements, then guarantees
The real fruit machine states both sides of every action in its interface:
/** True when `spin()` may be called: when idle. */
readonly canSpin: boolean;
/**
* Decide the result, and spin. Resolves with the result once the machine
* is idle again: after the reels land and any celebration ends. ...Needs
* `canSpin`.
*/
spin: (options?: SpinOptions) => Promise<SpinOutcome>;The requirement is canSpin. The guarantee is the rest: decide the result, spin, and resolve when the machine is idle again.
31.3.2 A broken requirement throws
When the requirement isn't met, spin() doesn't return quietly. It throws:
spin() {
assert(model.canSpin, () => `spin() needs canSpin, but the machine is ${phase}`);
// ...
}A call that breaks a requirement is a bug in the caller, and the sooner it fails, the closer the failure is to the bug. A quiet return would also widen the contract for good: by Hyrum's Law, some caller would soon rely on it, and the requirement could never be enforced again.
For this to be fair, the caller has to be able to check the requirement before calling. Meyer made that a rule: a requirement must use only what the caller can read. So every action with a requirement has a matching query that is the requirement, usually named after the action: spin() and canSpin, stop() and canStop. The fruit machine's views check only that query: they spin only if (model.canSpin), and the panel greys out its button with disabled={() => !model.canSpin}. The rule lives in one place, in the model (SSOT).
Some actions don't need a can* query
An action without a requirement doesn't need a query: a query that's always true doesn't tell the caller anything. And where the component already has a query that matches the requirement exactly, it doesn't need a new one. SlotList's insert() requires a free slot, and isFull already says whether there is one. A stop() that requires the reels to be turning could likewise rely on an existing isSpinning. The query's name doesn't matter, as long as one query states the requirement exactly.
The work is in choosing the requirement. A call that doesn't do anything isn't always a broken requirement:
- Meaningless in this state: a requirement.
spin()while the reels turn doesn't have a sensible meaning, so it gets a requirement, acan*query, and a throw. - A game meaning: documented behaviour. Pressing fire while the gun cools down is a real event, and the game's rule is that the press doesn't fire a shot. That is a guarantee, written down on purpose, not a broken requirement. Neon Monsoon's bullet field does this:
fire()"returns false, and fires nothing, when the field is full, so an over-dense pattern thins out rather than failing."
31.3.3 A broken guarantee throws too
A component that can't keep a guarantee throws too, rather than returning something that looks like success. @mvtjs/pixi's texture registry guarantees a texture for every name it receives. If the spritesheet lacks one, it throws, naming the texture and the sheet, rather than returning a placeholder that would show up later as a blank sprite. The same goes for what a component requires when it is built: the fruit machine's model asserts that its first reel doesn't have any wilds, because its win evaluation relies on that.
assert(): a contract you can run
Each check in this chapter is one line, assert(condition, message) from @mvtjs/utils, which throws with the message when the condition is false. It reads as a statement of the contract, and runs as a check of it:
// A guarantee, in the texture registry
assert(texture, () => `Texture '${value}' not found after loading '${spritesheetUrl}'`);
// A requirement on what the fruit machine is built with
assert(!strips[0].includes('wild'), 'The first reel has no wilds: a way takes its picture from it');A message built from values is passed as a function, so it is built only on failure. In TypeScript, assert also narrows types: after assert(texture, ...), the compiler treats texture as defined.
31.3.4 Bindings are contracts
A view's bindings are its contract with the rest of the game (B-contract). Each query binding's type says how strong a requirement it makes:
| Declared as | The view requires | The view guarantees |
|---|---|---|
T | A fixed value | Only that it reads the value once, and doesn't follow changes |
() => T | A function, even for a fixed value | To follow the value as it changes |
T | (() => T) (ValueOrGetter<T>, in @mvtjs/utils) | Either | To follow it, when given a function |
A view that declares () => T but reads it only once has broken its own guarantee (V-reactive). Relay bindings are usually optional (B-optional), which is a view requiring less: it works whether or not the caller supplies it.
31.3.5 Evolving a contract safely
A contract can change without breaking any caller in only two ways:
Require less, or guarantee more.
A caller that met the old requirement still meets a weaker one, and a caller that relied on the old guarantee still gets it. The reverse breaks some caller. Chapter 8, Bindings, shows this for query bindings: widening a value-only binding to accept either form leaves every caller working.
Three habits follow:
- Require strictly at first. A requirement can be loosened later, but never tightened.
- Guarantee little at first. A guarantee can be added later, but never withdrawn, and by Hyrum's Law, anything observable is a guarantee.
- Leave room to grow. An options object can take a new, optional field without breaking a caller; a fixed list of parameters cannot.
Each habit has a drawback. Neon Monsoon's fire() takes positional parameters, not an options object, because it runs thousands of times a second, and a new object on every call would leave thousands of objects a second for the garbage collector. That gives up room to grow, on purpose, for the hot path.
How much these habits matter depends on the work:
- Maintaining a library, such as the published
@mvtjspackages, or views shared by several games. Its callers are in code that its maintainers never see, so a contract changed the wrong way breaks them beyond reach. Here the habits are rules. - Making a game. A game's own models are called only from the same repo, so a broken caller is one search and one edit away. Here the habits are good practice. The contracts themselves matter just as much.
The same rule has two better-known names. Barbara Liskov's substitution principle applies it to replacements rather than versions: a stand-in must require at most what the original requires, and guarantee at least what it guarantees. And Meyer's open-closed principle states that a component should be extendable without being edited, which a contract that only grows makes possible.
31.4 Practical Benefits
- Faults stop where they start. A broken requirement throws in the caller that broke it, with a message that says what was required.
- The interface is the manual. Each action states its requirement and its guarantee, so reading the interface is enough to use it correctly.
- Tests follow from the contract. A contract says what to check: the requirement's throw, and each guarantee. Design for Testability takes this further.
- Change is safe in two ways. A component can grow, by requiring less or guaranteeing more, without a search for every caller it might break.
31.5 Practical Drawbacks
- Choosing requirements takes thought. Each action needs a decision: is a call in this state meaningless, or does it have a game meaning? Get it wrong, and either players hit a throw where they expected the game to ignore the press, or a quiet return hides a bug.
- Checks take time. An
assertis fast, but it runs on every call. On a hot path, a plainifandthrowtakes only the time of the condition. - Throwing stops the game. A broken contract in a released game crashes it, where a quiet return might have let it limp on. The trade is deliberate: the crash happens where the bug is, and gets fixed.
31.6 Common Misreadings
- Not defensive programming. Defensive code checks everything, everywhere, and tries to recover. A contract checks once, at the boundary, and doesn't recover from a caller's bug: it reports it.
- Not "every early return throws". A call with a game meaning that doesn't do anything is documented behaviour, and stays a quiet return.
- Not just types. A type states part of a contract: what kind of value goes in and comes out. The rest (
canSpinmust be true;spin()'sPromiseresolves when idle) lives in the documentation and the asserts. The next chapter, MISU, moves as much as it can into the types.
31.7 Checklist
Questions to ask of a design, or in a review:
- Does each public action state its requirement and its guarantee?
- Does each action with a requirement have a query that is exactly that requirement (usually
can*), and does the action assert it? - Does any action return quietly when called at the wrong time? If so, is that a game rule, documented as such?
- Does any component return something that looks like success when it couldn't keep its guarantee?
- Do callers check that query, rather than repeating the rule behind it?
- Does any change to a contract require more, or guarantee less? Who might it break?
31.8 Rules Built on DbC
| Rule | How it follows |
|---|---|
| B-contract | A view's bindings are its contract: what it requires, and what it guarantees in return |
| B-optional | An optional relay binding is a view requiring less, so it works in more places |
| V-reactive | A query binding declared as a function is a guarantee to follow changes, which reading it once would break |
| T-cap | A model may require small steps of time; the ticker guarantees them, however long the gap since the last frame |
31.9 Further Reading
- Bertrand Meyer, Object-Oriented Software Construction (1988; 2nd ed., 1997). Design by Contract in full, with preconditions, postconditions, invariants, and the open-closed principle.
- Bertrand Meyer, "Applying 'Design by Contract'", IEEE Computer (1992). A shorter introduction.
- Barbara Liskov and Jeannette Wing, "A Behavioral Notion of Subtyping", ACM Transactions on Programming Languages and Systems (1994). The substitution principle, stated in terms of contracts.
- Hyrum Wright, "Hyrum's Law". Why every observable behaviour becomes part of the contract.