title: BETTER LEARNING THROUGH CODE REVIEWS
url: http://capgemini.github.io/learning/better-learning-code-reviews/
hash_url: c8a2558279f3d400151c8881ef778da8

<p>One of the main reasons I wanted to join a big company was the opportunity to learn. I wanted the chance to work on bigger projects with colleagues who’ve been there and done that, and to benefit from their experience.</p>

<p>In the 4 years I’ve been at Capgemini, I’ve been lucky enough to get those opportunities, and for me, the thing that has been the most helpful in terms of my own learning has probably been code reviews - both other people reviewing my code, and me reviewing other people’s code.</p>

<h2 id="why">Why?</h2>

<p>Some people say <a href="https://ihofmann.wordpress.com/2012/10/12/five-reasons-why-structured-code-reviews-are-waste-of-money/">code reviews are a waste of time</a>, and if they’re talking about what code reviews used to be, I’d be inclined to agree. The idea of scheduling <a href="https://en.wikipedia.org/wiki/Fagan_inspection">a set of formal meetings</a> where a bunch of developers get together and go through a file line by line seems ludicrous nowadays.</p>

<p>Thankfully, it doesn’t have to be like that any more. With modern tools, code reviews are far more efficient. They’re an integral part of our workflow at Capgemini, and are <a href="http://www.tsphethean.co.uk/blog/2013/11/28/Reviewing-code-reviews/">vital in improving the quality of our code</a>.</p>

<h3 id="more-haste-less-speed">More haste, less speed</h3>
<p>Velocity is a term that gets used a lot, and it’s generally agreed that high velocity is a good thing. But sometimes, when a team of developers gets into their stride, they can resemble a team of galloping horses. Too much change in a short space of time can be overwhelming, and it can sometimes be good to create a bottleneck to slow down the pace of change and make sure that the change is appropriate. Besides, code reviews and the improvement in quality they bring about can actually <a href="http://blog.codinghorror.com/code-reviews-just-do-it/">increase productivity</a>.</p>

<h3 id="keep-it-clean">Keep it clean</h3>
<p>Once bad code has entered the code base, it can be very difficult to get rid of it. You may think that you can take care of coding standards and unit tests once you’ve got a few more high-priority tickets out of the way, and got your project manager off your back, but the truth is that <a href="http://on-agile.blogspot.co.uk/2007/04/why-you-wont-fix-it-later.html">you probably won’t get round to tidying up later</a>.</p>

<p>Code reviews are the ideal place to ask questions like:</p>

<ul>
  <li>Does this code actually solve the right problem?</li>
  <li>Does it solve the problem in an appropriate way?</li>
  <li>Is there unit-test coverage?</li>
  <li>Is the code secure?</li>
  <li>Is it likely to introduce any bugs?</li>
  <li>Does it comply with relevant standards?</li>
  <li>Are there any relevant accessibility considerations?</li>
</ul>

<p>Don’t imagine that you can rely on the QA team to catch any bugs that might be lurking, or that the developers are too busy for this sort of thing. The test team have got enough to do already, without devs deploying shoddy code onto the test environment.</p>

<h3 id="pour-encourager-les-autres">Pour encourager les autres</h3>
<p>Code reviews are an ideal place to encourage a mindset of <a href="http://tech.trivago.com/2015/08/31/culture_of_quality/">caring about quality</a>. They’re an opportunity to reinforce positive values within the development team, and get everyone thinking about how the code base can be improved.</p>

<p>The code review can also be a springboard for conversation, and an opportunity to explain the thinking behind a code change (although beware of the temptation to <a href="https://en.wikipedia.org/wiki/Parkinson%27s_law_of_triviality">bikeshed</a>).</p>

<p>For these reasons, the reviewers shouldn’t just be the senior devs - quality is everybody’s responsibility. Similarly, the senior members of the team shouldn’t imagine that <a href="http://blog.8thcolor.com/en/2014/04/5-reasons-you-are-not-doing-code-reviews/">their code doesn’t need to be reviewed</a>. Reviewers don’t need to be the subject matter experts for that feature. Even if the review comments are “I don’t understand this code” - that’s a sign that the code may need more comments. In fact, code reviews are a great way to ease people into learning about new areas of your codebase.</p>

<h3 id="lessons-learned">Lessons learned</h3>
<p>Everyone makes mistakes, and if you don’t realise that it’s a mistake, you’ll never be able to learn.
For junior developers, or even those with more experience, it’s easy to get into bad habits, especially if you’re used to working on your own. Pull requests can be a fantastic learning resource - a chance to see the workings of your colleagues’ minds, to consider alternative approaches to solving problems, and to ask why a particular approach has been chosen.</p>

<h2 id="how">How?</h2>
<p>Hopefully you see the value of doing code reviews, and either they’re part of your workflow already, or you’re planning to start doing them. So I’d like to share a few ideas about improving the quality of the code reviews, and hopefully that will help to improve the quality of the code itself.</p>


<p>When we moved from SVN to Git a couple of years ago, we had the opportunity to put a system in place. By using a feature branch workflow, pull requests become part of the process, and in theory no piece of code should go live unless it’s been reviewed. So it’s important that your tools support you.</p>

<p>Github pull requests are good as far as they go, but code review tools like <a href="https://www.atlassian.com/software/stash">Stash</a> provide more advanced features, which we’ve found to be really useful. For example:</p>

<h4 id="approvals">Approvals</h4>
<p>Reviewing isn’t just a case of looking at code, but of signing off that you’re happy for it to go live. Stash includes the notion of approvals, and allows you to require a certain number of approvals before the pull request can be merged.
One important thing to check is whether the repo is set up so that approvals are cancelled when more changes are pushed to the branch - annoyingly, this doesn’t seem to be the default.</p>

<h4 id="granular-permissions">Granular permissions</h4>
<p>Per-branch permissions can be set up so that only certain members of the team can merge to the main branches - this helps to keep team leads aware of what changes are going to go live.</p>

<h4 id="tasks">Tasks</h4>
<p>Another nice feature of Stash is tasks - you can add a checkbox to a comment, and set your repo up so that pull requests can’t be merged while they have tasks outstanding. For instance, you can approve the pull request with the caveat that something needs to be fixed before it gets merged.</p>


<p>One of the drawbacks of Github pull requests is that when someone pushes changes, the diff view will no longer show comments from previous versions, whereas Stash will still show comments on lines that haven’t changed since the comment was made. The other nice thing is that you can easily see which changes you’ve already viewed - especially useful in pull requests that go through a lot of iterations. A useful feature that was in Crucible (Atlassian’s SVN code review tool) but not in Stash, is an indicator of what percentage of the review each reviewer has viewed, so you could get an idea of whether you need to chase up the reviewers. Nice, but not enough to make me want to go back to SVN.</p>

<h4 id="integration-and-automation">Integration and automation</h4>
<p>The other good thing about Stash is that it plays nicely with our issue tracker. We have quite a granular workflow set up in Jira, and a lot of the transitions are automated. For example, when a pull request is created, the relevant ticket goes into “Code Review” status, and when the pull request is merged or declined, the status is updated again. We also have integration between Jenkins and Jira so that when the test environments are built, it updates the status of tickets pending build, and the QA team can see which tickets are ready for testing.</p>

<p>This is great, because it means no more nagging people to update the status of their tickets - it’s easy to see the current state of play for any of our projects, and to trust that the status reflects the truth.</p>

<p><a href="http://www.cgpgrey.com/blog/humans-need-not-apply">Computers are better at boring jobs than humans</a>. Boring jobs like checking that code comments are correctly punctuated and indented. I’ve written before about <a href="http://www.red-route.org/articles/keeping-clean-why-coding-standards-are-important">the importance of coding standards</a>, and while it may come across as pedantic, when code is written according to standards, it’s much easier to debug.</p>

<p>Tools like <a href="https://github.com/brigade/overcommit">overcommit</a> make it simple to integrate code linters into your workflow, and in our <a href="/devops/developer-automation">standard Vagrant box</a>, there are pre-commit hooks in place to prevent commits which violate Drupal coding standards. So hopefully this saves reviewers (and the author of the code) from the dispiriting experience of dozens of comments about indentation - much better to get the machine to do the nagging for you.</p>

<p>More importantly, you can set up a continuous integration server to make sure that the change isn’t breaking any tests. For example, whenever a developer pushes to our repos, it triggers Jenkins to run unit test builds on that branch. If your pull request has failed builds, it can’t be merged.</p>

<h3 id="check-yourself-before-you-wreck-yourself">Check yourself before you wreck yourself</h3>
<p>When (or even before) you create a pull request, look at the diff in the code review tool. Before your colleagues look at the change, you should be your own first reviewer. The context switch from editing to reviewing can help you to see the code you’ve written in a different light, and this can sometimes highlight errors or omissions.</p>

<h3 id="little-things-please-little-minds-while-bigger-fools-look-on">Little things please little minds, while bigger fools look on</h3>
<p>Ideally, pull requests are small - <a href="https://twitter.com/iamdevloper/status/397664295875805184">attention fatigue</a> means it’s almost impossible to thoroughly review large changes. When the diff view is a sea of red and green, it’s really hard to be sure that you’ve got your head around everything. In extreme cases in Stash, you’ll see “This pull request is too large to render. Showing the first 1000 files.” Seeing that message gives me the fear.</p>

<p>When a pull request is bigger than a couple of files, it can be a sign that you might not be splitting your tasks into small enough units. Sometimes it can help if developers create a feature branch, and branch off that for smaller tasks, so that when you come to do the big merge into the main branch, the code has already been reviewed in small chunks.</p>

<h3 id="dont-just-look-at-the-code">Don’t just look at the code</h3>
<p>If in doubt, check out the branch and test it yourself. It’s a really good habit to get into, especially for larger changes that may need a bit more consideration. It may seem time-consuming, but it’s much quicker to find problems <em>before</em> they get into a code branch that will be going live.</p>

<h3 id="its-good-to-talk">It’s good to talk</h3>
<p>Sometimes online comments aren’t the best way to communicate. Especially when changes are complex, or if a pull request needs a lot of work, talk them through together. Nobody wants to be on the receiving end of a big pile of negative comments. If it seems like someone needs a lot of guidance, maybe <a href="/development/pair-programming-budo">pair programming</a> would be a better approach.</p>

<h3 id="sharing-is-caring">Sharing is caring</h3>
<p>When you’re making a comment, don’t just tell your colleague that their code is bad. It’s really helpful to give examples of how it could be improved, or link to useful articles that will help people understand why you’re making that comment. For instance, when I’m reviewing CSS by junior developers, I very often find myself linking to the <a href="https://www.drupal.org/node/1887862">Drupal coding standards</a>, or an explanation of <a href="http://www.456bereastreet.com/archive/201004/whenever_you_use_hover_also_use_focus/">why hover styles should be accompanied by focus styles</a>.</p>

<h3 id="love-the-sinner-hate-the-sin">Love the sinner, hate the sin</h3>
<p>Some developers dread code reviews. I think we’re lucky in that the atmosphere within our team is very supportive, but I can imagine in some organisations a code review could feel like a trial by fire. It should go without saying, but code review comments should never get personal. The aim is to improve the quality of the team’s code, not prove that you’re cleverer than your colleagues.</p>

<p>The other thing to remember is that criticism isn’t just about pointing out things that are wrong. It can have a really beneficial effect on morale to make positive comments about something someone has done well.</p>

<h2 id="were-getting-there">We’re getting there</h2>
<p>Over the last few years, code reviews have really helped our team to improve and learn a lot, and we’ve come a long way towards a culture of quality, but there’s always room for improvement. There are more quality checks that we can automate, but top of my wish list would be to <a href="https://www.lullabot.com/articles/github-pull-request-builder-for-drupal">spin up a test environment for each pull request</a>. I’d also like to get more front-end quality checks automated, perhaps including performance metrics using a tool like <a href="https://github.com/gmetais/grunt-devperf">devperf</a>, or visual regression testing using <a href="https://github.com/BBC-News/wraith">Wraith</a> or <a href="http://garris.github.io/BackstopJS/">Backstop</a>.</p>

<p>But even if you don’t have robots doing all that clever stuff, if code review is part of your process, you’re going in the right direction. And besides, no matter how much you automate, you’ll still need someone to use their judgement.</p>
