Re:In Defense of Copy and Paste
by bobwaycott on Friday February 08, 2013 @11:23PM
(#5191656)
Same here. Doing the same thing three times is always my rule of thumb for when to take a look at refactoring, automating, etc.
Also at two, the repetition probably reduces complexity rather than having to deal with both use cases in the same code. At three, assuming the use cases are not any further apart, I think they start to simplify when refactored.
A lot of the time, you don't even know if two swaths of code are "coincidentally" identical (don't refactor) or identical in a "deep" way (refactor), even when the program is yours -- you just don't know how the program will evolve.
In the absence of additional information, I usually refactor only when I see three similar code paths, since by that point a project rarely goes back. Over the years, it's turned out to be a surprisingly good rule of thumb.