The TDD circle is:
"Write failing Test" -> "Write Code to fit a Test" -> "Refactor"
At "Coding" step we are assumed to write code as simple as possible, just to fix a failing test. We should not write complex code until it is really required.
The next step is Refactor. Should we refactor just written code? I think there is no real sence, as we should be happy with code as far as tests are passing.
Probably Refactoring activity should be forced by some thing, like Code writing is foced by failing Tests. Here some possible factors:
- The next test to be written requires some changes in system (refactoring)
- Performance is bad. We need to improve it without breaking the functionality
- Code review reveals that the code written is hard to understand.
What other reasons you see to start the Refactoring?
Also, is this scheme correct:
"Write failing Test" -> "Code" -> "Refactor" -> "Write failing Test"
or may be it should be seen as
"Write failing Test" -> "Code/Refactor" -> "Write failing Test"
+
"External factor (like bad performance)" -> "Refactor".