Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 988
Mustafa Avatar asked Dec 13 '13 22:12

Mustafa


People also ask

How do I compare codes in IntelliJ?

Compare any text sourcesPress Ctrl+Shift+A and start typing to locate the Open Blank Diff Window action. Paste any text you want to compare in the left and right panels. Right-click either left or right panel and select Switch to Three-Side Viewer to compare any three text sources.

How do I get a list of methods in IntelliJ?

By default, IntelliJ IDEA shows all classes, methods, and other elements of the current file. To toggle the elements you want to show, click the corresponding buttons on the Structure tool window toolbar. to show class fields. to have protected class members shown in the tree.

How do I compare two commits in IntelliJ?

Review the differences between two commits IntelliJ IDEA allows you to check which files were modified between two commits instead of having to browse the changes in each commit in between. Select any two commits in the Log tab of the Git tool window Alt+9 and choose Compare Versions from the context menu.

How do I navigate through methods in IntelliJ?

To navigate backwards, press Ctrl+Alt+Left . To navigate forward, press Ctrl+Alt+Right . To navigate to the last edited location, press Ctrl+Shift+Backspace . To find the current caret location in the editor, press Ctrl+M .


2 Answers

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.
like image 51
Kristen Balhoff Avatar answered Sep 25 '22 23:09

Kristen Balhoff


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.

like image 43
dbrown0708 Avatar answered Sep 25 '22 23:09

dbrown0708