On Code Review

In which the author makes a strong claim regarding the importance of timely code review, and offers advice to reviewers and submitters.

TL;DR

  1. Make your code review requests a pleasure to read.

I wrote this in March 2014 when I was managing the User Lookup Service team at Twitter, to codify our theory of and approach to code review, and to try to establish some basic best practices. It’s not intended to tell you or your team how to operate, merely to describe how I think about it, and how important I feel good, timely code review is to both the quality and throughput of a team’s code delivery.

The Premise

Pending code reviews represent blocked threads of execution.

In the best case, an engineer blocked on code review is able to make progress on some other change, at the cost of (1) context switching and (2) increased memory allocation. Some engineers are great at this, but the more changes you have in the air, the more total memory is required, and if some of that gets paged out, the context switching gets expensive.

In the worst case, an engineer blocked on code review can’t make any progress at all, because the work allocated to them is serialized on the review. I do my best to make sure everybody on my team has a few potential threads of execution available to them, but sometimes this isn’t possible and in this case code reviews amount to synchronous barriers to getting other work done.

As a team, we all depend on one another to get work done so we can efficiently meet our goals, and often this work cannot be completely parallelized. We’re often blocked on infrastructure shortcomings, or work from other teams. If we think in terms of what we ourselves control, when we are blocked from making progress on a given task, we are generally either blocked on someone writing code or on someone reviewing it.

We can’t limit total work in progress unless we also limit total reviews in progress, and the best way to do this is for everyone to review code in as timely and careful a manner as we can. Assuming I’ve sold you on the premise, how do we make this happen? I believe there are things we can do both as reviewers and submitters to make our code review process as efficient as possible.

Reviewers

Make sure you know what review requests you’re on the hook for

Shorten your event loop; when possible, be interrupt-driven

Do put off a code review for a few minutes while you finish a thought. Don’t put it off for a day or for more than a few hours; If you have time to get up to eat or go to the bathroom, you probably have time to run through your code review requests before paging your code back in.

Prioritize code review above other work*

This is sort of a combination of my previous two points, but I think it bears specific attention: for reasons I hope I made clear in my preamble, threads of execution are blocked and unproductive while code review requests are pending. If you let code go unreviewed, you are explicitly prioritizing your productivity over the productivity of the rest of the team. If everyone on the team is operating at 75% efficiency because they are spending 25% of their time in code review, that is a better outcome than some members of the team operating at 95% and other members idling or thrashing.

If you have nothing to say, then say that! If your name is on a review, you should at least acknowledge it with something like “LGTM, but I don’t really know this code well.” The key point is to minimize the time between outgoing and incoming activity on the review, so nobody’s ever blocked for more time than is strictly necessary.

Expect to spend time on code review every day, throughout the day

From https://smartbear.com/learn/code-review/best-practices-for-peer-code-review/

Be thorough, each and every time

Submitters

Dot your i’s and j’s

Keep review request as small as possible

From https://smartbear.com/learn/code-review/best-practices-for-peer-code-review/

Write good titles and descriptions

Acknowledge comments quickly

In closing

Originally posted in April, 2015 at glen.nu, in slightly modified form. For an even deeper dive into how to write a great review request, see Ryan Greenberg’s excellent post, “Writing pull requests your coworkers might enjoy reading”. Thanks to the entire Gizmoduck team for inspiring me to write this with their consistent dedication to good engineering practices, and in particular to Evan Meagher for his exquisite feedback and copy editing.

Software Engineer at Slack, 10x pedant, fundamentally OK guy