TL;DR IntelliJ has an inbuilt diff tool which you can use to compare files, classes or code with the clipboard. Just right click and choose Compare.
I was busy refactoring code in RestMud this morning because I want to try and open source the basic game engine, and then later the Web/REST API, but I want to tidy up the code a little first.
I’ve been working through the code:
- moving classes into new packages
- splitting classes to make code clearer and easier to manage
- new classes make code easier to test
And the more I tidy it up in general, the easier it is to spot smaller problems of code duplication.
Sometimes I fix the duplication by:
- extracting code into a new class and delegating functionality off to the class
- extracting code into private methods
IntelliJ has an incredibly useful piece functionality to help with that in the form of the Compare Tool.
The Compare tool
The compare tool is a built in Diff tool.
I can use it in three different ways:
- right click on a file and “Compare” then choose a file on my hard drive to compare with
- select two classes in the project view and right click to “Compare”
- select code, add it into the clipboard, then highlight a different block of code and “Compare with Clipboard”
They all use the same ‘diff’ view and this is much easier than:
- using a separate tool like WinMerge to compare files outside of the IDE.
- splitting the window and visually trying to compare code
I show all three approaches in this short YouTube Video.