coding-by-contract
English | 中文
A few statements carry everything. Anyone should understand a whole system's design philosophy
from a handful of contracts. Anyone should grasp a paper's point, novelty and insight from a few
sentences. An agent should ask the question that goes straight to the core. An experiment's result
should be read straight to its insight. Write the few down, hold everything else to them, and change
them only on purpose.
coding-by-contract is this philosophy practiced in coding.
A contract here is a handful of statements, not a document: the few rules a codebase must honor, not
Meyer's pre/postconditions and not an API spec.
The philosophy: the load-bearing few
Most of any large thing is derivable. A long project's thousand choices reduce to a handful of rules.
A thirty-page paper reduces to three sentences. A wall of numbers reduces to one insight. A long
conversation reduces to the one question that mattered. The contract is that irreducible few: the
statements from which the rest can be regenerated, and against which anything new is judged.
A contract is good when it is:
- Few. Three to eight statements. Needing more means you are writing a spec, not a contract.
- Load-bearing. Remove one and something real breaks or turns ambiguous.
- Plain. A reader in good faith makes the right call from the plain reading.
- Held. Everything new (a change, a claim, a result, a question) answers to it.
- Changed on purpose. Editing the contract is a visible, deliberate act, never a side effect.
The same thinking, in different arenas:
| Thing | Its contract | It is held when |
|---|
| A system | the few design rules everything else reduces to | every change is judged against the rules, not the ticket |
| A paper | a few sentences: the point, the novelty, the insight | a reader with only those sentences judges its relevance correctly |
| An experiment | the one reading that goes straight to the insight | a different result would have changed that reading |
| A question | the one question that goes straight to the core | the answer changes what you do next |
| A decision | the one reason it was made | a future re-proposal is stopped by citing it |
The practice: coding by contract
In a codebase, the load-bearing few become four habits:
- The rules are written, named, and few. Each states the claim, why, where it lives in code, and
what pins it (a test, a fixture, a check).
- Every change is adjudicated against the rules, not the ticket. A report names a symptom on one
path. The fix is the violated rule, fixed once where all callers route through.
- The contract is the single source of truth, executable where possible. Prose (
CONTRACT.md)
plus a conformance corpus (fixtures or tests). Change the behavior, change the fixture, let every other
implementation go red, port it. Silent divergence is the one forbidden thing.
- Contract changes are proposals, never side effects. Adding or altering a rule is a visible edit
to
CONTRACT.md and its fixture, in the same PR, approved by a human.
Rules that have carried real systems, one line each:
- Single source of truth. One authoritative implementation. Every mirror conforms to it and is
regenerated from it, never hand-edited.
- Function-and-lean. Everything present does a job, and nothing is there beyond its job: no
scaffolding for later, no field, layer, or sentence that does not earn its place.
- Backward-compatible. Changes are additive and toggle-gated. Released clients keep working
byte-identical.
- Real data vs auxiliary data. Real user data must persist and keep working. Auxiliary or derived
data may evolve or be re-derived, and a UI/UX compromise there is acceptable to bear.
A document is read once. A skill is invoked on every change. That is why this is a skill.
What the skill does
/coding-by-contract [init [path] | check [diff|PR|description] | ask <topic> | amend <rule change> | scaffold <name>]
| Action | What it does |
|---|
init [path] | Distill the project or subsystem into CONTRACT.md: the rules (claim · why · lives-in · pinned-by) and the change recipe. Proposes, you approve. |
check | Hold a change to the contract: which rules it touches, whether one is violated, whether the fix is root cause (the shared rule) or symptom (one caller). Runs the pins that are runnable. |
ask <topic> | Produce the question that goes straight to the core, and answer it from the real code by citing rules. |
amend <rule change> | Propose a contract edit: prose and pin together, shown as a diff, applied on approval. |
scaffold <name> | Generate an in-repo guard skill for one subsystem (below). |
Two ways to use it
Directly, on any repo: init once, then check every change and amend on purpose.