Tools to detect duplicated code (Java) [closed]
Asked Answered
V

6

49

I am in a project where previous programmers have been copy-pasting codes all over the place. These codes are actually identical (or very similar) and they could have been refactored into one.

I have spent countless hours refactoring these codes manually but I think there must be a better way. Some are very trivial static methods that could have been moved into an ancestor class (but instead was copy pasted all over by previous junior programmers).

Is there a code analysis tool that can detect this and provide reports/recommendations? I prefer free/open source tool if possible.

Valentine answered 21/6, 2010 at 2:4 Comment(1)
Quite unfortunate that some of the most useful discussions are closed as "off-topic". Did discussion below contain "opinionated answers and spam"? Why as soon as people get a bit of power they experience this constant urge to police something that doesn't require any policing?Lobo
P
26

I use the following tools:

Both tools have code duplication detection support. But both of them lack the ability to advise you how to refactor your code.

JetBrains IntelliJ IDEA Ultimate has good static code analysis with code duplication support, but it is not free.

Primalia answered 21/6, 2010 at 2:7 Comment(1)
Checkstyle - not any moreOwlish
F
5

Most of the tools listed on the Wikipedia article on Duplicate Code Tools will detect duplicates in many different languages, including Java.

Frigorific answered 21/6, 2010 at 2:13 Comment(1)
Since someone removed the unencyclopedic links from wikipedia, here's the link to the old version of the page: en.wikipedia.org/w/…Unlookedfor
B
5

SonarQube can detect duplicated codes but does not give recommendation on eliminating them. It is free and - although with the default setup it can only detect lexically identical clones

Ballroom answered 18/4, 2014 at 11:18 Comment(0)
A
2

Either Simian or PMD's CPD. The former supports a wider set of languages but is non free for commercial projects.

Aircraftman answered 21/6, 2010 at 2:15 Comment(3)
One feature of simian that's quite good is it's ability to find code that was not copied, but developed independently. So it may do the same thing, but have completely different variable names and even sub types. In simainls setup you can specify to ignore variable names and regard sub types as the same parent type etc.Notice
It is extremely rare for clone detectors to find code that "was not copied but developed independently" unless the code fragments are microscopic (ab is a clone of xy and is developed independently but nobody cares). Having built a strong clone detector, my experience is what they find is code that has been cloned; better ones can find cloned code with changed variable names and different constants. Simian is one of these. Strong ones (mine is one of these) can detect when arbitrary subexpressions and statements have been replaced.Hexahydrate
Simian doesn't seem to be around any more in its original form. In any case, the link is dead. Here is a link to a Simian tool but it is not clear to me if it is the same product: harukizaemon.com/simianEminent
B
0

http://checkstyle.sourceforge.net/ has support for finding duplicates

Bodnar answered 21/6, 2010 at 4:18 Comment(0)
H
0

See our SD Java CloneDR, a tool for detecting exact and near-miss duplicate code in large Java systems.

The CloneDR will find code clones in spite of whitespace changes, line breaks, comment insertions deletions, modification of constants or identifiers, and in a number of cases, even replacement of one statement by another or a block of statements.

It shows where each set of clones is found, each individual clone, an abstraction of the clones having their shared commonality and parameterization of the abstraction to show how each clone instance can be derived from the abstraction.

It finds 10-20% clones in most Java systems.

Hexahydrate answered 22/6, 2010 at 4:50 Comment(1)
It is not free, but I will give it a spin nonetheless.Valentine

© 2022 - 2024 — McMap. All rights reserved.