How fanatically do you eliminate Code Duplication?

2019-03-11 23:33发布

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:

  1. Sometimes the reduction in duplication is very minimal, because the newly refactored method have too many parameters to be actually useful / readable.
  2. 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).

17条回答
女痞
2楼-- · 2019-03-12 00:06

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.

查看更多
爷、活的狠高调
3楼-- · 2019-03-12 00:06

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.

查看更多
干净又极端
4楼-- · 2019-03-12 00:07

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

查看更多
你好瞎i
5楼-- · 2019-03-12 00:10

Normalize code, normalize variables, normalize database, normalize corporate hierarchy, normalize government...

查看更多
Rolldiameter
6楼-- · 2019-03-12 00:16

I"m pretty much a psycho about it. If I do something more than once I refactor. period. exclamation point.

查看更多
Viruses.
7楼-- · 2019-03-12 00:18

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:

  • Don't modify code for which you don't have (or can't economically/practically develop) a unit test.

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.

查看更多
登录 后发表回答