One of the biggest changes in the thirty-something years I’ve been programming full time is the gradual acceptance of code reviews of various forms.

I’d never even heard of code reviews until the early ’90s. I’m not saying they didn’t happen, because of course they did, but they weren’t widespread outside of failure-is-not-an-option situations, like medical-device firmware or control code in rockets. You know, the sort of thing where bugs kill people. 1

For most programmers 30 years ago, the thought of someone else looking through your code felt…invasive. Sure, if you’re collaborating with people, you have to at least look at the interface to your teammate’s code to figure out how to interface with it, and you’ll probably end up single-stepping through someone else’s code—but actually walking line-by-line through code and passing judgment on it felt deeply weird. Like reading someone’s diary, or (in modern terms) stumbling onto someone’s browsing history.

Anyhow, in the early ’90s I transferred into a team at Microsoft that had a code review policy. Lucky for me, my project was so inconsequential in that team’s grand scheme of things that my project team and I were completely ignored. Among other things, we were left to decide what our code review process was. I’m not even sure what the actual official code review process for the big team was; we just did what we thought made sense and nobody ever checked up on us. I certainly wasn’t going to ask for guidance, in fear of having some horrible process imposed on us. Much better to ask for forgiveness than permission.

To my shock, I found code reviews immediately and undeniably useful. I’ve been doing them with my teams ever since—but not for the reasons I expected.

The most obvious reason to do code reviews is to detect bugs before they’re checked into the project. If your code review process is at all rational, the person doing the review is well-prepared to understand the code that’s being checked in. Maybe they’ve been part of the implementation of that section of the code, or they’re an expert in some other bit of code that the new code relies on, or they’re a frequent user of the code they’re reviewing. In any case, they might be able to spot problems like an assumption you’ve missed or broken, a misuse of some bit of code you’re calling, or maybe a change to the system’s behavior that will break some other bit of code the reviewer is working on.

Does this happen? Do code reviews actually turn up bugs? Sure, a few—at least in my experience, given the way we conduct code reviews on my team.

That’s an important caveat . The value you’re going to get out of code reviews is going to depend on how much time and effort you invest into them and how you conduct them. Here’s a quick description of how most Sucker Punch code reviews work these days:

  • It’s real time—two people sitting down at the same computer (at least in pre-pandemic times).

  • It’s informal. When you have code ready for review, you walk over to a plausible reviewer’s office and ask them for a review. Our social contract is that when someone asks for a review, you agree, barring really pressing circumstances.

  • The reviewer walks through the change in a diff utility while the reviewee provides commentary on the changes. It’s a dialogue, with the reviewer asking questions until they’re satisfied they understand the changes being made, suggesting changes, identifying things that need to be tested, and discussing alternate approaches. Having the reviewee drive the review is usually a mistake; it’s too easy for the reviewer to just accept what the reviewee says instead of thinking things through for themselves.

  • The reviewee is responsible for taking notes on all the suggested changes and extra tests to run. The social contract is that all suggestions are incorporated, at least by default.

  • Depending on the scope of the change, the code review can take five minutes or five hours. It’s rare to have a code review that doesn’t result in at least a change or two before check-in. A big code review can result in pages and pages of notes to incorporate.

  • Usually one code review is enough. After the appropriate changes are made and extra tests run, the reviewee commits the code. Sometimes, if it’s a big change with lots of review notes, the reviewer might re-review the updated changes. If the original code reviewer isn’t confident they understand some part of the change, they might suggest that another person on the team also review the code. But most often it’s code review + incorporate changes + commit.

With this process, we do find bugs…but again, not in the way that you’d expect. Here are the three basic ways we find bugs in code reviews, roughly sorted by how often bugs are found, most common to least common:

  • Before you ask for a review, you walk through the diff yourself to make sure you’ve tidied up anything embarrassing before showing it to someone else. In the process of self-reviewing, you find a bug: say, an error case you’ve missed. You fix the problem before anyone sees it.

  • During the review, you’re talking the reviewer through a particular section of the code…and being forced to explain your approach helps you understand why it’s flawed. You point out the bug to the reviewer, discussion ensues, and you make a note and move on. Or, if the flaw you’ve discovered is big enough, you just bail on the code review entirely, restarting it once you’ve made the wholesale changes you need.

  • During the review, the reviewer sees a problem you’ve missed. Or the way you describe what you’ve done makes it clear that you’re misunderstanding some bit of code you’re calling. You discuss the possible issue, agree that it’s a problem, and make a note.

It’s rare that the reviewer finds a bug just by staring at the code in question and applying deep insight. The code review process itself tends to surface them, either during preparation or as a result of talking through the change. That’s why it’s useful for code reviews to be dialogues—the process of explaining things and understanding that explanation illuminates any mismatched assumptions between reviewer and reviewee. That’s good for finding bugs, but it’s also good for knowing where a comment is needed or a name needs to be changed.

It’s important to point out the inarguable limitations of our code review process. Every single bug in our code managed to sneak through a code review, and we have thousands of bugs! We don’t make exceptions to the code review requirement—every single line of code that gets checked in has been reviewed—so every bug was missed by multiple people before it got checked in. Code reviews find bugs, but they certainly don’t find all of them .

Code reviews are an inefficient way to find bugs. Yet we’re still doing them. That’s because finding bugs is only one of the reasons we do code reviews, and it’s not even the most important reason.

Code Reviews Are About Sharing Knowledge

Here’s a more important reason to do code reviews—properly conducted, they’re an excellent way to propagate knowledge across your team.

That’s particularly important for the team at Sucker Punch, because we’re flexible about assignments, with coders moving pretty freely between different parts of our codebase. That works a lot better if each of the coders has basic knowledge of how the different parts of the codebase work. Code reviews are a good way of spreading this knowledge.

Imagine arbitrarily dividing the programmers on your team into “junior” and “senior” groups, based roughly on familiarity with the codebase. Senior coders know the codebase well, while junior coders are still learning its ins and outs. Our code reviews involve two people, so there are four possible combinations of seniority for the reviewer and reviewee. Only three of them are useful, as shown in Table 6-1 .

Table 6-1. Code review taxonomy |   | Senior reviewer | Junior reviewer | | --- | --- | --- | | Senior reviewee | Useful | Useful | | Junior reviewee | Useful | FORBIDDEN |

If a senior coder reviews a junior coder’s work, they’re well-positioned to see problems—not just bugs in the code being reviewed, but general misunderstandings the junior coder has. Perhaps the junior coder hasn’t followed the team’s formatting standards correctly, or they’ve generalized their solution too early, or they’ve written a complicated solution to a simple problem. None of these are bugs, per se, but violating the Rules of Programming degrades the quality of the code, so the senior coder should note this to be fixed in the code review.

If a junior coder reviews a senior coder’s work, they’re less likely to find problems, but they’re more likely to ask questions in order to figure out what’s going on. In the process of answering those questions, the reviewee helps the reviewer understand the context for the code, leaving them with a better understanding of how all the pieces of the codebase fit together. The reviewer sees and can ask about examples of good code—correctly formatted, appropriately engineered, and clearly structured and named.

Think of these two junior–senior interactions as part of the education process for new coders on your team. To be effective, new people need to know how all the pieces fit together, how code gets written on your team, and why things are done the way they are. Code reviews are an excellent way to transfer all of this informal knowledge to new members of the team.

Useful combination number three is a senior coder reviewing another senior coder’s code. This is a good chance to find bugs and to check both coders’ assumptions about how the changes fit into the overall scope of things, discuss future work in the area, identify extra tests that might be run, and ensure that at least two people understand the lines of code being checked in.

The Forbidden Code Review

The last combination, a junior coder reviewing another junior coder’s work, is not useful. In fact, it can be really destructive. All of the benefits I just discussed evaporate when both coders are junior. There’s no knowledge transfer, there’s not enough context to find bugs, and there’s no using the code review as a springboard to talk about future directions. At worst, the two junior coders reflect half-formed opinions back and forth until they seem like official team policy. When weird paradigms and conventions pop up in Sucker Punch code (which happens, despite our best efforts), it’s often a result of two junior coders ping-ponging reviews back and forth. So we ban this sort of code review.

The True Value of the Code Review

We find bugs and we transfer knowledge. That’s probably enough to justify the effort we put into code reviews—which usually amounts to maybe 5% to 10% of the time spent writing the code in the first place. But there’s one more important benefit of code reviews, probably the most important one of all, and it’s entirely social:

Everyone writes better code if they know someone is going to look at it.

They’ll follow formatting and naming conventions better. They won’t take shortcuts or leave tasks for later. Their comments will be clearer. They’ll solve problems the right way, not with hacks and workarounds. They’ll remember to take out temporary code used to diagnose a problem.

All of this happens before the code review itself—it’s a result of the pressure we put on ourselves as programmers to do work we’re proud of and are happy to show to our peers. It’s a healthy form of peer pressure. We write better code, and over time this results in a healthier codebase and a more productive team.

Code Reviews Are Inherently Social

Summing up, well-conducted code reviews are good for three reasons:

  • You’ll find some bugs.

  • Everyone will understand the code better.

  • People will write code they’re happy to share.

Look, code reviews are like any process. If you’re going to spend time on them, you want them to be productive. That means thinking through what you’re getting out of them and why. Get rid of the parts of the process that aren’t helping, and double down on the things that work. Either you get more out of the time you’re spending, or you spend less time to get the same value.

Unless you’re doing something like pair programming, writing and debugging code is usually a solitary act. One lone warrior, alone at their keyboard, triumphing over bugs and recalcitrant libraries.

Code reviews aren’t solitary. Most of their value comes from the social interactions between the reviewer and reviewee. You realize you’ve got a bug while explaining a line of code, you explain a section of code well enough that the reviewer uses it correctly the next time they call it, you clean up the hack you don’t want anyone to see before asking for a review, or you learn a simpler way of doing something from the reviewee’s explanation of the technique they’re using.

Knowing that the value in a code review springs from social interaction, from two people talking through a change, you should make sure your code review process encourages that interaction. If the review is quiet—if the reviewer silently flips through a diff and occasionally grunts while the reviewee silently watches—then something is wrong. Yes, it’s still a code review, but you’re missing out on the real value that the review could provide.

And if all of your code reviews turn into arguments, you’re doing them wrong! A reviewee who isn’t open to the reviewer’s input won’t learn anything; neither will a reviewer who doesn’t make the effort to understand why the reviewee wrote the code the way they did. And, in any case, code reviews are not the forum for arguing about the project direction, or about the team’s conventions or philosophy. Work out those issues as a team; you won’t resolve anything in a series of two-person spats .

A healthy code review strengthens your codebase while it strengthens the bonds of your team. It’s a professional and open dialogue where both participants leave having learned something.

1 Real people, not virtual ones. I’m a video game programmer: virtual people die from my bugs all the time.