4 Writing Lean code
The chapter opens by arguing that lean coding is about avoiding waste before it enters the system. It frames software work as a series of judgments about what to build, what to leave out, and how to keep code easy to understand, change, or delete. In an AI-heavy workflow, this becomes even more important because generative tools can rapidly produce large, plausible solutions to the wrong problem. The chapter’s central message is that good engineering depends less on cleverness and more on making intent explicit, constraining solutions early, and treating “done” as something that should be defined before implementation begins.
A major theme is the shifting source of truth in software development. The chapter traces how authority moved from code in waterfall-style development, to tests in TDD, to readable business scenarios in BDD, and now to explicit specifications in spec-driven development. Each shift aimed to reduce a different kind of waste, but each also introduced trade-offs such as waiting, motion, inventory, or overprocessing. The chapter presents specs as a leaner way to define correctness up front: they constrain AI and human implementation alike, reduce ambiguity, and make it easier to tell when a solution has drifted from intent. Rather than adding more documentation for its own sake, the goal is to define just enough to guide implementation without creating dead inventory.
The later sections focus on designing for deletion and managing dependencies with care. Code is treated like inventory that carries maintenance, cognitive, security, and obsolescence costs, so software should be built with reversibility in mind. The chapter distinguishes between short-lived experimental code and foundational system code, arguing that each deserves a different level of structure. It also warns that dependencies and AI can both quietly expand surface area, introduce hidden complexity, and fragment patterns across a codebase. To counter this, the chapter recommends standard work: document team patterns, use specs that set clear boundaries, review code against objective criteria, and delete anything that does not serve the spec. The result is code that stays smaller, safer, and easier to control over time.
A classic, sequential, waterfall way where authority ultimately resides in the code, often revealing defects and overproduction only after significant investment.
A TDD loop where tests become the source of truth and code is written to satisfy those predefined expectations.
AI-driven development expands the technical implementation surface area to produce reasonable-looking solutions.
Comparison of how deletion can be approached as a design decision.
Importing an entire library with multiple capabilities to service one business need.
Without defined standards, each AI-generated feature adopts different patterns, creating a maintenance burden and technical debt.
Using our standard work as a guide for PR reviews.
Summary
- Flow protection starts before code is written: Specs stop building the wrong thing. Standard work stops pattern fragmentation. Reviews stop non-compliance. Each is an Andon cord that catches defects before they compound.
- Specs constrain AI from plausibility to necessity: Write what must be true before generating what makes it true. Include "Never" statements. A 30-second spec prevents overproduction. Specs improve through iteration.
- Code and dependencies are inventory: Every addition creates maintenance costs and potentially expands the system's security and cognitive surface area. Design for deletion through isolation, explicit dependencies, and reversible decisions. Evaluate dependencies against user, business, and technical value, considering their long-term costs before introducing them. Where a small, auditable implementation meets the requirement, avoid importing unnecessary functionality and its transitive dependencies.
- AI is a dependency with a cognitive attack surface: Non-deterministic outputs fragment patterns. One casual suggestion created all 8 TIMWOODS waste types. Constrain with specs and standards.
- Standard work creates determinism: Document the current best way. Code review is Gemba: observe reality. Improve standards when patterns emerge. Standards aren't rules; they're an ongoing conversation.
- Reviews protect flow at the gate: Review against standards, waste, and value. Spec compliance blocks merges. Standards violations block merges. Overproduction blocks merges. Style preferences suggest only. Objective criteria, not debates. Stopping a PR is cheap. Fixing it in production is expensive.
- Knowing what NOT to build matters more than knowing how: AI makes adding code trivial. Your job: prevent unnecessary code from ever being written.
Build High Value Software Systems ebook for free