Don't repeat yourself. I'll say it again – don't repeat yourself. When writing code, consolidate duplication. (But when explaining, say it several different ways). Code duplication hinders code readability and maintainence.
On Friday I downloaded an interesting tool called Simian It is so named because it's a SIMIlarity ANalyser, or if you like, after the people who write cut-and-paste code. It's a command-line program. It's simple and no-frills, but it runs quickly (analysed 40 projects in under 5 seconds) and the output is simple and to the point. I don't regard this as a disadvantage - I'd soon get over flashy presentation. It impressed me less as a big important program and more like a small but very useful utility that prompts the question: Why didn't someone think of that decades ago?
In the project under inspection's directory, I ran the following command line:
C:\temp\simian\bin\simian-2.2.14.exe **\*.cs -threshold=15 > simtestout.txt
You can see that the Simian executable is living in a temporary directory. "**\*.cs" tells Simian to look at all .cs files in the current directory and subdirectories. threshold=15 tells it to ignore matches less than 15 lines long. And the output, in keeping with the simple and flexible idea, goes to standard output, so you have to redirect it to a file, in this case simtestout.txt
The output is a list of duplicates, e.g.
Found 17 duplicate lines in the following files:
Between lines 350 and 383 in C:\PROJECT_TFS\Src\Main\ APP\App.WebSite\WebParts\FooControl.ascx.cs
Between lines 1190 and 1222 in C:\PROJECT_TFS\Src\Main\ APP\App.WebSite\Admin\BarPage.aspx.cs
Sure enough, there's a method that's duplicated between those two classes. Well-spotted!
To use it beyond a free trial, we'd need to buy a site licence for US$499.
Simain supports, Java, C#, ASP, Visual Basic and more. In fact it will treat files that it doesn't understand as plain text, and scan them for duplication anyway.
Simian is from RedHill Consulting in Australia, a continent with no native species of monkeys.
Our own Howard van Rooijen has written about Simian and has integrated Simian and Visual Studio.
David Höhn pointed out the "Copy Paste Detector", an equivalent function in Eclipse. He says he could do the equivalent in a perl script for free. Now that's got me thinking...