Code Review Exercise

Goals

Background

There is no one right or wrong development workflow and approach to code review. In class we outlined an approach using Git feature branches and GitHub pull requests (PR). It is similar to this process (albeit without the interactive rebase at the end, we will merge via the GitHub interface.) Today we will practice the code review-portion of that process in class. Review the GitHub documentation for performing code review via a GitHub PR.

A code review is not intended to be an adversarial process. You and the reviewer - your teammate - share the same goal: making your application the best it can be. Review the following example guide for reviewing and having your code reviewed for considerations to make that process more effective and productive. I want to highlight some of their suggestions:

For the reviewer, recall that your role is to provide feedback and a second set of eyes, not serve as a “gatekeeper”. Start by understanding what this PR is trying to accomplish. Then look for opportunities to simplify the code (while still solving the problem), opportunities to setup the team for future success (e.g., that function/component may be useful elsewhere), error cases that maybe weren’t considered, and more. When suggesting improvements or possible error cases, start from the assumption that the author already considered those ideas and so you are trying to learn what they think about those topics, e.g., “What do you think about null values here?...”.

Our code reviews are generally not about style, and specifically not the aspects of style that are the purview of Prettier (i.e., this is not the place to litigate indentation approaches). All code should have been run through our styling and linting tools.

Practicing code review

First, one member of your group should click through to accept the assignment from GitHub Classroom. As part of that process they will create a group, e.g., “Group 1”, that others can join. Once one person has created the group and the shared repository, other group member should click through to GitHub classroom to accept the assignment but join the existing group. You should all be able to see the same shared repository.

In our typical workflow, we create a PR to merge a feature branch into main. Here we will do the opposite to create a PR from existing code in a way that works with GitHub classroom (this isn’t a process we need to emulate in the future).

One group member should do the following:

  1. Clone the repository create by GitHub classroom as usual. It should have two commits.
  2. Create a new branch named initial from the first commit by executing the following in a terminal:
    💻 git checkout -b initial f89ea44
    
  3. Push your newly created branch to GitHub, i.e., 💻 git push origin initial.
  4. In the response from GitHub you should see a link to create a PR. Follow that link. If not, you can use the “Pull Request” tab on the site for you GitHub repository.
  5. Create a PR to merge main into initial like shown below. This PR should contain the single commit with the ProductCard.

Your focus is the ProductCard component (and associated tests) implemented in src/components/ProductCard.js and src/components/ProductCard.test.js. ProductCard is intended for an e-commerce application. It shows relevant information, including discounts, to the shopper and enables them to add that item to their cart (if they are logged in and if it is available in the desired quantity). To keep the scope manageable for class, this feature is not completely implemented (i.e. there is a placeholder for a fetch, no styling, etc.) Focus your attention on the aspects that are implemented.

As a group (see note below), perform a code review via the GitHub PR, following the guidelines above. Provide your feedback as comments on specific lines in those two files (see the image below for the mechanics). There is nothing to be submitted and you don’t need or want to merge the PR; this is an in-class exercise in which we will discuss the feedback we provided as a class. There is not necessarily a “right” answer. Recall that our goal is to practice performing a thoughtful and productive code review.

As an example, we might include the following feedback in our review for line 57, the img element:

I am seeing a ESLint warning suggesting using next/image for the product image. What do you think? Should we make this switch? Or perhaps disable that rule here or generally?