Be Constructive
Let’s look at how to give constructive feedback in code reviews. We’ll show you a simple example and explain why the feedback matters.
The Scenario
A developer has written a function to calculate the total price of items in a shopping cart. The code may work but could be improved.
calculateTotal.ts
// Before
function calculateTotal(items) {
let total = 0
for (let i = 0; i < items.length; i++) {
total += items.price
}
return total
}PR Comment
Choose the comment that you think is the most constructive and helpful.
Click here to learn more
Improvements
1. Code Clarity
- The
reducemethod clearly shows we’re combining all items into a single value - The code is more declarative, showing what we want to do rather than how to do it
- Less code means fewer places for bugs to hide
2. Edge Case Handling
reducewith an initial value of 0 handles empty arrays correctly- The original code would also work with empty arrays, but it’s not as obvious
- The new version makes the behavior more explicit
3. Bug Prevention
- The original code had a potential bug:
items.priceinstead ofitems[i].price - The new version makes it impossible to make this mistake
- The structure of
reduceforces us to handle each item correctly
Tips
1. Start Positive
- Acknowledge what works well
- Show you understand the code’s purpose
- Example: “Great work on the total calculation!“
2. Explain Your Suggestions
- Why is your suggestion better?
- What problems does it solve?
- Example: “The
reducemethod makes the code more concise and easier to read.”
3. Be Specific
- Point to exact lines or patterns
- Provide clear examples
- Example: “This handles edge cases better, like an empty array.”
4. Keep It Friendly
- Use encouraging language
- Focus on improvement, not criticism
- Example: “Thanks for making it more readable!”
Common Pitfalls to Avoid
1. Being Too Critical
- ❌ “This code is wrong. You should use
reduce.” - ✅ “Great work! Here’s a suggestion to make it even better.”
2. Not Explaining Why
- ❌ “Use reduce instead.”
- ✅ “Using
reducemakes the code more concise and handles edge cases better.”
3. Ignoring the Positive
- ❌ “This needs improvement.”
- ✅ “Good work! Here’s how we can make it even better.”
Remember: The goal is to help your teammate write better code while maintaining a positive and collaborative environment!
Last updated on