Reasoning to wait until third time in the Rule of Three

dryrefactoringrule-of-three

I just came across the article "Rule of Three" in wikipedia

Rule of three is a code refactoring rule of thumb to decide when a
replicated piece of code should be replaced by a new procedure. It
states that the code can be copied once, but that when the same code
is used three times, it should be extracted into a new procedure. The
rule was introduced by Martin Fowler in Refactoring and attributed
to Don Roberts.

I know that this is just a rule of thumb, but why is it recommended to refactor only after the second duplication? Is there any downside to refactoring when we write the first duplication?

Best Answer

I think this rule of thumb exists because it is easy to get caught into playing "What if..." when designing the code for the first time or after the first duplication. I've encountered severe analysis paralysis in some cases because people started designing functionality that might be needed later. But not needed for the immediate problem at hand.

There is an art for designing/writing only what you need, while keeping the code amenable to future re-factoring.