Code reviews: What and Why?

Swaraj Rajpure
5 min readDec 12, 2021

Code reviews are the best! What is a code review? When you make some changes to a piece of code of a project (to introduce a new feature or to fix a bug or anything else) and wish to get that merged in the project, you create what is called a Pull Request and then the project maintainers will review your code before directly merging your changes. Why? Firstly, the security concern, to check if your code is (intentionally or unintentionally) leaking some sensitive information (like credentials/some other private data), or if your code has imperfect logic, missing some edge cases or if it has unnecessary code changes or if it can be improved in any way and so on. Don't worry if you are not able to relate, we'll see some examples and that'll help us get some clarity 🙂

As a contributor to the project, why should you care about the code reviews?

Well, nobody’s perfect and neither is the first version of our code 😛. We don’t wish to send some code with imperfect logic to be merged in the project we’re targeting, right? Even if the logic is correct, the code can be improved in many ways like improving the time complexity of some functions in some cases or improving the readability of the code (you’ll see this quite frequently in code reviews of beginner contributors) or making the code modular/decoupled. Code reviews teach us A LOT. There could be some easy way of doing something which we’ve complicated in our code, a code reviewer could point that out and we could get to learn about some new library or language API or algorithm altogether. This is “learning while building”.

There is always a limit to what we can learn in definite time. Code reviews help us leverage the experience of senior/experienced/even junior developers. Everyone comes with different experiences and there is something to learn from everyone out there. We’re writing the code with our experience and we’re improving the code with help of others’ experience, how awesome is that?! And again, this new experience gets added to our own experience as well 😉

At the same time, we have to understand that if someone is pointing out something to be improved in our code, that doesn’t mean we’re inferior to them or anything. We ourselves and the reviewers, we have the same goal in mind, making the project better and better. Improvising the code comes as a part of that. There’s nothing to be sad/ashamed about having changes requested on our code. In fact, we should take it as an opportunity to learn something new!

That was enough talk. Let’s take some code and try to understand what I meant by learning new things from code reviews. Let’s say there’s a use case that you have a string and you have to check if that string exists in an array of strings.

An obvious way would be to loop through the array and add equality checks, like so: (the following is a JavaScript snippet)

Simple code, nothing fancy going on here. Even if you don’t know JavaScript, you can make sense of what’s happening here. Now, there is nothing logically wrong in the approach, but it can simplified in terms of efforts taken here. This is a standard problem which we (developers) face on regular basis and JavaScript has solved it for us, by providing us with the .includes() array method. If I was reviewing this code, I'd have suggested you to read about this method and would have provided you the link to read about it (most probably MDN docs). For anyone new, this method returns us a boolean value if the array contains the field we are looking for 😉

Coming back to the code, the improved code would look like:

Much simpler, easier to understand, in my opinion. It also saves the developers’ time, by not having to re-invent the wheel. We might have not known about .includes() method earlier, but now we do and this is one learning which is now going to stay with us forever. "Today I learnt about a JavaScript method to check if an element exists in an array, which makes my life much simpler!" 😄

This is just one example, probably an over-simplification, but we don’t wish to get stuck in some highly technical stuff right now, so let’s move ahead. I hope the point is understood 🙂

Why should you care about code reviews as a reviewer?

Code reviews help the reviewer more than the person whose code is being reviewed. Let me explain how. There’s a limit to the features you’ll get to work on but there’s no limit to the knowledge that’s out there. Most of the times, you’ll get to learn only about those things which you require in your own feature. How will you learn/come to know about the other things which you haven’t got exposure of? CODE REVIEWS! Other people might be working on something different which uses some APIs/methods/libraries/techniques which you have never come across and you’ll see those being used in their code and that’s one awesome way to learn about a ton of things in very short period of time! 💥

For example, if you had never seen an array method called .includes() in JavaScript and you see that in a pull request, you immediate understand "hey, this is something new, let me google what it does". That right there, you've learnt something new! Again, an over-simplification, but I hope you get the idea!

By the way, we’re not even discussing about wrong code here 😂 (in terms of incorrect logic, security concerns etc). I’m keeping aside the responsibility part of the reviewer and focusing on the learning part 🙂

Now that we understand why code reviews are important, even as a beginner, how do we start? What things should we look out for, when doing code reviews? What should be our tone while doing code reviews? We’ll see all of this in the next blog! Stay tuned! 🚀

--

--

Swaraj Rajpure

Web Developer | Javascript | Open Source Contributor