29
How to review Angular code
Original cover photo by Alvaro Reyes on Unsplash.
One of the most important responsibilities of a senior developer is reviewing code written by their fellow developers; if anything, there are lots of teams that don't even require one to be a senior developer to be able to review pull requests and code written by others in general (everyone can review anyone). In this article, we are going to explore common approaches and problems that are related to reviewing pull requests in Angular projects and answer several important questions.
So, let’s get started
Before we begin discussing approaches, let’s gloss over some stuff that should not be a part of a pull request review process
Let’s now discuss some human stuff.
An important thing to remember (always, not only in code review) is that people you interact with are human beings. Remember to do the following things:
Array.map
instead of Array.forEach
”, but rather write “can we please change Array.forEach
to Array.map
?”If you follow the guideline only in those two first paragraphs, you already will be a reviewer who does not cause problems; that is great. Let’s now try to become a reviewer that is very effective at the job!
Let’s start with noticing obvious and common pitfalls that people allow into their code, and become efficient at finding those:
Now, let’s dive into what is specific to reviewing Angular codebases
Angular comes with its own list of bad and best practices (some of which are admittedly opinionated). Before you read this part of the guide, you can familiarize yourself with those using my following articles: Angular Bad Practices, Angular Bad Practices Revisited and Angular Best Practices. Also, definitely read and strictly follow the Angular Coding Style Guide. If you have even more time, you can watch my discussion with Santosh Yadav on his YouTube channel.
After exploring those, we can now start listing important things to pay attention to:
extends
keyword, examine if that is the case; if so, propose to refactor the class to use object composition/dependency injection instead of inheritance (remember, inheritance is an is-a relationship, not a has-a one)Most Angular codebases make at least some use of RxJS and stream based approaches; be careful to notice bad patterns, unnecessarily complicated code, and wrong usage of operators. Here are some things to pay attention to:
Observables
, but sometimes there are situations it is necessary, but if you have more than one subscription, be careful to examine all of them to see if some can be refactored to just use the async pipeObservable
pipe
- usually lots of operators are not a problem, but sometimes they can introduce unnecessary complexity, and some operators have alternatives that do the same thing as a combination of 2Observable
stream - be careful to notice any occurrences of the “this” keyword inside the operator callbacks, as usually this is not something to be encouraged; when noticing such instances, examine thoroughly why the component state was referenced and if/how it can be refactored. One way is to see if the properties changed inside the Observable
pipeline are being displayed in the template; if so, use the async
pipe. In general, avoid modifying to the this
context of a component in Observables, unless it is by calling a third-party imperative function (like FormControl.disable
, for example)Subjects
- Subjects are often used to transport data from one part of application to another, and again can be abused to introduce unnecessary complexity into a system. Sometimes usage of a Subject can be discarded completelyReviewing someone else’s code is often a challenging task, and requires both coding and social skills. Being attentive to details, clear with your comments and rigorous when upholding accepted practices is of huge importance. Hopefully, with this guide, you can become more efficient at reviewing code and finding issues