☕️ 6 min read

Conquering Code Reviews: A Developer’s Guide to Giving and Receiving Feedback Like a Pro

avatar
Milad E. Fahmy
@miladezzat12
Conquering Code Reviews: A Developer’s Guide to Giving and Receiving Feedback Like a Pro

Ah, the noble art of code reviews, a battlefield where the warriors of syntax and the guardians of functionality clash and collaborate to forge software of the finest caliber. If you've ever found yourself staring at a pull request, wondering how to navigate the perilous waters of critique without capsizing the ship of camaraderie, fear not. As Milad, a seasoned developer who's survived more code reviews than a cat has lives, I'm here to guide you through the gauntlet with wit, wisdom, and a sprinkle of humor.

Step 1: Setting the Stage for Constructive Feedback

Before diving into the nitty-gritty of code reviews, let's set the stage. A successful code review starts not with the code, but with the culture. A culture that celebrates feedback as a tool for growth, rather than a weapon for criticism, paves the way for constructive, insightful reviews. Remember, the goal is to improve the code, not to prove who's the smartest person in the (virtual) room.

The Environment Checklist:

  1. Psychological Safety: Ensure everyone feels safe to express their thoughts and make mistakes.
  2. Clarity on Goals: Be clear on what you're trying to achieve with the code review. Bug squashing? Performance improvements? All of the above?
  3. Tools of the Trade: Use tools that facilitate easy commenting, discussions, and integrations with your code repository. Platforms like GitHub, GitLab, and Bitbucket have robust code review features.

Step 2: The Developer's Checklist for Submitting Code for Review

Before you unleash your code into the wild, give it a good grooming. A well-prepared code submission not only makes the review process smoother but also shows respect for your reviewer's time.

The Submission Checklist:

  1. Self-Review: Start with a self-review. If Milad can catch his own silly mistakes, so can you. Run your code, test it, and look for obvious errors.
  2. Descriptive Commit Messages: Each commit should tell a story. Instead of "fixed stuff", how about "Improved query performance by optimizing indexing"? Be descriptive.
  3. Break It Down: No one likes reviewing a monolithic chunk of code. Break down your changes into manageable, logical pieces.
// Bad: A giant blob of confusion
function doEverything() {
  // Imagine 500 lines of spaghetti code here
}

// Good: Sweet, digestible functions
function fetchData() {
  // Fetch data logic
}

function processData(data) {
  // Process data logic
}

function displayData(processedData) {
  // Display data logic
}

Step 3: Crafting Feedback That Empowers - A Reviewer's Perspective

Giving feedback is an art. The goal is to empower your fellow developer, not to send them into the depths of despair.

The Empowerment Checklist:

  1. Be Specific and Actionable: Instead of saying, "This code is confusing," try, "This function could be clearer. What if we refactor it like this?"
// Suggestion example
// Before:
function processData(data) {
  // Confusing magic happens
}

// After:
function processData(data) {
  // Clear, commented steps with a sprinkle of magic
}
  1. Ask, Don't Tell: Encourage dialogue. "What was your reason for implementing this logic in such a way?" is more inviting than, "This logic is wrong."
  2. Provide Constructive Feedback Respectfully: Highlight good practices and constructive feedback in comments, ensuring any criticism is given constructively and with respect. This fosters an environment where the team can learn from each review, benefiting from shared knowledge.

Step 4: Navigating Feedback - Receiving and Integrating Critiques Gracefully

Receiving feedback can be as daunting as giving it. Here's how to do it with grace.

The Graceful Acceptance Checklist:

  1. Don't Take It Personally: Remember, the feedback is about the code, not about you as a person or developer.
  2. Clarify and Understand: If feedback isn't clear, ask for clarification. Understand the "why" behind the feedback.
  3. Iterate and Improve: Use the feedback to make your code better. Refactor, rewrite, and thank your reviewer for their insights.
// Example of iterating based on feedback
// Before feedback:
function validateInput(input) {
  // Assume the input is always correct
  return true
}

// After feedback, for better validation and error handling:
function validateInput(input) {
  // This check covers both `null` and `undefined` values, ensuring robust input validation
  if (input == null || input.trim() === '') {
    throw new Error('Input cannot be empty')
  }
  // Additional validation logic here
  return true
}

Conclusion: Fostering a Culture of Continuous Improvement Through Code Reviews

Code reviews are more than just a step in the development process; they're an opportunity to foster a culture of continuous improvement, knowledge sharing, and mutual respect. By approaching reviews with the right mindset, preparing thoroughly, giving feedback that empowers, and receiving critiques with grace, we can all grow as developers and as a team.

Remember, the ultimate goal of code reviews is not just to improve the code but to build a team that's continually learning, adapting, and thriving together. So, the next time you're gearing up for a code review, take a moment to smile, breathe, and dive in with the tips and tricks you've learned here. Happy reviewing!