Code Efficiency
Let’s look at how to suggest performance improvements in code reviews. We’ll show you a simple example and explain why the feedback matters.
The Scenario
A developer has implemented a function to fetch user data. The code works but could be optimized to be more efficient.
userService.ts
// Before
async function getUserData(userId: string) {
const allUsers = await fetchAllUsers()
return allUsers.find((user) => user.id === userId)
}PR Comment
Choose the comment that you think is the most constructive and helpful.
Click here to learn more
Improvements
1. Performance Impact
- Original code fetches all users unnecessarily
- New code fetches only the needed user
- Clear explanation of the performance benefit
2. Code Quality
- Better performance for large user bases
- More efficient use of resources
- Cleaner, more focused implementation
3. Future Maintenance
- Easier to understand the performance considerations
- Clear documentation of improvements
- Better for team collaboration
Tips
1. Start Positive
- Acknowledge the working code
- Show you understand the current approach
- Example: “Great work on the user data fetch!“
2. Explain the Benefits
- What are the performance gains?
- How does it help the system?
- Example: “This will be more efficient as the user base grows”
3. Be Specific
- Suggest concrete improvements
- Explain why they matter
- Example: “Fetching only the needed user reduces database load”
4. Keep It Friendly
- Focus on improvement, not criticism
- Use encouraging language
- Example: “Thanks for considering performance!”
Common Pitfalls to Avoid
1. Being Too Critical
- ❌ “This is inefficient. Fix it.”
- ✅ “Good work! Here’s how we can make it even more efficient.”
2. Not Explaining Why
- ❌ “Use a direct fetch instead.”
- ✅ “Using a direct user fetch improves performance and reduces database load.”
3. Ignoring the Positive
- ❌ “This needs optimization.”
- ✅ “Great work! Here’s how we can make it even more efficient.”
Remember: The goal is to help your teammate write better code while maintaining a positive and collaborative environment!
Last updated on