Code review is the process of having peer developers examine code to find mistakes, maintain style guidelines, and discover more elegant solutions, resulting in higher quality software and broader team involvement.
Benefits include catching errors and vulnerabilities early, spreading knowledge across the team, encouraging constructive discussion, distributing responsibility, and helping junior developers learn from senior peers.
Recommended practices include keeping PRs focused on a single goal, writing meaningful commit messages and titles, staying under 500 lines of changes, using linting and automated tests, keeping related code close together, using trailing commas in multiline literals, and following a consistent code style pattern.
Reviewers and authors should not take comments personally, since feedback is meant to improve overall product quality.
Yes, you read it right. I took the liberty to adapt Daft Punk’s song title to talk about code review. As I write this, I’m wondering if it is going to pass the thorough examination of the chief editor, but I like how the title sounds (and it really describes how a Pull Request should be). And you see, even this harmless piece of text is going over a rvesoin revision process before you can have the chance to be struck by my insights, so why shouldn’t we do the same with our code?
Code review is the process of having a peer developer reading your code after it’s done or during development time (pair-programming). The main goals are to find overlooked mistakes, guarantee the maintenance of the code style guidelines and discover more elegant ways to solve the same problem, taking into account scalability and performance, for instance. The result is software with better quality and wider involvement of the development team. Having your code inspected by someone else has many positive impacts:
Find errors, possible vulnerabilities and under-performing code before it reaches the final user (or even before it reaches QA, depending on your process).
Spread knowledge among all participants, making sure there are at least two people that know what’s going on with that chunk of code.
Encourage constructive discussion with other developers: when people dive deeper into the code as a group, they tend to write better code as a group.
Distribute responsibility and ownership. This way, nobody has to feel the pressure of failure on their own and everybody takes credit when it succeeds.
It’s an excellent way for junior developers to learn with more seasoned professionals.
Despite all these advantages, a lot of developers still shiver when they are assigned to a Pull Request. Code review is many times taken as unnecessary, inefficient and deadly boring. Don’t get me wrong: I understand this is sometimes the case. What I want to point out is that the problem usually isn’t the code review itself, but the process involved. Picture the following scenario:
You see a new Pull Request assigned to you. At first, you have no idea what’s it all about. The title reads: “Improvements and refactor”. You click on the link and see an ocean of green and red marks: four thousand additions, a thousand deletions. You don’t freak out, start full gas: “comment it, scroll it, improve it, code it” (read with robotic voice). Soon you are bored, comments are less and less frequent, and the code review loses its purpose.
This is not because the revision process doesn’t work at all, it’s simply because it’s being done the wrong way. Here are some principles we try to follow at our best here at Cheesecake Labs to improve code review quality:
Keep your PR focused on its goal: if you just want to fix a typo, that’s all you’re going to do. Don’t take the chance to do small refactorings here and there, they are a big source of distraction for those reviewing your code.
Write meaningful commit messages and PR titles: give people some context on what are you trying to achieve with your code.
Try your best to keep it under 500 lines between additions and deletions: less lines of code means less time reviewing it (and a more concentrated reviewer as a result).
Use linting and automated tests: the reviewer shouldn’t waste time with what the computer could have done.
Keep related code as close as possible: if two files have related code – let’s say the view and the controller – save them close to each other, in the same directory if possible. This way they are going to appear one right after the other on the code diff.
Keep the trailing comma in multiline literals (arrays, objects, dictionaries): it may sound small, but it can save a considerable amount of time with merge conflicts. As a plus, it appears as a one-line change on versioning diff, instead of two.
Follow a code style pattern: our brain is trained to understand patterns. If you always structure imports, docstrings, breaks and naming the same way, your code will be less tiring and easier to read.
Last but not least: don’t take things personally. Unless someone is really rude with comments, everything is annotated to improve the product’s overall quality. You can still be friends and have a beer together later.
Here at Cheesecake Labs we take code review as a core part of our culture. We use it to have awesome conversations among devs, learn new technologies with more experienced peers, let others know how we solve problems and most importantly: deliver great software as a team.
By the way, want to learn Pull Request submission best practices? Check out this post that my friend Iacami wrote about it: Git Etiquette: Submitting a Pull Request.
Code review is the process of having a peer developer read your code after it's done or during development time (pair-programming). The main goals are to find overlooked mistakes, guarantee the maintenance of code style guidelines, and discover more elegant ways to solve the same problem, taking into account scalability and performance.
What are the benefits of having code reviewed?
Code review helps find errors, possible vulnerabilities, and under-performing code before it reaches the final user or QA. It spreads knowledge among participants, encourages constructive discussion, distributes responsibility and ownership, and serves as an excellent way for junior developers to learn from more seasoned professionals.
Why do some developers find code review boring or inefficient?
The problem usually isn't the code review itself, but the process involved. For example, receiving a Pull Request with a vague title like 'Improvements and refactor' containing thousands of additions and deletions leads to reviewers becoming bored and making fewer meaningful comments.
What principles can improve code review quality?
Keep your PR focused on its goal, write meaningful commit messages and PR titles, keep PRs under 500 lines between additions and deletions, use linting and automated tests, keep related code as close as possible, keep trailing commas in multiline literals to reduce merge conflicts, follow a code style pattern, and don't take comments personally.
Why should PRs be kept under 500 lines?
Fewer lines of code means less time reviewing it, which results in a more concentrated reviewer.
Settled down from travelling to build some good applications. Feels comfortable developing on both ends, though lately tends to the front-side of the moon.