Red, Green, Refactor
TDD isn't dead. It's just misunderstood. Many developers view it as an academic exercise that slows them down. In reality, writing the test before the code is a design activity, not a verification activity. It forces you to consume your own API before it exists, leading to better developer experience (DX).
The Cycle
- Red: Write a test that fails. (e.g., `expect(calculator.add(1, 2)).toBe(3)`). This confirms the test logic is sound and the feature doesn't happen magically.
- Green: Write the simplest possible code to pass the test. Even if it's hardcoding `return 3`.
- Refactor: Clean up the code. Remove duplication. Optimize. Since you have a Green test, you can refactor aggressively with zero fear of breaking functionality.
The Confidence Metric
The main benefit of TDD isn't fewer bugsāit's the confidence to refactor. A legacy codebase without tests is 'rotten'; developers are afraid to touch it. A TDD codebase is malleable. You can upgrade libraries, change architecture, or rewrite entire modules because your test suite acts as a safety net.