Implementing effective code reviews

June 14, 2012 by Joe Sebast in Software Development

Side of KeyboardCode reviews are an essential, but often dreaded, part of a developer’s work day. But with good planning, the right tools, and a moldable attitude, reviews can perfect code, grow your knowledge, and enrich your working relationships.

Your best defense is a good offense

The most effective code reviews get their start well before the review even begins. Here are three ways programmers can set their work up for success.

Comment your code

Commenting is the best way to start your code review before it even begins. When you comment, you benefit from your own reminders, and your reviewer won’t have to stop to ask about that little line of code because they can just read your comments. When giving a code review, having those little snippets in there can really help keep your review moving forward.

Use a coding assistant

Paired (or extreme) programming is great, but a tool like ReSharper doesn’t need coffee first thing in the morning and doesn’t get stuck in traffic. A simple add-in coding assistant can dramatically increase your development skills in just a few days.

Use a comparison and merging tool

At Wadeware we use WinMerge for two-way merges and KDiff3 for three-way merges (both are free and open-source). If you are checking a file with a large number of changes and are having difficulty effectively reviewing your changes, skip the tools and do your review in an IDE (i.e. Visual Studio), or a simple source code editor like Notepad++.

Tips for the code author

The best code reviews require an open and engaged code author. Here are four tips to help you when you are scheduling reviews for your own code.

Do a self-code review

You would be amazed at how many little mistakes you can make in just two hundred lines of code. If you can take a few minutes to scan your code for inconsistencies or TODO notes, you’ll find your code reviews go much smoother.

Understand your own code

Tying into the last comment, you must be able to explain and defend your code to whoever is taking a look at it. Remember, this is an opportunity to show off your skills. If you’ve written a really clever little tidbit, you’ll need to be able to explain how awesome it is to your reviewer.

Be open to improvements

Think Zen and let your ego go, this is an opportunity to not only show off your skills, but also to improve upon those skills. If your reviewer has some good insight, take it! It can only help you become a better developer.

Review often

Burn out happens fast, so keep reviews small and crisp. Try aiming for two hundred lines of code (give or take a hundred lines). This will keep the complexity level down and keep your reviewer from overloading on information. Also, review at a nice slow pace, aim for 300 lines of code per hour.

Tips for the reviewer

The reviewer holds the power for a seamless review. When you review your coworker’s code, keep these four tips in mind to produce better code and foster a mutually enriching work environment.

Be positive

No one wants to be told their child is ugly, but they won’t be insulted if you offer to help their child with their math homework. As developers, our code is like our children. We put a lot of time and effort into making it the best we can, so when you have a critique, try to give it in an uplifting way.

Ask clarifying questions

If you don’t understand something, don’t be afraid to ask about how or why the author did it. On the other hand, if the code seems to be flawed, don’t assume that the author is incompetent; instead, ask why they did it. Typically, they’ll either realize their mistake or teach you something new and clever!

Use a white board

Using a white board (or some other note-taking device) allows you to write down thoughts as you have them and keeps you from unnecessarily interrupting the author. It also allows you to write down any good tweaks you come up with on the fly.

Don’t force changes

If there isn’t anything wrong, you shouldn’t feel obligated to find something that isn’t there. Sometimes the best thing you can say is, “looks good!”

Resources for code review

Hopefully these few tidbits are helpful to you in your adventures as a developer. If you are looking for further resources you can try: