The TDD loop and the refactoring phase

Test Driven Development consists in first writing a test that describes a business requirement ; this test, when executed, must fail. Then, it consists in writing the minimum of code that make the test succeed. At this point, the business requirement described by the test is fully implemented, and business code can be delivered. But probably, the code isn't readable, isn't robust, has some duplications, and must be refactored to remove all duplications, to be easily readable and understandable, and to become robust.

This process is known as the TDD loop. And the most important phase of this loop is the refactoring phase. It's in this phase that we introduce methods that hides the technical complexity, and that describes the business intent. Once the business intent is clearly exposed in code, the code is readable, understandable, and code is easy to modify.

Refactoring is a process where we have to rewrite the code without changing any behavior. The sole way to guarantee this is to have unit tests, and to run unit tests each time we modify code, and to check that all tests succeed. If tests still succeed, program behavior has not been changed.