Code Review Best Practices

Code Review Best Practices

Julien Danjou

Adopting the right code review best practices can drastically speed up your review process while also improving the quality of your reviews.

Often enough, code reviews can become areas of contention among members of your team, prompting them to become defensive about work they’ve done or changes they may have reviewed. Working with code review best practices can keep such problems from cropping up, but only if your team takes action and remains consistent.

Why code review best practices matter

Code review best practices do more than streamline your team's reviewing processes. Code reviews carry a lot of potential to slow down your project's progress and generate disputes when not approached in the proper manner.

Enhancing productivity

Following certain code review best practices can bring about noticeable improvements in your team's overall productivity. Team members can dive into the details without butting heads or wasting time.

Improving team morale

Your team's morale may be negatively impacted by poorly managed code reviews. A bad code review process can lead to heated disagreements, lowered trust, and impaired motivation to continue working on your project. Best practices that improve clarity and lay a clear foundation for future reviews help to curb the possibility of such negatives arising and hurting your team's performance.

6 code review best practices

When correctly implemented, best practices like the ones covered below can keep your entire team on the same page and get your code to market much faster.

1. Go slow

Proceeding with caution is especially important for any code review to go well. Reviewing code takes time and conscious effort, leaving many drained after as little as an hour of applied focus. To compensate for the high degree of mental strain code reviewing may put you under, it is best if you limit your reviewing sessions to manageable chunks.

In a practical sense, this usually means making 500 lines of code the hourly limit and pausing your review when 60 minutes are up to ensure you’re at peak focus as you work. After a break, you can return to your code and continue reviewing it without suffering as much of a performance drop as marathon reviewing runs often cause.

2. Set clear goals

Define the goals and general scope of your code review before you begin. This saves time by letting your reviewer know precisely what they should focus on and what they’re actually supposed to be looking for.

Make sure your objectives are both clear and quantifiable. You can try integrating a simple checklist into your review process to ensure that results are always consistent and useful.

3. Test before assigning reviewers

Continuous integration checks and tests should be performed before your project's code passes into the review stage. Testing before reviewing ensures the entire codebase remains stable as changes are introduced, and makes the reviewers' job significantly easier. Once predefined tests have been completed, you can proceed with a full review.

4. Limit reviewers and resolve differences of opinion outside of the review

The total number of reviewers involved in a single session should be minimized as much as possible. Too many reviewers working on the same piece of code can slow down the process dramatically. When differences in opinion arise, reviewers can end up debating the proper path forward instead of picking an option and moving on.

Try setting a hard limit of three individual reviewers for each code review and further simplifying code to be reviewed if it seems necessary to add more people to the process.

5. Automate formatting reviews

Automation helps alleviate all manner of mundane code quality concerns before they ever reach a human reviewer. For instance, a linting tool can quickly clean up minor nitpicks such as typos and missing characters without bothering a reviewer at all. Formatting checks can also be highlighted and rectified automatically with appropriate tooling to keep busy reviewers from wasting their time searching for such things.

6. Annotate prior to review

Annotations make it easier for reviewers to find their way through unfamiliar code and understand the changes that have been introduced. By annotating their contributions, code authors can open up a meaningful dialogue with future reviewers and shed additional light on the reasons for the changes they’ve proposed. The act of annotating can even help them spot their own mistakes before their code moves on to the review stage.

Code review can be easy

If you approach the code review process carefully and give the guidelines covered above a go, then you can make it flow much more smoothly for your team. The importance of streamlining what parts of the review process that you can with clarity and automation can’t be overstated. To learn more about automation in particular and how Mergify can help automate the mundane parts of your build process, reach out to our team today.