Refactoring and the Rule of Three – Baby Steps
Refactoring is missing from the previous rules. We feel that TDD without refactoring is just half of the game. For the time being, in the refactoring phase, just look for duplication. But wait until you are sure you have a duplication pattern. Avoid removing duplication too soon, as this may lead you to extract the wrong abstractions.
Important
Extract duplication only when you see it for the third time.
The Rule of Three defers duplication minimization until there is enough evidence of duplication. Code that does not contain duplication is often referred to as abiding by the Do not Repeat Yourself (DRY) principle.
Important
Duplication is far easier to refactor than the wrong abstraction. Use the Rule of Three if you are not sure what the correct abstraction would look like.