Join GitHub today
[doc] Provide some explanation for WHY duplicate code is bad, like mutations #1349
It occurs to me that none of the documentation for CPD makes any attempt to justify WHY it matters that we should look for duplicate code blocks. It's implicitly obvious to everyone reading this, but not so obvious to Joe/Mary (that doesn't sound as smooth :) ) developer who is asked to reduce the percentage of duplicate blocks in their codebase.
I can imagine why there's no statement to that effect, as again, isn't it obvious? Again, it's not.
I would suggest you add something that includes something like the following:
The real risk of having duplicate code blocks is when one of those blocks mutates slightly so it's not a duplicate anymore. This is a block that was supposed to do the exact same thing as another block, and now it's not doing the same thing. What happens if one of these blocks is duplicated again? Which one was duplicated? After some time, how do we know what the block was supposed to do anymore?
I assume, you are referring this documentation:
I agree, this background information on the reasoning, why duplicated code can be a problem should be mentioned.
In addition to your argument (duplicated code blocks diverging over time), I'd add this:
Suppose, in a code block, which as been copied to a couple of places in the project, a bug is discovered. Now you would need to find all similar code block and apply the same fix. This increases the maintenance cost and also increases the risk, that the bug is not fixed, since it is easy to miss one duplicated code block.
Would you mind creating a proposal pull request for this? You can do this easily on github under this URL and simply edit the documentation in the browser:
After saving, this will create a fork of PMD in your github account and automatically create a new PR with your proposed changes for the documentation. Thanks!