Coding Practice: Why doesn’t someone make a tool to help you detect code duplication and opportunities for refactoring?
Someone does. In fact, it is built into Visual Studio 2012 (Ultimate and Premium editions) under the Analyze menu > Analyze Solution for Code Clones. Running this tool will search through your solution for duplicate code and is capable of finding “near miss” clones.
“Duplication may be the root of all evil in software. Many software practices have been created for the purpose of controlling or eliminating it.”
– Robert Martin
Consider running code clone analysis whenever you write new code just to check to see if similar code already exists. You will definitely want to run code clone analysis as part of your code review process just to make sure you aren’t missing an opportunity to refactor your code in observance of the DRY (Don’t Repeat Yourself) principle.I already touched this topic earlier when discussing “when is it okay to cut and paste code”, but I thought this tool was significant enough to warrant an extra post just to draw attention to it.