How fanatic are you about elimination of duplicate code?
Personally, whenever I see duplicate code, either in testing code or production, I tend to refactor the duplication away. The only exception I make are these:
- Sometimes the reduction in duplication is very minimal, because the newly refactored method have too many parameters to be actually useful / readable.
- Sometimes, in test code, when several tests use the same piece of code that's not really a coherent flow I leave the duplication alone (but not always - depending on the dup size).
I consider myself an extreme fanatic when it comes to eliminating code duplication. As long as we are not at a critical point in a milestone, I do my best to remove any duplicate code I come across in my code. Eventually I simply run out of time and have to leave the code alone for the next milestone. In that case though, I often check in at least a comment noting the duplication and what needs to be done to remove it.
VERY. As far as i'm concerned almost all of our development tips, sayings and "best practices" stem from the principle of not repeating code and making it reusable. MVC, decorator, OOP etc etc.
Obviously one needs to maintain a sense of pragmatism at times but I tend to lean very heavily towards DRY.
I have always adhered to the principle that the first duplication (i.e. original plus one copy) is not usually worth the effort to remove. This is because original plus one copy is probably a "one off", and you don't gain enough from removing them to justify the work.
However, as soon as I start to make a second copy, I then rewrite all three to remove the duplication. That's because it has now (in my opinion) moved from "one off" to "trend". It becomes more likely that I'll use the code again so the effort to remove the duplicates is now worth it.
I hesitate to call the process "refactoring", because that's a buzzword from the XP camp, and I was doing this back in the early '80s with FORTRAN and C.
Good programming practice is ageless (and usually trendy-less, too).
Cheers,
-Richard
Normalize code, normalize variables, normalize database, normalize corporate hierarchy, normalize government...
I"m pretty much a psycho about it. If I do something more than once I refactor. period. exclamation point.
As has been said, I try to live by the "DRY" principle - but I'd also say that there is another condition where I'm often reluctant to eliminate duplication:
That set of tests would include the calling code for any method extracted.
If I can't test it, I can't really say I haven't introduced a defect. With a test suite, I can at least say it did what it used to.