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,
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.
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.
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.
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 .
Another quick way to compare methods (or any block of code) within IntelliJ:
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.
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