Make wrong code impossible: structure, deep modules, contracts
A 180-line function, a rounding helper imported from eleven files, an optional discount that can be -15. None of these agents made a syntax error.
A 180-line function with six levels of nesting. A pricing/internal/rounding.ts imported directly from eleven files across three domains. An optional discount?: number that one agent defaults to 0, another sets to undefined, and a partner API sends as -15. None of these agents made a syntax error. The system simply allowed it.
Telling agents “be careful” doesn’t scale. Making the wrong code fail to lint, fail to import, or fail to compile does. Three layers: control flow, module boundaries, types.
Readable control flow (Dijkstra)
In 1968, Edsger Dijkstra’s “Go To Statement Considered Harmful” (Communications of the ACM) argued that we can only reason about a program when its text mirrors its execution. Structured programming followed: sequence, selection, iteration; one entry, one exit; read top to bottom.
Agents have no instinct for this. Every edge case becomes one more if, nested where they found it. And twisted code degrades every later change, because the next agent has to hold the whole flow in context.
“Write short functions” is forgotten by turn three. A failing linter isn’t:
- TypeScript / ESLint:
complexity,max-depth,max-lines-per-function. - Rust / Clippy:
clippy::cognitive_complexity,clippy::too_many_lines,clippy::too_many_arguments.
Set them to deny (error in ESLint), not warn. Start a little strict; allow local, justified exceptions visible in review.
Narrow interfaces (Parnas, Ousterhout)
In 1972, David Parnas (“On the Criteria To Be Used in Decomposing Systems into Modules”) said to split systems by design decisions likely to change, each hidden behind an interface. In 2018, John Ousterhout’s A Philosophy of Software Design added the deep module: a narrow interface over a rich implementation.
An agent can’t read the whole repo; it relies on imports, signatures, exported names. For an agent, every export is an invitation to couple. A narrow interface is also context management: less to load, less to break.
Make boundaries mechanical:
- TypeScript: dependency-cruiser for import rules (no reaching into another domain’s
internal), one entry point per module with minimal exports, knip to find unused exports, files, and dependencies. - Rust:
pub(crate)by default; one crate per domain, so the compiler enforces the boundary.
The test: can you describe a module in two sentences from its exports alone? If the export list looks like the file list, it hides nothing.
Contracts in the types (Hoare, Meyer)
In 1969, C. A. R. Hoare’s “An Axiomatic Basis for Computer Programming” introduced preconditions and postconditions. In 1986, Bertrand Meyer formalized Design by Contract in Eiffel: requires, ensures, invariants. The contract says who guarantees what.
An agent doesn’t “know” a discount must be positive. Move the contract where it can’t be ignored:
- Make invalid states unrepresentable.
draftorpaidwith apaidAtis two variants, not a boolean plus an optional date. - Parse at the boundaries. HTTP, files, LLM output, database: parse once into types that carry their guarantees. No scattered defensive checks inside.
- Fail explicitly with a typed error, never a silent default.
TypeScript: strict, noUncheckedIndexedAccess, exactOptionalPropertyTypes; discriminated unions ({ status: "draft" } | { status: "paid"; paidAt: Date }); branded types (UserId, PositiveAmount); zod or valibot at the boundaries.
Rust: newtypes (struct OrderId(Uuid)) with validating constructors; enums with data per variant; typestate (Order<Draft>, Order<Paid>); deny clippy::unwrap_used and clippy::expect_used outside tests; forbid(unsafe_code) except documented exceptions.
The compiler becomes a tireless reviewer that knows the contract — and fixing precise compile errors is something agents are excellent at.
Takeaways
- Dijkstra (1968): complexity lints set to deny — ESLint
complexity,max-depth,max-lines-per-function; Clippycognitive_complexity,too_many_lines,too_many_arguments. - Parnas (1972), Ousterhout (2018): deep modules; dependency-cruiser, minimal exports, knip;
pub(crate)and crates per domain. - Hoare (1969), Meyer (1986): invalid states unrepresentable, parse at the boundaries, strict compiler settings.
- Structure beats instructions: an error blocks every agent; advice blocks none.
Share this post