Skip to content

mawrkus/pull-request-review-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 

Repository files navigation

A practical guide for better, faster code reviews

"A code review is a dialogue, but it is also a critique of a person’s work, and both reviewer and author should bear in mind the psychology of ownership and criticism. Some of the most valuable things that come up during code review are things which different developers can reasonably disagree about, and discussing such things is exactly how we learn and innovate." -- Alexandra Hill, The Art of Giving and Receiving Code Reviews (Gracefully)

Disclaimer

All the information provided has been compiled & adapted from the references cited at the end of the document. The guidelines are illustrated by my own examples, fruit of my personal experience writing and reviewing code. Many thanks to all of the sources of information & contributors.

πŸ“” Considerations

  • A code review is a dialogue, and as such, it should always be done in a respectful and constructive way.
  • Many programming decisions are opinions. A code review opens a conversation about trade-offs in order to reach a resolution that everyone is satisfied with.
  • A code review is a form of asynchronous communication, so a lot of context (like body language and the opportunity for back-and-forth) is lost.
  • For both authors and reviewers, a code review is a collaborative tool for knowledge spreading and for code quality improvement.

πŸ“” Guidelines

  1. For both authors and reviewers
  1. For the authors
  1. For the reviewers
  1. Interactions
  2. Profiles
  3. Resources
  4. Contributors

🀝 For both authors and reviewers

Be respectful / Ask - Explain - Suggest

Always be considerate and respectful, specifically:

  • don't use the imperative
  • don't judge
  • don't blame (don't even think about cursing)
  • don't be arrogant
  • don't use sarcasm.

Instead:

  • ask questions, assuming that everyone is intelligent and well-meaning
  • explain your point of view in an explicit manner, even if it's a bit more verbose
  • suggest.

Respect and humility remove all frictions. Clarity dispels all doubts about your intentions. Suggesting creates the conditions for improvement.

At the end, the goal is to have better, more meaningful conversations and to save time and energy.

β€’ Go to top β€’

Stop the "reply" hemorrhage / Offer to talk in person

Many replies to a comment might be the symptom of miscommunication, misunderstanding or lack of knowledge. They can create a lot of noise for everyone participating in the review, as well as frustration.

Should it be the case, offer to talk in person to clarify the topic under discussion, to agree on a solution and/or to pair on any changes suggested.

Once done, post a concise comment to summarize the conversation and to share the agreement with everyone.

β€’ Go to top β€’

Offer encouragement & appreciation / Give positive feedback

Code reviews often just focus on mistakes, but they should also offer encouragement and appreciation for good practices.

Whether you learned something thanks to a comment written by one of your peers, or when you read an elegant piece of code, or even just to congratulate the good work, say: "thank you!", "great!", "nice!".

It makes a huge difference to receive positive feedback!

β€’ Go to top β€’

Agree on a coding style / Automate

Conversations about coding style are often long and unproductive. After all, it's a matter of personal taste and everyone has their own preference.

This is why it's important to find a coding style that your team can adhere to and to use automation tools to ensure that it is being followed (code formatters, linters). By removing the personal tastes from the review, everyone can focus on what's most important: code quality and best practices.

β€’ Go to top β€’

πŸ–‹οΈ For the authors

And the first reviewer is... you! / Assess your code

After creating the pull request (PR) and before inviting anyone, take some time to assess your own code. Reviewing your changes outside of your IDE could help you spotting inconsistencies, mistakes, missing parts, or even finding easier ways to solve the problem at hand.

β€’ Go to top β€’

Be proactive / Provide context

If the PR is complex, write a general comment to explain its purpose, to give an overview of the changes being made and to share any relevant information about your choices. It will help your reviewers to dive in the review without effort, which will surely result in less comments and a faster review.

If there are many changes, highlight the most important parts.

β€’ Go to top β€’

Wait for the green light

Save time for everyone: if your CI/CD pipeline has a test stage, wait until all tests have passed before inviting anyone to the review. You don't want the first comment to be "The build is failing :/".

β€’ Go to top β€’

Choose your audience

Take some time to choose your reviewers. Prior to inviting them, ask yourself:

  • Who should I give visibility of the changes being made? Who could be impacted by those changes?
  • Who has time to review my code?
  • Who is qualified to give me the best feedback?

β€’ Go to top β€’

Choose the right number of reviewers

Be inclusive, but not too much. Having a large number of reviewers might lead to:

  • too many comments/suggestions going into too many different directions
  • too few comments, when everyone thinks someone will take care of the review.

To prevent this, you can start with a few core reviewers to receive the main feedback, then add more people, to give them visibility.

β€’ Go to top β€’

Keep it small / Divide and conquer

Small successive PRs, each one focused on a single change, will be reviewed more quickly and more thoroughly.

Try to organize your work in order to be able to break a complex task into a succession of simpler ones. Value your reviewers time, they will thank you and you will benefit of a faster and more valuable feedback.

10 lines of code = 10 issues.

500 lines of code = "looks fine."

Code reviews.

(https://twitter.com/iamdevloper/status/397664295875805184)

β€’ Go to top β€’

Rome wasn't built in a day / Create new tasks

If the need for a big refactoring or substantial changes arise during the review, create a new task and add a link to it in the review.

Remember that, the longer a review takes:

  • the bigger the number of changes
  • the higher the risks of merge conflicts
  • the greater the probability of demotivating everyone involved in the review.

β€’ Go to top β€’

Don't take it personally / Keep an open mind

One of the goals of a code review is to maintain the quality of the code base. When a reviewer provides a critique of your code, think of it as their attempt to help you, rather than as a personal attack on you or your abilities.

If you feel offended, ask for explanations before reacting, take some distance, assume the best intention from the reviewer and try to evaluate your code critically, as if someone else had written it.

Always keep in mind that it's not about you, it's about a better code, a better project built with your colleagues.

β€’ Go to top β€’

Clarify the code first / Help future developers

If a reviewer says that they don't understand something in your code, your first response should be to clarify the code itself. If the code can't be clarified, add a code comment that explains why the code is there. If a comment seems pointless, only then should your response be an explanation in the code review.

If a reviewer doesn't understand some piece of your code, other future readers likely won't understand it either. Writing a response in the code review doesn't help future code readers, but clarifying your code or adding code comments does help them.

β€’ Go to top β€’

Leave this world a little better than you found it / The boy scout rule

Opening a PR is a chance to improve the existing code base: have you noticed a missing test? A method that is too long? Some naming inconsistencies?

You don't have to make a big change or to make the code looks perfect, just a little bit better than before!

If you find a mess, try to clean it up regardless of who might have made it. By doing this, you intentionally improve the code base for all the developers, helping the project to get better and better as it evolves.

β€’ Go to top β€’

Answer all comments / Write the end of the story

Before closing the review, answer all the comments. Not only as matter of respect for you reviewers (after all, they are spending time and energy as well) but also because a review is a dialogue and, as such, it should have clear a conclusion.

Moreover, it can be helpful as a reference in the future.

β€’ Go to top β€’

Practice mindfulness / Love

Take a few deep breaths... Repeat a mantra that is focused on gratitude and kind speech, for instance:

Code can travel thousands of miles and affect many people.

I vow to write code mindfully and lovingly.

May this code create mutual understanding and peace.

Find your own mantras and spread the love!

πŸ‘“ For the reviewers

Empathize / The other person is you

Always consider the potential impact of your words, not just their original intent. Ask yourself how you would react to the words you're about to post. Take some time to write comments that are not only helpful but also kind, the other person is you ;)

β€’ Go to top β€’

Avoid selective ownership / It's our code!

Instead of using words like: "your", "mine", "not mine", which could be interpreted as finger-pointing, prefer the more inclusive "our" and "we" ones.

Rephrase any comment to be more collaborative, showing that you share ownership of the code being modified.

β€’ Go to top β€’

Provide references

Don't assume that the author knows exactly what you are talking about. To support your comment/suggestion, add:

  • a code sample
  • a link to relevant documentation
  • a link to a previous review
  • a link to a blog post on the topic
  • a link to a GitHub repository.

Sharing some references can also benefit the other reviewers.

β€’ Go to top β€’

Seek the author's perspective / We always learn

If something bothers you in the code you're reviewing, try to understand the reasons behind the author's choices.

Keep in mind that, often, the author is closer to the code than you are, and so they might really have a better insight about certain aspects of it.

β€’ Go to top β€’

Don't be a gatekeeper / Improvements have a threshold

Do we always need to push "perfect" code to production? Maybe a good increment in quality is enough?

Don't place the author in a never-ending cycle of requests for changes. Instead, try to strike a balance between code quality, performance, and developer happiness.

Remember that you are in the review to provide feedback, not to be a gatekeeper.

β€’ Go to top β€’

Ensure the best review quality

If don’t feel qualified to do some part of the review, don't hesitate to invite a developer who you think is qualified. Ensure that the best possible feedback is always given to the author.

β€’ Go to top β€’

What to look for

In general, examine every line of code and make sure you understand what all the code is doing. In particular, take into consideration these specific points:

  • Design: Do the interactions of the various pieces of code make sense? Does this change integrate well with the rest of the code base? Is now a good time to add this functionality?
  • Functionality: Does this code do what the author intended? Is what the author intended good for the users of this code (end-users AND developers)?
  • Complexity: Can the code be understood quickly? Will developers be likely to introduce bugs when they try to call or modify this code? Is it over-engineered? Is it trying to solve a problem that the author speculates might need to be solved in the future instead of solving the problem they know needs to be solved now?
  • Tests: Does the code have appropriate unit tests? Are they correct, sensible and useful? Review them first, they will help you understand the intent of the author very quickly (could be just by looking at their names).
  • Naming: Did the author pick good names for everything? Long enough to fully communicate what the item is or does, without being so long that it becomes hard to read?
  • Comments: Did the author wrote clear and useful comments? Do they explain the "why?" or the "what" instead of the "how"?
  • Consistency: Does the code follow the existing agreements about style, naming, files organization, etc.?
  • Documentation: If the code changes how users build, test, interact with, or release code, check that the relevant documentation is also updated, deleted or added.

β€’ Go to top β€’

Practice mindfulness / Gratitude

Take a few deep breaths... Repeat a mantra that is focused on gratitude and kind speech, for instance:

Reviewing this code,

I am grateful to those that wrote it

and for the tools that were used to write it.

May this code bring health, peace, and well-being.

Find your own mantras and spread the love!

β€’ Go to top β€’

πŸ“” Interactions

In the following interactions, can you spot which guideline applies?

1

Alan: Please don't do this, you are actually creating a layer that the application does not require, and don't say that is temporary, because it isn't.

Billy: I don't understand.

Alan: That you don't need the copy-paste that you are doing.

Billy: Let’s talk, your comment does not help me understand.

2

Jamie: Do we really need this? I don’t see the gain.

Billy: It’s a polyfill used in lazyLoad.js

Jamie: Yes I got that, but why not importing there instead of here? I know that the import is not executed always in lazyload.js, but I see it weird.. Maybe a require() instead?

Billy: Good point, I don’t know actually! I’ll make some tests.

3

Alan: Maybe it is time to stop the tradition of providing all the arguments as props.

4

Alan: I'm not sure if it's a good idea to return to this, it's a screenshot from project X.

Billy: It’s not human-friendly indeed. But do we need human-friendly?

Alan: Yes.

Jamie: Do you know why?

5

Evan: Do you want to keep this line?

6

Alan: It's not consistent this tracking, because it directly depends on the provided options.

Billy: Ok, what do you suggest we should do instead?

7

Alan: I can imagine why you are putting this fix here now, but our mission is to make the templates simpler, and the only way to accomplish that is to move all the business logic related to the page to the model. It's a bit more complicated, but this is the way that we need to follow now. There are many benefits of doing that, and one of them is to have tests. If you need some help about it, drop me a message.

β€’ Go to top β€’

πŸ“” Profiles

  • The terrorist: destroys every piece of code one by one.
  • The picky: will approve the PR with great difficulty, because there's always a little something to improve.
  • The bot: will always approve the PR without any comment. For some, their best friend, for others, the most annoying reviewer.
  • The vague: can be found by following the trail of "What do you mean?" comments.
  • The bull: answers comments by committing new changes.
  • The ghost: was in the PR, or not. Have you seen them?
  • The purist: does not live in a world of trade-offs and concessions. Do they even create PRs?
  • The novelist: loves to explain in great lengths the history, the context and all the nuances that you should definitely know before merging the PR.

β€’ Go to top β€’

πŸ“” Resources

β€’ Go to top β€’

🫢🏾 Contributors

About

πŸ§˜πŸ’»πŸ§˜ Guidelines for better, faster pull request reviews

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published