Skip to Content

Duplications

Let’s look at how to address 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.

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

PR Comment

Choose the comment that you think is the most constructive and helpful.

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

Click here to learn more

Improvements

1. Code Quality

  • Original code has duplicated validation logic
  • New code uses a shared validation function
  • More maintainable implementation

2. Maintainability

  • Single source of truth for validation
  • Easier to update validation rules
  • Better for team collaboration

3. Future Maintenance

  • Easier to add new validation rules
  • Clearer code structure
  • Better for testing

Tips

1. Start Positive

  • Acknowledge the working code
  • Show you understand the current approach
  • Example: “Great work on the email validation!“

2. Explain the Benefits

  • Why does reducing duplication matter?
  • How does it help maintainability?
  • 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
Domain TypesPositive Reinforcement