Mustafa
Mustafa

Reputation: 2487

Quick way to compare methods in IntelliJ

I'm currently working on a legacy java code which was written with almost no coding conventions or common practices. I often come across working but very lousy code snippets (typos in variable names, redundancy and many others) that I find myself urging to fix. In one of those instances, I've found a class that contains three methods with different names but almost identical method bodies.

As per my initial observation, the only difference was the name of one of the parameters.

Before taking any steps and merge them though, I wanted to make a comparison of three methods to see if there's any subtle difference that I didn't notice.

I know it's possible to compare two files, compare with the version from VCS etc. but I was not able to find a quick way to compare the method definitions in the same file.

Is there a more practical way to compare different sections of the same file in IntelliJ rather than copying them into different files and run a file comparison?

Thanks,

Upvotes: 51

Views: 7578

Answers (2)

Kristen Balhoff
Kristen Balhoff

Reputation: 1281

Another quick way to compare methods (or any block of code) within IntelliJ:

  1. Copy one version to the clipboard.
  2. Highlight the other version.
  3. Use the "Compare with Clipboard" feature from either the context menu in the editor or from View > Compare with Clipboard.

Upvotes: 128

dbrown0708
dbrown0708

Reputation: 4754

IntelliJ has Locate Duplicates in the Analysis. It's highly configurable and language-aware to the point that it's not just looking for blocks of text that are the same. It would be able to detect if two blocks of code are the same, but with different variable names for example.

You can even expand the scope so that it detects code duplicates that in different files.

See this help page for more information.

Upvotes: 9

Related Questions