loading...

How to do a better code review?

paharihacker profile image Rajesh Dhiman ・1 min read

We recently started doing a code review of every PR that our team members make.

Most of the time I worry about commenting on something and requesting changes from them. Because in my head I am thinking what if this sounds rude to them or what if I am wrong, or what if they don't feel good about my comments?

Have you witnessed a similar situation? How do you handle that?

Discussion

markdown guide
 

I have a few personal guidelines I try to follow when reviewing pull requests.

  • Do not comment on style matters that we have not agreed upon as a team. Yeah, I like single quotation marks and they have used double quotation marks here, but we haven't agreed on either so let it go. If it really bothers you, start a discussion with the team about code style instead, but pull requests are not the place for that.
  • Try to understand the reasoning behind decisions that I don't agree with. For instance, instead of saying it's bogus to create a class for this logic when it could easily have been just a function, I'd say is there any reason to use a class here instead of a simple function?. If I don't find the response satisfactory I can always start a discussion later, but more often than not either I missed something that justifies the design, or the author will be like oh yeah, hadn't thought of that, I'll change it.
  • When asking for changes, offer code suggestions. I'd say something like this logic is repeated in these three places, we could create this higher order function code example and then compose it in these places like that another code example. Then the author will have a clearer idea of what I mean. Plus, if they are lazy they can just copy paste my suggestions.
  • If something is not critical but I think can be done better, offer it as a suggestion for the future without requesting a change in the current pull request.
  • Make positive comments. I can't stress this enough, but if you only take one thing from my post, take this. Especially if you are just starting with pull requests or as a team. If you learn something you didn't know, see a smart trick, the author applied something you taught them previously, or simply think a piece is particularly elegant, write a comment to tell them. You can hardly show too much appreciation to your colleagues. Showing genuine appreciation goes a long way to create trust within the team, and it's easier to take criticism in a part of the code if you're shown appreciation in another part.

The bottom line is try to make sure that the code is good enough, and that you all are learning in the process, not try to make it as if you would have written it.

 

Hi Avalander,

Thanks a lot for the suggestions. These are very helpful. I'll follow them all.

 

Here is my experience.

A good approach is not requesting, only suggesting changes, and pointing out actual requests i.e. where the current situation is not acceptable. Always tell why so that the PR creator can work out a solution.

Also, it's important to know what is (your) preference/opinion vs. an actual issue with a code (bug, not clean enough etc.).

And most importantly, don't commit into the code unless you want to drive people nuts. :D

I think that if you're unsure then ask instead of stating things, and suggest instead of requesting. Assertive communication goes a long way, and every PR submitter needs to get used to getting their code reviewed.

 
 

Hi Rajesh,

The statement "we recently started..." suggests there is a current driver for the code reviews - do you know what that driver is? It's likely to be "something something quality", but it may not be... I've seen mandatory reviews of PRs added for other reasons (e.g., trying to share knowledge of work being done, rather than critiquing it). I would make sure everyone understands the purpose of the code reviews first.

Code reviews can be a very uncomfortable situation, especially if there's no existing relationship between the people beforehand. I would say they can only be as successful as the level of trust within the team: low trust -> poor result, high trust -> good result.

In the meantime, I think following the "treat others as you would expect to be treated" and you'll be on the right path.

 

Hi Andy

Thank you, This is an excellent advice. I'll follow this for sure.

 

I have shared a post exactly related to this , please take a look into it and leave feedback.

dev.to/divakarkumar/how-to-become-...

dev.to/divakarkumar/how-developer-...