This tutorial aims to guide you through the process of conducting effective code reviews on GitHub. By the end of this guide, you will learn how to:
Before we start, you should have a basic understanding of Git & GitHub and be familiar with creating repositories, branching, and creating pull requests.
Code review is a systematic examination of computer source code intended to find and fix mistakes overlooked in the initial development phase, improving overall code quality. It's a crucial practice in successful software development as it encourages team collaboration and knowledge sharing.
On GitHub, code reviews are done through pull requests. A pull request is a way to submit contributions to an existing project. It proposes changes to the code, requests review from others, and enables collaborative discussion about the proposed changes.
Here are the steps to perform a code review on GitHub:
git checkout -b new-branch
# edit files
git commit -a -m "descriptive message about changes"
git push origin new-branch
Review the Changes: As a reviewer, go to the 'Pull Requests' tab and open the PR. Review the changes by going through the 'Files changed' tab.
Leave Comments: If you find any issues or have any suggestions, you can comment directly on the code by clicking the '+' button next to the line of code.
Approve or Request Changes: Once you have reviewed all the changes, you can either approve the pull request or request further changes.
Merge the Pull Request: Once all reviewers have approved the changes, the pull request can be merged.
Let's consider a simple code snippet to understand how to comment and request changes.
def add(a, b):
return a+b
Let's say you want to suggest the author to add type hinting to the function parameters and return type. You can comment as follows:
def add(a: int, b: int) -> int:
# This function takes two integers as parameters and returns their sum.
return a + b
In this tutorial, we have learned the importance of code reviews and how to conduct them on GitHub. We've also learned how to comment on pull requests and request changes.
Exercise 1: Create a new branch, make some changes, and initiate a pull request.
Exercise 2: Review the changes made in Exercise 1, leave comments, and request changes.
Exercise 3: Address the comments from Exercise 2, make the necessary changes, and merge the pull request.
Each of these exercises will give you practical experience with the code review process. Keep practicing and happy reviewing!