I'm looking for a tool to find duplicate or similar code of Java/Javascript. I can't tell the exact definition of "similar", but I wish the tool is smart enough and give me advices to refactor the code, e.g.,
(1) class A and class B have imilar methods (e.g., there 5 methods have same method name, arguments and similar implementation appearing in both classes), then it should advise to move these similar methods into a base class.
(2) class A has similar code lines at different places multiple times, the tool should advise to move these similar code lines into a single method.
I tried PMD which can find duplicate code lines but it's not clever enough. It did not find out those similar source codes which is widely spreaded in one my projects.
Is there such tool?
If a duplicate code is present in: The same method, create the same Local Variable and reuse it. The same class, create common Method refactoring.
If the duplicate code is inside a constructor, use Pull Up Constructor Body. If the duplicate code is similar but not completely identical, use Form Template Method. If two methods do the same thing but use different algorithms, select the best algorithm and apply Substitute Algorithm.
CPD (for "Copy-Paste Duplication") is a tool that automatically finds duplication in source code. It's tuned for Java code. Also available as a plugin for the EclipseIde. And there's a JavaSpaces version available too, which is funky.
I have not used IntelliJ IDEA before, but I just found it supports Analyzing Duplicates with it's ulimate edition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With