Lesson 43 of 60 – Code Review
72%

Code Review

Code Review is the process of examining source code before or after it is integrated into a project. In GitHub, code review is commonly performed through Pull Requests.

Note: Code review helps developers find problems, understand changes, improve code quality, and discuss implementation decisions before changes are merged.

1. What is Code Review?

Code review means checking code written by another developer or checking your own changes before they become part of the main project.

Developer Writes Code
        ↓
Code Review
        ↓
Feedback
        ↓
Improvements
        ↓
Merge

2. Why is Code Review Important?

Code review provides an opportunity to inspect changes before they are integrated.

  • Find potential bugs
  • Improve code readability
  • Share knowledge
  • Check project conventions
  • Discuss design decisions
  • Improve maintainability
  • Identify missing tests

3. Code Review with Pull Requests

GitHub Pull Requests provide a central place for reviewing proposed changes.

Feature Branch
      ↓
Pull Request
      ↓
Code Review
      ↓
Feedback
      ↓
Update Code
      ↓
Approval
      ↓
Merge

4. Reviewer

A reviewer is a person who examines the proposed changes in a pull request.

A reviewer may inspect:

  • Changed files
  • Code logic
  • Tests
  • Security concerns
  • Performance
  • Readability

5. Pull Request Reviewers

A pull request can have one or more reviewers depending on the project's workflow.

Pull Request
      ↓
Reviewer 1
Reviewer 2
Reviewer 3
      ↓
Feedback

The repository's settings and team workflow determine how reviews are handled.

6. Reviewing Changed Files

Reviewers should examine the files changed by the pull request.

Modified:
    login.php
    dashboard.php
    style.css

Reviewer checks each change.

Reviewing the actual diff helps identify exactly what changed.

7. Reviewing the Diff

A diff shows the differences between versions of a file.

+ Added line
- Removed line
  Unchanged context

The reviewer can use the diff to understand the changes without reading the entire project from the beginning.

8. Reviewing Code Logic

Reviewers can check whether the code performs the intended task.

Input
  ↓
Validation
  ↓
Processing
  ↓
Output

The reviewer should consider whether the implementation handles normal and important edge cases correctly.

9. Checking Code Readability

Readable code is easier for other developers to understand and maintain.

Reviewers may look at:

  • Variable names
  • Function names
  • Code structure
  • Indentation
  • Comments
  • Unnecessary complexity

10. Checking Naming

Clear names make code easier to understand.

// Less descriptive
$x = 500;

// More descriptive
$totalFee = 500;

A reviewer can suggest clearer names when they improve understanding.

11. Checking Functions

Reviewers can check whether functions have clear responsibilities.

function calculateTotalFee($fee, $fine)
{
    return $fee + $fine;
}

Small and focused functions can make code easier to understand and test.

12. Checking Security

Security should be considered during code review.

Reviewers may look for issues such as:

  • Hard-coded passwords
  • Exposed API keys
  • Unsafe SQL queries
  • Missing input validation
  • Improper access control
  • Sensitive information in commits

13. Checking SQL Code

For applications using databases, reviewers should pay attention to how database queries are constructed.

// Prefer parameterized queries

$stmt = $pdo->prepare(
    "SELECT * FROM students WHERE id = ?"
);

$stmt->execute([$studentId]);

This approach helps avoid unsafe construction of SQL queries.

14. Checking Error Handling

Reviewers can check whether important errors are handled properly.

try {
    // Database operation
} catch (Exception $e) {
    // Handle error
}

Good error handling can make applications easier to debug and more reliable.

15. Checking Tests

A code review can include checking whether appropriate tests have been added or updated.

Code Change
    ↓
Run Tests
    ↓
Pass?
 ┌──┴──┐
Yes    No
 ↓      ↓
Review  Fix

16. Automated Checks

Repositories can automatically run checks when a pull request is opened or updated.

  • Unit tests
  • Build processes
  • Linters
  • Type checks
  • Security scans
  • Other project-specific checks

17. Review Comments

Reviewers can leave comments to ask questions or suggest improvements.

Reviewer:
Could this function be simplified?

Developer:
Yes. I will refactor it.

Comments help developers discuss specific parts of the proposed changes.

18. Suggesting Changes

A reviewer can suggest a specific modification when they see a better way to implement something.

Current:
return $a + $b + $c;

Suggestion:
return $total;

The exact review workflow depends on the repository and the changes being discussed.

19. Requesting Changes

When a change needs to be addressed before integration, a reviewer can request changes through the pull request review process.

Pull Request
     ↓
Review
     ↓
Changes Requested
     ↓
Developer Fixes Code
     ↓
New Commit
     ↓
Review Again

20. Approving a Pull Request

A reviewer can approve a pull request when the changes meet the project's review requirements.

Review
  ↓
Approved
  ↓
Required Checks
  ↓
Merge

Approval requirements can vary between repositories.

21. Updating Code After Review

If changes are requested, the developer can modify the code locally, commit the changes, and push them to the same source branch.

git add .
git commit -m "Address review comments"
git push

The existing pull request can then reflect the new commit.

22. Self Review Before Submission

Developers should review their own changes before asking others to review the pull request.

  • Read the changed code.
  • Check the diff.
  • Remove debugging code.
  • Check formatting.
  • Run tests.
  • Verify the pull request description.

23. Keeping Reviews Focused

A focused pull request is generally easier to review than a pull request containing many unrelated changes.

Good:
Add login validation

Avoid:
Login + payment + redesign +
database migration + unrelated fixes

Keeping related work together can make discussion clearer.

24. Respectful Code Review

Code review should focus on the code and the project requirements, not on attacking the person who wrote the code.

Prefer constructive comments such as:

"Could we simplify this function
to make it easier to maintain?"

Clear and respectful communication helps teams collaborate effectively.

25. Common Code Review Mistakes

  • Reviewing without understanding the purpose of the change.
  • Ignoring security issues.
  • Ignoring test results.
  • Making unrelated comments.
  • Not explaining important feedback.
  • Reviewing too many unrelated changes at once.
  • Ignoring project coding standards.
  • Approving changes without checking important files.

26. Code Review Checklist

  • Understand the purpose of the pull request.
  • Check the changed files.
  • Read the important code changes.
  • Check logic.
  • Check error handling.
  • Check security.
  • Check tests.
  • Check readability.
  • Check project conventions.
  • Review automated checks.

27. Example Code Review

Pull Request:
Add student registration

Reviewer checks:

1. Form validation
2. Database query
3. Duplicate student handling
4. Password handling
5. Error messages
6. Tests
7. Code readability

Result:
Changes requested

Developer fixes the issues
and pushes a new commit.

28. Complete Code Review Workflow

Developer
    ↓
Create Feature Branch
    ↓
Write Code
    ↓
Commit Changes
    ↓
Push Branch
    ↓
Create Pull Request
    ↓
Self Review
    ↓
Reviewer
    ↓
Comments / Suggestions
    ↓
Developer Updates Code
    ↓
Automated Checks
    ↓
Approval
    ↓
Merge

29. Best Practices for Code Review

  • Keep pull requests reasonably focused.
  • Explain the purpose of the change.
  • Review the diff carefully.
  • Use clear and constructive comments.
  • Check tests and automated checks.
  • Look for security problems.
  • Follow project conventions.
  • Ask questions when the code is unclear.
  • Resolve important review discussions before merging.
  • Keep the review focused on the proposed change.

30. Summary of Code Review

Code review is an important part of collaborative software development. GitHub Pull Requests provide a convenient place for developers to inspect changes, discuss code, run checks, request updates, and approve proposed changes.

Write Code
    ↓
Create Pull Request
    ↓
Review Changes
    ↓
Discuss
    ↓
Fix Problems
    ↓
Run Tests
    ↓
Approve
    ↓
Merge

A good code review focuses on correctness, security, readability, maintainability, testing, and the requirements of the project.

📌 Key Points

  • Code review means examining proposed code changes.
  • GitHub Pull Requests are commonly used for code review.
  • Reviewers inspect changed files and differences.
  • Code review can identify potential bugs and security problems.
  • Reviewers can leave comments and suggestions.
  • Reviewers can request changes when necessary.
  • Developers can push new commits to address review feedback.
  • Automated tests and other checks can support the review process.
  • Readable and focused code is easier to review.
  • Self-review should be performed before requesting review.
  • Code review should focus on the code and project requirements.
  • Approval and merge requirements depend on the repository workflow.

🧠 Quick Quiz

Question: What is the main purpose of code review?