Code Review Best Practices – Good, Bad and the Ugly
Tags: programming, code, review, development
Published: 2021-01-15 16:17:15

---
title: Code Review Best Practices – Good, Bad and the Ugly
published: true 
description: This article is about the code review best practices. It explains code review from the common-sense perspective. If you do not want to read through the whole article take a look at the table of contents and the summary for a quick overview.
tags: programming, code, review, development
cover_image: https://www.milanlatinovic.com/wp-content/uploads/2019/01/Code-review-good-bad-and-the-ugly.png
canonical_url: https://www.milanlatinovic.com/code-review-good-bad-ugly/
---

<!-- wp:paragraph -->
<p>This article is about the code review best practices. It explains code review from the common-sense perspective. If you do not want to read through the whole article take a look at the table of contents and the summary for a quick overview. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>What is a code review process?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Code review exists to prevent bugs from ending up in your production. But, it is much more than that. It has technical aspects, technologies, expertise, knowledge change, and feelings involved. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you are the person being reviewed you want to know what can you do to make your code more understandable. On the other side, if you are a reviewer, you want to make a positive impact on the reviewed code.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>A code review in its essence</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>It is a process, which has a goal to assure the quality of delivery. In this specific case, the team is reviewing the quality of the code. This is accomplished by one or more developers examining the code which their colleagues wrote.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":9238} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://milanlatinovic.com/wp-content/uploads/2019/01/code-review-process-wtf.png" alt="code review process wtf" class="wp-image-9238"/><figcaption>Code Quality Measurement - Medium, unknown source</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The quality of code review is hard to measure. However, a good code review should be able to accomplish several items.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Find potential problems/bugs in code and provide ideas on how to handle them.</li><li>Improve the structure of the code if possible (utilize proper software design patterns, refactor and better segmentation, etc.)</li><li>Check if the feature actually works, while trying to find additional test (and edge cases) to check if the code would work in specific situations.&nbsp;</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>What is a good code review?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>First of all, a good code review is friendly, polite and it aims to help improve code and product or feature this code implements. Secondly, it is a process where knowledge and experiences are shared. It is where developers get dedicated time to communicate, raise concerns, consult, and try to discover problems before they happen.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A good code review is event where developers:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>check code quality and ensure it is up to company standards</li><li>share their experiences and concerns, ask questions about possible impacts of new code changes</li><li>address concerns, compare coding approaches and most importantly share knowledge</li><li>build team, trust, and respect for each other</li><li>learn on their mistakes and get well-intended advice on how to improve </li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>More importantly, a good code review is not, or at least it should not be, a place where anyone tries to demonstrate any kind of domination or does any ill-intended actions. It is a consultancy, an unwritten agreement between developers to help each other by double-checking what was done.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Common-sense rules and checks of a code review</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before starting with a code review, we should consider several <strong>important ugly truths</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Planning of reviews is important when describing tasks</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Ticket planning should include the time needed for a proper review.  The team should be aware of something called Definition of Done. This is a list of things that should be completed before the ticket is considered as finished.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Review tasks should be part of DOD and review time should add to that ticket. A team should calculate the cumulative expected time for this ticket. If 3 developers spend 2 hours that is actually 6 hours for code review, which is 1 man/day of the sprint. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Choosing the right reviewer or more of them</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Original developers should test reviewed code before inviting others to review it. This is important because it will save everyone's time. Developers will always make bugs. The only question is when will the team discover these bugs. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Code reviews and the management</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Managers will not always have an understanding of the time needed to do a proper review. It is up to developers and reviewers to communicate this properly. Other team members will not always have an understanding of long or complicated reviews. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>People make mistakes and that is completely all right.</strong> Bugs might appear in code, even after a good review.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":9236} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://milanlatinovic.com/wp-content/uploads/2019/01/First-Time-Managers.jpg" alt="First Time Managers - Code Quality Best Practices" class="wp-image-9236"/><figcaption>Bodith - First Time Managers</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Furthermore, we are aware that different reviews can have different weights. For example, some reviews will be easy, while some of them will be hard. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's try to group these code reviews in categories, so we can establish code review best practices.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Are code reviews worth it?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Yes they are.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Really, no doubt about it, they are.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Yes, code reviews are worth it. I pinky swear on it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>OK, now when we have established that, let us elaborate on why code reviews are worth it. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Downsides of code reviews</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Code reviews take time, sometimes lots of time. Multiple developers are reading code, spending time and focus on it. Management sometimes does not understand why this investment is important.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Sometimes, code reviews can block development. A common example is when one feature is not completed because something complex is discovered during a code review. This is blocking other features etc. However, this might indicate that planning was not done in a good way.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Different people do code reviews with different motivation, spirits and quality. We are not all equal and we should not be. Capable manager or team lead should know how to leverage their team and build code review process to focus on strong sides of team, instead on weak sides.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Upsides of code reviews</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Code reviews are improving quality of code, reducing future technical debt and improving overall quality of software. Furthermore, they are in a way building a team, people communicate, share opinions, concerns, fears, advises and faith. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Without code reviews there would be less quality, structure and teamwork. Even time savings would not be there, because that time saved on code review is already lost and reserved for fixing issues and sorting our overlooked problems.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Code Reviews might take some time</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Reviews won't happen in a minute, it takes time to do it properly. <strong>The team</strong> should plan proper time blocks, depending on the complexity of the review.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Categories of code reviews&nbsp;</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Determining a type of review early is extremely important because it provides us with information on how to handle it. Furthermore, we should be able to compare our capabilities to properly examine the provided code. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Finally, if the review is too challenging we might decide to invite another reviewer or even ask to be additionally educated on technical topics from that review.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":9235} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://milanlatinovic.com/wp-content/uploads/2019/01/pyramid-of-doom.jpg" alt="Code Quality Best Practices, pyramid of doom" class="wp-image-9235"/></figure></div>
<!-- /wp:image -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>Code review is also called the pull request, I  am using both terminologies in this article, depending on the sources that I am referring to. </p></blockquote>
<!-- /wp:quote -->

<!-- wp:heading {"level":3} -->
<h3>Classifying by urgency</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Ben Balter, a Senior Manager of Product Management at GitHub, wrote a fantastic post about <a href="https://ben.balter.com/2015/12/08/types-of-pull-requests/" rel="nofollow">The six types of pull requests you see on GitHub</a>. This post classifies pull requests as:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Just a heads up</li><li>Sanity check</li><li>Work in progress (WIP)</li><li>Early feedback</li><li>Line-by-line review</li><li>Pull request to pull request</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Furthermore, Ben described how each of these types works and when to use it. For example "just a heads up" would be a simple (one-liner) pull request where the author doesn't need review. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>On the other hand, line-by-line is pull request which is used when product is ready to be shipped.</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p> There is no denying that pairing is a highly visible cost to management: "Two developers at one workstation? No way! We already pay them a fortune!" - Paul Blair,  <br><strong>The Unnoticed Costs of Code Review by Pull Request </strong></p></blockquote>
<!-- /wp:quote -->

<!-- wp:heading {"level":3} -->
<h3>Classifying by size and familiarity</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Firstly, when we examine new pull request, we need check number of changed lines.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p> If a pull request has<strong> more than 500 lines </strong>author of that request should consider<strong> splitting it</strong> into multiple parts or find a way to get involved in the code review process. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Furthermore, reviewers should check if tests are executed for big pull requests. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Otherwise, reviewer might find critical issue in the middle of review, and then everything would go back to start.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Classifying by impact</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>There are two ways to consider impact of a pull request:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>What positive and fantastic things will happen when we launch this new cool feature? </li><li>What s*it will hit the fan if and when something goes wrong with this cool new feature?</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>The second one is the important one.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When measuring impact of pull request, team should think about several things:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Could there be any data corruption?</li><li>Will we be able to revert the problems if they happen?</li><li>How many users might be impacted by this?</li><li>What kind of policies might be compromised by this? (GDPR i.e.)</li><li>Is this feature related to SLA (Service Level Agreement)?</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Who should care about code complexity?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>But wait, should the developer or reviewer really be concerned about these items during the code review?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>No.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Because these things should have been defined when a ticket is created and planned. For example, during the sprint planning phase, or even earlier during sessions between product owner and management. <br></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, developers should not close their eyes if they notice any of the mentioned issues. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":9233} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://milanlatinovic.com/wp-content/uploads/2019/01/dt060211.gif" alt="Overthinking quality with Dilbert" class="wp-image-9233"/><figcaption> On the other hand, don't overthink it. - Dilbert again </figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>What are code review best practices?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Code Review Best Practices Checklist:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Keep it friendly and polite</li><li>Make test cases for all newly introduced or modified code parts.</li><li>Do syntax errors checker</li><li>Follow up on the latest coding practices </li><li>Pay attention to software design patterns</li><li>Consider legacy code and impacts to legacy features</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Keep it friendly and polite</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I can not stress this enough. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It is important to be friendly, professional, and polite during the code review process. The bigger the company, professional communication is more important.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It does not matter if you are the person who is reviewing or the one being reviewed. Keep an open mind, accept any and all comments with dignity and grace. Always think of what you can improve and what others might benefit off.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Don't try to be ironic or angry. If some comment is important clearly state it. On the other hand, if something is just your opinion and it does not hold objective improvement state that as well.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Test cases</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Execute any available tests for feature which you are reviewing.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Think of manual (quick tests) which can be done. For example, if the feature changes the database connection function, simply try to connect to the database. If your feature is changing the BasicController component for API, just try to execute a couple of different API calls</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I was already writing about clean code rules and how to improve the quality of your code. By <a href="https://www.milanlatinovic.com/clean-code-rules/">improving the quality of your code</a> in general your skills will get better both as a code reviewer and as the one being reviewed.  </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Syntax errors</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Different developers use different coding editors. Some companies have policies about this and some don’t.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Therefore, it is expected that some syntax errors might crawl up to a pull request.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What does this means?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Don’t examine pull requests only trough pull request interface (i.e. bitbucket or gitlabs). You can also check out these branches and examine them in your trusted editor.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Coding practices</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Most of the companies have some document with coding standards or best practices.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If such document exist, pull request should include comparing the latest code with these best practices.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If there is no such document, here are some quick things to check:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>consistency in variable names</li><li>proper comment blocks on new functions</li><li>separation of concerns</li><li>code should not contain any sensitive data (i.e. access tokens)</li><li>your code should not contain any demo data (except inside of the tests, or where this is really needed)</li><li>code should be clean</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Software design patterns</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Depending on pull request and type of the change some software design patterns can be applied.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Also, if some design patterns are already inside code architecture they should be applied.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>For example, adding a new function in all inherited classes of an abstract class would not make sense.&nbsp;</strong>Furthermore, having a strategy pattern in place and not creating a new strategy for some configuration would also be a wrong usage of that design pattern.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Legacy aspects</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Your software will always have legacy software.<br>Meaning, your pull requests will affect this software somehow.<br>Therefore, you need to handle this. 🙂</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"style":{"color":{"background":"#e3e3e3"}}} -->
<p class="has-background" style="background-color:#e3e3e3">I was already writing about clean code rules and how to improve the quality of your code. By&nbsp;<a href="https://www.milanlatinovic.com/clean-code-rules/">improving the quality of your code</a>&nbsp;in general your skills will get better both as a code reviewer and as the one being reviewed.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Summary</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The code review is an extremely important part of the whole software development process. Therefore, the whole team must make enough effort to properly plan the time needed for this.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Pull requests differ from case to case, but teams should always be aware of impacts and potential issues.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Mistakes will happen to those who work. Code review is essentially risk management, therefore it should be treated as such. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A good code review will improve the overall quality of the code, but it will also reduce the risk related to a new feature.</p>
<!-- /wp:paragraph -->