Last week I tweeted something that got some conversations going:
One of the best habits you can get into before committing code is viewing all the changes in a diff tool. 9/10 times you forgot something.
— Donn Felker (@donnfelker) December 5, 2014
The tweet itself was not a huge novel concept and it’s something that many quality devs do every single day, many times a day. Unfortunately its also something that the majority of folks do not do. Jake expressed his beliefe that this should happen as well in this humorous reply:
@donnfelker I wish you could force open source contributors to do this. Some kind of timer + forced scroll to bottom.
— Jake Wharton (@JakeWharton) December 5, 2014
Well then … Why is reviewing your code before committing (or a code review) important?
Reviewing your code is important because you’re going to catch something you forgot to add, remove or you will see an error in your ways and you’ll fix it before you push the commit it to the server. I can’t tell you how many times (I’m talking thousands upon thousands of times) I’ve caught some invalid code during a pre-commit diff.
How many times have you accidentally pushed that code that had some old debug tracing or a red background color? Probably more than you can remember. It happens to all of us.
The simplest way is to do it via the command line if you don’t use a diff tool or an IDE that supports it. I use git, here’s the command to show an inline diff in the command line interface:
or with the path to the file you’re interested in (if many file are modified)
git diff /path/to/file
If you have a difftool installed you can issue the following command:
or with path to the the file you’re interested in (if many files are modified)
git difftool /path/to/file
With an IDEA IDE (Android Studio, WebStorm, IntelliJ, PyCharm, RubyMine, and more)
You can right click on the file in the editor and select git —> Compare with Same Repository Version and you’ll get the screen below:
Using a HotKey
Personally, I avoid using the mouse at all times so I’ve set up a shortcut in all my IDEA based IDE’s. Here’s how to do that:
I’m using Android Studio here, but the process is the same for other IDEA IDE’s.
Open the Preferences:
Next in the filter, type “keymap”, you’ll see the items the filter below narrow down to simply “Keymap”
In the search box type “compare with” to filter the list of keymap options.
Next right click on the “Compare with the same Repository Version” under your VCS. I’m using Git below.
Now your cursor will be in the “First Stroke” box. I chose “Shift + CMD + D” for my hot key for this command. Simply press those three buttons at the same time “SHIFT + CMD + D” and the values will fill themselves in for you in the “First Stroke” box. You’ll notice at the bottom of this dialog you will show you any other key combos conflict with this key combination. In this case, there are no conflicts. Click “Ok” and and click “Ok” on the parent Preferences window and you’ll be back at your IDE.
Now go back to your file, make a change, and hit “SHIFT + CMD + D” and you’re diff window will show up automatically. 🙂
This can be done with any IDEA IDE, so have fun. Other IDE’s like Visual Studio, etc also have similar features, you’ll just have to hunt them out.