Skip to content

Lesson 2 of 14 · 18m

TDD That Earns Its Keep

Red-green-refactor is a design tool with a measured price — 40–90% fewer defects for 15–35% more time — and it earns it on logic, not on exploration.

Write the test first and the test becomes the first consumer of an API that doesn't exist yet. That's the entire mechanism: calculate_tariff(order) gets called before calculate_tariff is written, the test fails for the right reason (red), you write the minimum that passes (green), then you clean up with the safety net running (refactor). Red-green-refactor isn't a ritual. It's a way to make the design answerable to a caller before the design hardens.

def test_tariff_applies_regional_rate():
    order = Order(region="EU", subtotal=100)
    assert calculate_tariff(order) == Money("20.00")   # red: this function doesn't exist

Watch the discipline closely: green means writing return Money("20.00") — the literal constant. Not because you're lazy, but because the second test is what forces the general implementation:

def test_tariff_applies_none_for_exempt_regions():
    order = Order(region="CH", subtotal=100)
    assert calculate_tariff(order) == Money("0.00")

Now return Money("20.00") can't survive — you write the lookup. That's triangulation: each test eliminates a class of wrong implementation, and the code converges on the general case instead of starting there and hoping.

The honest evidence

The best study is still Nagappan, Maximilien, Bhat, and Williams (2008) — four teams at Microsoft and IBM. Defect density dropped 40–90%; development time went up 15–35%. That's the real trade: you pay time now for defects later, and the exchange rate favors the defects. Anyone selling TDD as free is lying; anyone dismissing it as dogma hasn't run the numbers.

DHH's 2014 "TDD is dead" posts were aimed at something narrower: the mock-everything London school that turned test-first into test-indirection — suites where every collaborator was mocked, the tests verified wiring instead of behavior, and refactoring meant rewriting half the mock expectations. The backlash killed a caricature. The Fowler–Beck–DHH hangouts that followed mostly agreed: test-first is a tool for shaping design, not a moral position, and hexagonal/isolation-heavy TDD is where it goes wrong. The classicist version — write the test, use real collaborators where cheap, refactor under green — survived fine.

Beck's own formulation in Test-Driven Development: By Example is stricter than the ritual most teams run: no production code except to make a failing test pass, and no test beyond what's needed to fail. The strictness is the point — it caps how much unverified code can exist at once. Run honestly, the cycle keeps the distance between "code written" and "code verified" under about ten minutes, which is the safety net the refactor step stands on.

Where it earns

  • Pure logic. Parsers, money math, state machines, schedulers — anywhere you can state the expected output before the implementation exists. The test names the next inch of the design.
  • The bug fix. Reproduce the bug red, then fix it. The test is both proof and permanent regression coverage.
  • Known-what, unknown-how. When you know what the function should do but not how, the test forces the interface decision first — arguments, return shape, error cases — while changing your mind is still free.

Where it doesn't

  • Exploratory code. "What does this vendor API actually return?" Write the probe first; tests come after you learn the shape. Testing the probe itself is usually worthless.
  • UI layout. You can't write a failing test for "the settings page feels right." Visual behavior needs a different harness — e2e or a human eye.
  • Glue with no logic. A handler that maps fields to a repository call has nothing to design. Test it at the integration layer where the contract lives.

The rule underneath: if you can't write the failing test, you don't know what you're building — that's the signal TDD sends. Ignoring the signal and writing the code anyway is how the wrong design gets built confidently.

Field notes

  • One red test at a time. Three failing tests means you stopped doing TDD and started debugging.
  • The refactor step is where the design pressure pays. Skipping it turns TDD into "tests first," which is just testing with extra steps.
  • TDD on coupled code is miserable by design — the misery is information about the coupling, not about the practice.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.