Skip to Content

Code Duplication

Let’s look at how to suggest improvements for code duplication in code reviews. We’ll show you a simple example and explain why the feedback matters.

The Scenario

A developer has written code to validate user input in multiple places. The code works but has duplicated validation logic.

Less Helpful Approach

userValidation.ts
// Before function validateEmail(email: string) { if (!email || !email.includes('@')) { throw new Error('Invalid email') } } function validateUserEmail(email: string) { if (!email || !email.includes('@')) { throw new Error('Invalid email') } }

Less Helpful:

Review Comment:

“This code is duplicated.”

Why This Isn’t Helpful:

  • It Doesn’t explain why duplication is a problem
  • Doesn’t suggest how to fix it
  • Feels critical without being constructive
  • It Doesn’t explain the benefits of reducing duplication

More Helpful Approach

userValidation.ts
// After function validateEmail(email: string) { if (!email || !email.includes('@')) { throw new Error('Invalid email') } } function validateUserEmail(email: string) { validateEmail(email) // Reuse the validation logic }

More Helpful:

Review Comment:

“Great work on the email validation! Reusing the existing validation function helps maintain consistency and makes future updates easier. This follows the DRY (Don’t Repeat Yourself) principle.”

Why This Is Better:

  1. Acknowledges the working code
  2. Explains the benefits of reducing duplication
  3. It Points out specific improvements
  4. Uses friendly, encouraging language

Understanding the Improvements

1. Code Maintenance

  • Single source of truth for validation
  • Easier to update validation rules
  • Consistent validation across the codebase

2. Bug Prevention

  • Less chance of inconsistencies
  • Easier to test
  • Better error handling

3. Code Quality

  • More maintainable code
  • Better organization
  • Clearer responsibility

Tips for Handling Code Duplication

  1. Start with Something Positive

    • Acknowledge the working code
    • Show you understand the current approach
    • Example: “Great work on the email validation!”
  2. Explain the Benefits

    • Why reduce duplication?
    • How does it help?
    • Example: “This makes future updates easier”
  3. Be Specific

    • Suggest concrete improvements
    • Explain why they matter
    • Example: “Reusing the validation function helps maintain consistency”
  4. Keep It Friendly

    • Focus on improvement, not criticism
    • Use encouraging language
    • Example: “Thanks for considering code reuse!”

Common Pitfalls to Avoid

  1. Being Too Critical

    • ❌ “This is duplicated code. Fix it.”
    • ✅ “Good work! Here’s how we can make it more maintainable.”
  2. Not Explaining Why

    • ❌ “Use the existing function.”
    • ✅ “Using the existing function helps maintain consistency.”
  3. Ignoring the Positive

    • ❌ “This needs to be refactored.”
    • ✅ “Great work! Here’s how we can make it even more maintainable.”

Remember: The goal is to help your teammate write better code while maintaining a positive and collaborative environment!

Last updated on