7 Code Review Best Practices and Dynamics to Implement (Part 1)
In the first part of this series, we look at some of the patterns you can correct to keep your code review process going smoothly.
Join the DZone community and get the full member experience.Join For Free
a traditional perspective is that code review allows development teams to find bugs before they hit production. while not entirely wrong, we believe that's a narrow view and there is far more value to be realized within the review process.
yet, if there's an opportunity to capture more value, then there must be something inhibiting us from capturing that value. in code review (we use the words "pr," "pull request," and "code review" interchangeably), a team ideally progresses through seamless interactions that lead to incorporated feedback and everything is terrific. no problem!
but as a practical matter, that's not how it goes every time.
because engineers are human beings, every code review has tension built into it. engineers have their own ways of doing things, and the whole point of code reviews is that someone else is weighing in with their own (sometimes differing) opinions. at times that tension can benefit from a third-party assist.
the problem is that leaders and managers are typically not a part of the review process on any meaningful scale.
to become meaningfully involved, leaders needed a set of signals in this nonstop stream of pull requests to help them identify which reviews to participate in and how best to participate.
in our mission to elevate engineering leadership with objective data, our data science team studied over 100 signals in the data to make better prs, better engineers, and ultimately, better products. (more on gitprime's new code review and collaboration product here ).
this multi-part post covers seven dynamics you can recognize in the data, and act on to improve your team's effectiveness through code review. by emphasizing how teams of human beings work together, you can dig into fertile opportunities for tangible improvement.
we've organized the seven pull request dynamics into two groups: processes and people .
in this first post, we'll examine how you can build effectiveness by focusing first on your team's process, before moving on to your team's people. so we're going to dive into four signals that indicate that you can improve the processes your team has in place.
1. unreviewed code and pull requests
unreviewed pull requests occur when engineers open a code review and it gets approved without any comments, often by the submitters themselves. as a general rule, engineers should never merge their own code, and in fact, most companies don't permit them to. unreviewed pull requests bypass any form of check on the code, as well as skipping the opportunity for improvement and learning.
if the code is worth putting into the main code branch, it is worth having somebody review it. yet as a practical matter, unreviewed pull requests happen a lot, for any number of reasons.
with today's tools, unreviewed pull requests are typically tough to spot on a large team with hundreds of pull requests flowing through on a regular basis and deadlines on the horizon. gitprime data can help identify these instances for you.
unreviewed pull request, as seen in gitprime
in this view, you can see all of the pull requests that this team opened in a single sprint. six of them, in yellow, are still unreviewed. three are unreviewed but still open (instances which we'll cover in the next two sections below), but three have already been merged into the main.
those are the unreviewed pull requests you want to take a closer look at. scan them every single time they occur.
you can evaluate them on a case-by-case basis. even though you're checking them out after they've been merged, you want to make sure that any problems aren't going to run out of control. the sooner you check them, the easier it is to intervene. sometimes, if it's a trivial commit, you can just give qa a heads-up to take a close look there. sometimes, those unreviewed pull requests are non-trivial. walk those back if you can.
in any case, you want to reduce the quantity and frequency of unreviewed pull requests as a best practice. if certain engineers are repeat offenders, have an informal conversation with them about the importance of the code review process. you typically don't need to get heavy-handed with them. focus your conversation on better planning, more so than policies and rules. most engineers will understand your reasoning and do their part to reduce unreviewed pull requests going forward.
2. pull request hotspots
part of the challenge of managing the pull request process is that the vast majority of pull requests are routine. they open, someone makes a few comments, maybe the engineer commits some changes, and then they're approved and merged.
the pull requests that don't go that way can cause some pretty serious disruption. we call those disruptors pull request hotspots, and they're pretty much the opposite of unreviewed prs.
hotspots happen when engineers conduct much more commenting than normal, with lots of back and forth, especially when it's over a very little bit of code. you can also look for an unusual number of follow-on commits that drag the review process on for days. in either case, you're looking for excessive activity that's not reaching any conclusions. these hotspots often signal uncertainty or disagreement.
of course, excessive and problematic commenting depends on your team's baseline. the first thing you want to do is understand what your team's normal behavior is.
pull request dynamics, as seen in gitprime
these charts indicate normal behavior for this example team. according to the data, this team typically resolves its prs promptly, in just over an hour. but we can also see, by looking at the outliers, that despite a pretty good process, not everything goes as planned. these circles indicate some potential hotspots.
outliers in the pull request process, as seen in gitprime
you can dig into those outliers to better understand the activity going on and why those particular pull requests are necessitating so many comments and follow-on commits without any resolution.
drilling down into code review metrics in gitprime
sorting by highest activity, and working from the top down, this code review has an abnormal amount of activity, as indicated by the bars and circles by the cursor. it was opened seven days ago, and it's not large, but it already has two follow-on commits, eight comments, and no approval. considering that this team typically approves a pr in about an hour, a seven-day pr may be out of control. it's time for you to step in.
you can scan the conversation to get a sense of the review. in this case, the conversation is cyclical and isn't resolving. that means it's time for you to get these engineers in a room (or on the phone, if the team is distributed) to sort it out. in almost any case, spending thirty minutes at a whiteboard will likely prevent several more days of spin. furthermore, it will pay dividends in knowledge sharing.
whatever approach you take, you want to get engineers engaged and talking through the problems. what's the context of the hotspot-disagreement, or healthy dialogue? use that insight to form your response.
also, encourage the engineers to resolve and close those prs, because open pull requests can drive uncertainty in a team. alternately, excessive follow-on commits may indicate that the work isn't ready for review, in which case it may be better to withdraw that pull request until the engineer can have it better prepared for the review process.
3. long-running code reviews and prs
long-running pull requests may appear similar to hotspots, but you can really improve your process by drilling into them differently. long-running code reviews may not be a problem in and of themselves, unlike hotspots, but they tend to signal other underlying problems-such as disagreement or uncertainty between engineers. such uncertainty can result from missing requirements, last-minute changes to the design specs, missed components, or simply inertia.
indicated in the data below shows a pull request that's potentially being overlooked. it was opened more than a month ago. three people commented about one line of code right away, and then that turned into radio silence.
neglected or overlooked code review, as seen in gitprime
this code review is stuck. maybe it just got dropped. maybe a war is about to break out over a disagreement and everyone is trying to avoid setting off the powder keg. whatever the case, you have a logjam and no one quite knows how to resolve it. rather than jumping into the muck, everyone has backed off.
as a leader, you can take a look at the comments to see if you can get a read of the situation. figure out if the engineers are grappling with disagreements or uncertainties. and then, having identified at least part of the problem behind the long-running pr, seize the moment to step in and bring resolution to the review.
we recommend you start by talking with the submitter. ultimately, concluding that code review is the submitter's responsibility, and you don't want to take that accountability from them lightly. check in to see how they understand the problem and offer them help in navigating the waters with their teammates.
perhaps everyone needs to move the written conversation to a verbal one. maybe they need half an hour at the whiteboard. maybe they just need your encouragement to go ahead and resolve the pull request themselves.
in any case, you're bringing closure to a code review and to your team. a long-running pull request is like a rock in your team's shoe. identifying these irritants with the data can make a significant difference in helping engineers work well together and advance as a team.
4. blocked pull requests
blocked pull requests are expensive and frustrating. your engineers want to be productive, but in the review process, you need feedback to be productive. and a blocked pull request happens when engineers are stuck waiting for that feedback.
when engineers submit prs, they're in the mindset for that code. they're thinking about it, and it's fresh in their minds. after a few hours, though, their concentration is, by necessity, somewhere else. and their recollection of that code tends to get fuzzier and vaguer by the day. so if code reviewers are slow to comment, that delay can impair their teammates' ability to be productive.
in other words, it's critical that reviewers provide their comments promptly to ensure their full impact.
as a leader, sure, you can nag your team to get their reviews in quicker. but nagging doesn't exactly boost morale. it's helpful when you can bring data into the conversation.
slow moving or blocked pull request, as seen in gitprime
here, sarah opened a pull request and waited six days until a reviewer made a comment. she had four follow-on commits to make, which she accomplished pretty quickly, and then her pr was finally approved seven days later.
that means it took nearly two weeks from the time she wrote the code to when it finally reached qa. that's less than ideal in almost any environment. but the delay on sarah's code wasn't due to sarah's performance. her reviewer failed to respond to her pr in a timely and responsible fashion.
"reaction time" as seen in gitprime
her reviewer, bodhan, has his relevant stats highlighted on this screen. it appears that his time-to-response in this scenario is a demonstrable pattern. on average, he takes a day to respond, and he takes even longer with chris than he does with sarah.
this kind of pattern becomes toxic because it speaks to respect for an engineer's teammates. even if no disrespect is intended, keeping teammates blocked is inconsiderate of their time and productivity.
in this example, bodhan bundles all his review work to the end of his days and then jams it all in. that's efficient for him, because he's busy with his own work as a senior engineer. but it comes at a cost to the rest of the team.
fortunately, this pattern tends to be fairly easy to manage. it's important to set clear expectations with the blocking engineers, and you can focus the discussion on the impact to others. many times, blocking engineers just don't realize the repercussions of their patterns. they're concentrated on their own work and haven't even considered that other people are waiting for them.
in the data, you can identify an engineer's responsiveness and reaction time, so an easy place to start is by establishing some goals. we recommend establishing 4-8 hours max as a target reaction time. usually, by four hours, engineers have a break in their work — lunch, or a meeting, or whatever gaps are normal in your workplace culture. those breaks are a good time to encourage them to do their reviews. by focusing on that time to first comment, you get a pull request off to a strong start and set the tone for the whole process.
usually, an informal chat with a blocking engineer will make all the difference in the world. when people realize their teammates are depending on them, they almost always correct course. every now and again, you might encounter a recurring problem, but we find that almost never happens with this pattern.
up next: the people and collaboration side of code reviews
that covers the four process-based dynamics of code review best practices that you can identify with data and take action on. in the next post, we'll continue our exploration into the people-based dynamics of code reviews.
Published at DZone with permission of Jennifer McGrath. See the original article here.
Opinions expressed by DZone contributors are their own.