The Three Laws of TDD
Master the red-green-refactor micro-cycle: write a failing test first, then only the code needed to pass it.
The 30-Second Cycle
TDD is not about testing after — it is a design technique. The micro-cycle has three phases: Red (write a test that fails — it documents the desired behavior before the code exists); Green (write the minimum code to make the test pass — no more, no less); Refactor (clean the code without breaking any tests). The entire cycle should take 30 seconds to 3 minutes. If it takes longer, the step is too big.
The Three Laws
Law 1: You may not write production code unless it is to make a failing unit test pass. Law 2: You may not write more of a unit test than is sufficient to fail — and compilation failures count as failures. Law 3: You may not write more production code than is sufficient to pass the one failing unit test. Together these three laws force code and tests to grow in tiny, safe increments.
Code Challenge
Identify the TDD violations in the bad example and apply the three laws in the good version.
💡Key takeaway
The three laws of TDD are constraints, not suggestions: no production code without a failing test, no bigger test than needed to fail, no bigger implementation than needed to pass.
🔧 Some exercises may still have errors. If something seems wrong, use the Feedback button (bottom-right of the page) to report it — it helps us fix it fast.
Hint: Each law constrains the previous one. Together they prevent over-engineering (Law 3) and under-testing (Law 1). If you can't write a failing test, you don't know what the code should do yet.
✗ Your version