Overview

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 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.
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.
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.
Comparison of how deletion can be approached as a design decision.
Importing an entire library with multiple capabilities to service one business need.
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.
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.
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.

FAQ

What is the main goal of writing lean code in this chapter?The goal is to prevent waste before it enters the system by writing code that is easy to understand, change, or delete. Lean code does exactly what is needed, and no more.
Why does the chapter emphasize clarifying intent before implementation?Because vague intent leads to rework, overproduction, and plausible-looking solutions to the wrong problem. Making intent explicit helps define “done” before code is written.
How does the source of truth affect software development?The source of truth determines what teams trust to decide whether something is correct and finished. Depending on whether the truth is code, tests, docs, contracts, or specs, different kinds of waste show up in the system.
What problem does Spec Driven Development solve?Spec Driven Development helps constrain AI and human implementation by defining requirements before code is written. It reduces ambiguity, overproduction, and hidden assumptions by making the specification the quality gate.
How is lean code different from simply writing less code?Lean code is not about minimizing code volume for its own sake. It is about minimizing lifetime waste, ensuring the code is valuable, maintainable, and easy to remove when it is no longer useful.
Why is deletability considered a design skill?Because most code is temporary, even if it feels permanent at the time. Designing for deletion makes it safer and cheaper to remove or replace code when requirements change.
What hidden costs do dependencies introduce?Dependencies add versioning, security, transitive packages, upgrade risk, and a behavioral contract you do not fully control. They can increase maintenance and cognitive load even when they save time initially.
How should teams evaluate whether to add a dependency?They should ask whether it provides user, business, or technical value, and whether that value outweighs the surface area cost. If the functionality can be written simply and safely in a small amount of code, a dependency may not be worth it.
Why is AI treated like a dependency in this chapter?Because AI shapes code, architecture, and patterns even though it is not a traditional library. It can increase cognitive debt, introduce inconsistent styles, and generate code that looks reasonable but is hard to understand or maintain.
What role does standard work play in lean coding?Standard work gives the team a shared way to make decisions about specs, dependencies, error handling, logging, validation, and reviews. It reduces variation, makes AI outputs more consistent, and helps reviewers spot waste or deviation quickly.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build High Value Software Systems Build High Value Software Systems ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build High Value Software Systems Build High Value Software Systems ebook for free