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:29

I'm a big believer in DRY coding. Don't Repeat Yourself. If you do it more than once, put it in a helper class.

There is very little that is worse than having to remember to make changes to the same thing in several places.

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

As mentionned in the Rewrite or Repair question, you might do some refactoring like duplication code removal from time to time, as you detect them.

But I believe this kind of "rewrite" action is better managed when detected by a metric, from a code static analysis tool, which:

  • detects those duplicate codes
  • points out a trend (like more and more duplicated code tends to be detected)

In that case, a corrective action can be prioritized, to focus on that kind of refactoring.

On second thought, I wonder if I may be the QA guy Zig is referring to ;-)

查看更多
狗以群分
4楼-- · 2019-03-12 00:32

Code duplication can quickly bite you in the behind and cause you a lot of pain. If I see duplicate code (usually old code from other people of course ;)) I try to refactor it right away. It is very rare that it is not worth the effort. Spend the time now or you'll spend more time doing it later.

查看更多
放荡不羁爱自由
5楼-- · 2019-03-12 00:32

I'm sometimes guilty of copy-pasting, but I try to eliminate duplication wherever possible. The exception is when I have a function which calls several other functions and is very slow. Sometimes the contents of the sub-functions can be combined for speed, or the underlying SQL queries can be combined into fewer or just one.

Example: In inventory management, the minimum quantity on hand for an item is equal to the quantity in your working reserve plus the quantity in safety stock. Safety stock is equal to half of the working reserve.

Function workingReserve(itemCode, someDate)
     ' Inside here is a looping structure that adds up the requirements for the next six weeks (because that's how long it takes between reorder and receipt).
End Function

Function safetyStock(itemCode, someDate)
    safetyStock = workingReserve(itemCode, someDate) / 2
End Function

Function minimumOnHand(itemCode, someDate)
    minimumOnHand = workingReserve(itemCode, someDate) + safetyStock(itemCode, someDate)
End Function

I apologize that this is written in VB, but it's from an Excel VBA function.

In effect, the workingReserve function is running twice on the same data. The performance can be improved by combining the business logic of the safetyStock() function in the minimumOnHand() function.

Function minimumOnHand(itemCode, someDate)
    minimumOnHand = workingReserve(itemCode, someDate) * 1.5
End Function

In the real code, I have comments in the code explaining the business logic, but have omitted them here for brevity.

查看更多
何必那么认真
6楼-- · 2019-03-12 00:33

Avoid factoring down code where the configuration parameters (needed to alter the behaviour) obsfucate the intent of the code. Go as far as you can before you hit this point ... but it`s a balancing act.

查看更多
登录 后发表回答