Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminating code duplication in a single file

Sadly, a project that I have been working on lately has a large amount of copy-and-paste code, even within single files. Are there any tools or techniques that can detect duplication or near-duplication within a single file? I have Beyond Compare 3 and it works well for comparing separate files, but I am at a loss for comparing single files.

Thanks in advance.

Edit:

Thanks for all the great tools! I'll definitely check them out.

This project is an ASP.NET/C# project, but I work with a variety of languages including Java; I'm interested in what tools are best (for any language) to remove duplication.

like image 489
Jon Onstott Avatar asked Feb 02 '10 04:02

Jon Onstott


People also ask

What is duplicate code and how to avoid it?

What is duplicate code? Duplicate code as the name suggests is a repetition of a line or a block of code in the same file or sometimes in the same local environment. People might consider code duplication acceptable but in reality, it poses greater problems to your software than what you may have thought.

How much duplication should I set for my source code?

I recommend keeping your duplication percentage to a minimum. Every company has it’s own threshold for duplication percentage which matches their quality standards. Having 0% duplication is very difficult and gets tougher as the number of files of source code increases.

What are the disadvantages of duplication?

Duplication decreases your code quality : Have duplication is fine, as long as you plan to throw your software away soon. Code quality is a necessity to make your software survive for long. Having duplicate code will make your code smelly and increases technical debt associated with it.

Is it possible to have 0% duplication in a project?

Having 0% duplication is very difficult and gets tougher as the number of files of source code increases. The most important thing is to follow proper coding standards like DRY and having an efficient code review process in place. It is important to conduct code reviews on a timely basis so as not to increase the technical debt later on.


1 Answers

Check out Atomiq. It finds code that is duplicate that is prime for extracting to one location.

http://www.getatomiq.com/

like image 182
Chris Missal Avatar answered Sep 29 '22 15:09

Chris Missal