Trade-Offs
Let’s look at how to handle trade-offs 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 validate a user’s password. The code works but could be optimized for better performance at the cost of readability.
passwordValidation.ts
// Before
function validatePassword(password: string): boolean {
// Simple but potentially slow for long passwords
const hasUpperCase = /[A-Z]/.test(password)
const hasLowerCase = /[a-z]/.test(password)
const hasNumber = /[0-9]/.test(password)
const hasSpecialChar = /[!@#$%^&*]/.test(password)
const isLongEnough = password.length >= 8
return (
hasUpperCase && hasLowerCase && hasNumber && hasSpecialChar && isLongEnough
)
}PR Comment
Choose the comment that you think is the most constructive and helpful.
Click here to learn more
Improvements
1. Performance vs Readability
- Original code uses regex which is clear but runs multiple tests
- New code uses a single pass through the string
- Clear explanation of the performance benefit
2. Code Quality
- Better performance for long passwords
- Still maintains code clarity
- Balanced approach to optimization
3. Future Maintenance
- Easier to understand the performance considerations
- Clear documentation of trade-offs
- Better for team collaboration
Tips
1. Start Positive
- Acknowledge the working code
- Show you understand the current approach
- Example: “Great work on the password validation!“
2. Explain the Trade-offs
- What are the competing concerns?
- How do they affect the code?
- Example: “This balances readability with performance”
3. Be Specific
- Suggest concrete improvements
- Explain why they matter
- Example: “Using a single pass improves performance for long passwords”
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 too slow. Fix it.”
- ✅ “Good work! Here’s how we can balance performance with readability.”
2. Not Explaining Why
- ❌ “Use a for loop instead.”
- ✅ “Using a single pass through the string improves performance while maintaining readability.”
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
This is too slow. Optimize it.