The “3 rules of TDD” is hard to understand. Let me rephrase it a bit.

Thongchai Kolyutsakul
1 min readMar 19, 2022
Image from https://marsner.com/blog/why-test-driven-development-tdd/

Uncle Bob’s The Three Rules Of TDD helps guide us do TDD (Test-driven development) better. These are the rules:

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

I find them hard to understand. Those NOT and UNLESS words take my brain 10 minutes and 10kcal of energy just to understand one of them. I think it is easier to understand if we rephrase them like this:

  1. If all tests are passing, don’t write more code.
  2. If any test is failing, don’t write more test. Fix code to make tests pass.
  3. Go read number 1.

And after all functionalities are implemented, you refactor the code.

Now that’s better! 😆 I hope it will help you remember it easier.

--

--

Thongchai Kolyutsakul

iOS developer since 2009. Working @Viki in Singapore. I write about iOS development, Swift language, and general software engineers tips.