Image by matrishva vyas from Pixabay

Whether you support an open-source repository and need to accept PRs once in a while, or are involved in the code review process for your company’s project, doing a good code review is hard. Depending on your dedication, it may take up to half of the time that was spent by the author of the code on the actual implementation.

Here are some principles that should help you make the code review effective and productive and provide the best output for the author and yourself.

Automate as Much as Possible

A lot of the things typically checked during the code review are easily automatable. Linters, formatters, static analysis tools allow you to quickly detect and even correct code formatting issues, proper API usage, and other typical problems that otherwise distract you, the reviewer, from the crux of the matter.

Remember that the best way to fix an issue is to avoid it. The earlier the author gets the feedback that some of the rules are broken, the better. Add typical rules to the build pipeline or, even better, make it visible in the IDE by using plugins for static analysis tools.

If you’re a Java developer, you’ll want to take a look at tools like SonarQube. If you’re a JavaScript ninja, you might already use ESLint or any other linting tool. You are not a machine to routinely verify indentation or check if the file is closed after use. Leave the mundane things to the automation, and concentrate on more complex and interesting matters, like proper architecture and design decisions.

Moreover, constantly analyze what you typically check during the interview, and think whether this can be automated with a linter rule or a custom test. This will save you a lot of time in the long run and make your code reviews more effective.

Use a Checklist

The amount of stuff you should check is usually quite significant, so if you just rely on your memory, you might forget to verify some important things once in a while.

Take notes of what you usually check, and build a checklist to use in a review. Go through the code not only by reading through each file, or layer-by-layer, but also tick off the items on your checklist.

The checklist may look differently depending on your source code language, the type of your project, build infrastructure, type of the task or patch, etc. So you might have to come up with your checklist depending on your needs. But just for example, here’s a snippet of my typical implementation checklist:

  • the build is green;
  • the changelog and/or README is updated;
  • no dependency conflicts are introduced;
  • comments are present for complicated parts of the code that are not easy to grasp from the first glance;
  • the database migrations are verified on production-like data;
  • unit/integration/UI tests are added or updated;
  • for bug fixes, the root cause is described in the issue comments;
  • all relevant TODO comments are fixed, or follow-up issues to address them are created;
  • etc.

You should once in a while look at your checklist from the automation point of view (see the “Automate as Much as Possible” section). Can something be automated away? In my list, dependency conflicts are something that definitely can be caught automatically — which sounds like room for time-saving improvement.

Review Outside the Box

A lot of the issues or potential improvements arise not from what was changed, but from what was not changed. Minding such things is what I call “review outside the box”.

If the patch fixes some generic bug, you might want to check if a similar problem may be present elsewhere in the code.

If the pull request brings some improvement or refactoring in one class, you should check if it makes sense to propagate this improvement to other classes, and create a task or an issue to do this, if the scope becomes too large.

Don’t let anything slip away — for each review, write a list of spots you don’t quite understand, some suspicious things you need to double-check, or ideas you hope to implement later. Go through this list before the review is over, and create follow-up tasks if needed.

Learn from the Review

When doing the code review, don’t assume that you have the high ground — you are as much a learner here as the author of the code. The reviewer and the author can educate each other, and this is arguably the most valuable part of the code review.

Take a note of some new API class or method used by the author that you didn’t know about. Read up on an unfamiliar design pattern.

If you feel you’re out of your element, e.g. you’re a backend developer, and you have to review the changes that are mostly frontend-related, take your time to at least get a basic understanding of what’s going on: read a tutorial or a book chapter on the subject.

Thoroughly familiarize yourself with the new code that was written or the changes that were introduced — two people are better than one when it comes to sharing knowledge about some part of the system.

Give Kudos!

Don’t let your review turn into a series of grumpy comments to improve this or fix that. This may create a feeling that everything’s bad, especially for less experienced developers.

If you learn something new during the review, or just find the author’s solution cool or interesting, don’t hesitate to write a comment like “Kudos, I didn’t know about this feature!” or “Awesome solution!”

This always helps to relax the generally depressing air of scrutiny around the whole review process and creates a friendly atmosphere. Give credit where credit’s due.

Talk to the Author

Sometimes during the code review, a discussion starts in the comments. The author may not agree with your proposal, but you want to drive your point across. This may take a while or even lead to nowhere since the written text is not always the best form of communication.

If you can talk to the author in person or via a voice chat, at some point in the discussion you should do this. For me, a hint to move the discussion to a coffee break or a voice call is when it becomes more than 3–4 comments long.

Don’t assume that written text is the best way of collaboration. Many people express their thoughts better in a voice medium. Some of the author’s arguments may become more clear to you after verbal explanation.

Let the Author Have the Final Word

There are types of comments that require a fix, like an obvious implementation bug or a deviation from the requirements. But everything else might be more or less a matter of taste.

A method looks too long to you and could be split into smaller methods. A class or module dependency is not clear cut. Four lines of code could be compressed into an intelligently crafted one-liner. Those are good advice you can give to the author to improve the code.

But if the author doesn’t agree, don’t push forward just to prove your point or enforce your sense of architectural beauty. A healthy working atmosphere is worth more than the perfect code.

Don’t keep the changes hanging for too long in the review state — the imperfect code that does the work today is often better than the perfectly crafted code tomorrow.

Conclusion

The code review is a bilateral communication between the author and the reviewer. The goal of the code review is not only to improve the code but our experience and knowledge about the code — to provide food for thought and professional development to both parties.