// Perform comprehensive code reviews with best practices, security checks, and constructive feedback. Use when reviewing pull requests, analyzing code quality, checking for security vulnerabilities, or providing code improvement suggestions.
| name | code-review-analysis |
| description | Perform comprehensive code reviews with best practices, security checks, and constructive feedback. Use when reviewing pull requests, analyzing code quality, checking for security vulnerabilities, or providing code improvement suggestions. |
Systematic code review process covering code quality, security, performance, maintainability, and best practices following industry standards.
# Check the changes
git diff main...feature-branch
# Review file changes
git diff --stat main...feature-branch
# Check commit history
git log main...feature-branch --oneline
Quick Checklist:
# โ Poor readability
def p(u,o):
return u['t']*o['q'] if u['s']=='a' else 0
# โ
Good readability
def calculate_order_total(user: User, order: Order) -> float:
"""Calculate order total with user-specific pricing."""
if user.status == 'active':
return user.tier_price * order.quantity
return 0
// โ High cognitive complexity
function processData(data) {
if (data) {
if (data.type === 'user') {
if (data.status === 'active') {
if (data.permissions && data.permissions.length > 0) {
// deeply nested logic
}
}
}
}
}
// โ
Reduced complexity with early returns
function processData(data) {
if (!data) return null;
if (data.type !== 'user') return null;
if (data.status !== 'active') return null;
if (!data.permissions?.length) return null;
// main logic at top level
}
SQL Injection
# โ Vulnerable to SQL injection
query = f"SELECT * FROM users WHERE email = '{user_email}'"
# โ
Parameterized query
query = "SELECT * FROM users WHERE email = ?"
cursor.execute(query, (user_email,))
XSS Prevention
// โ XSS vulnerable
element.innerHTML = userInput;
// โ
Safe rendering
element.textContent = userInput;
// or use framework escaping: {{ userInput }} in templates
Authentication & Authorization
// โ Missing authorization check
app.delete('/api/users/:id', async (req, res) => {
await deleteUser(req.params.id);
res.json({ success: true });
});
// โ
Proper authorization
app.delete('/api/users/:id', requireAuth, async (req, res) => {
if (req.user.id !== req.params.id && !req.user.isAdmin) {
return res.status(403).json({ error: 'Forbidden' });
}
await deleteUser(req.params.id);
res.json({ success: true });
});
// โ N+1 query problem
const users = await User.findAll();
for (const user of users) {
user.orders = await Order.findAll({ where: { userId: user.id } });
}
// โ
Eager loading
const users = await User.findAll({
include: [{ model: Order }]
});
# โ Inefficient list operations
result = []
for item in large_list:
if item % 2 == 0:
result.append(item * 2)
# โ
List comprehension
result = [item * 2 for item in large_list if item % 2 == 0]
Test Coverage
describe('User Service', () => {
// โ
Tests edge cases
it('should handle empty input', () => {
expect(processUser(null)).toBeNull();
});
it('should handle invalid data', () => {
expect(() => processUser({})).toThrow(ValidationError);
});
// โ
Tests happy path
it('should process valid user', () => {
const result = processUser(validUserData);
expect(result.id).toBeDefined();
});
});
Check for:
// โ Silent failures
try {
await saveData(data);
} catch (e) {
// empty catch
}
// โ
Proper error handling
try {
await saveData(data);
} catch (error) {
logger.error('Failed to save data', { error, data });
throw new DataSaveError('Could not save data', { cause: error });
}
# โ Resources not closed
file = open('data.txt')
data = file.read()
process(data)
# โ
Proper cleanup
with open('data.txt') as file:
data = file.read()
process(data)
## Code Review: [PR Title]
### Summary
Brief overview of changes and overall assessment.
### โ
Strengths
- Well-structured error handling
- Comprehensive test coverage
- Clear documentation
### ๐ Issues Found
#### ๐ด Critical (Must Fix)
1. **Security**: SQL injection vulnerability in user query (line 45)
```python
# Current code
query = f"SELECT * FROM users WHERE id = '{user_id}'"
# Suggested fix
query = "SELECT * FROM users WHERE id = ?"
cursor.execute(query, (user_id,))
proc_data could be more descriptive as processUserDataโ Approved with minor suggestions | โธ๏ธ Needs changes | โ Needs major revision
## Common Issues Checklist
### Security
- [ ] No SQL injection vulnerabilities
- [ ] XSS prevention in place
- [ ] CSRF protection where needed
- [ ] Authentication/authorization checks
- [ ] No exposed secrets or credentials
- [ ] Input validation implemented
- [ ] Output encoding applied
### Code Quality
- [ ] Functions are focused and small
- [ ] Names are descriptive
- [ ] No code duplication
- [ ] Appropriate comments
- [ ] Consistent style
- [ ] No magic numbers
- [ ] Error messages are helpful
### Performance
- [ ] No N+1 queries
- [ ] Appropriate indexing
- [ ] Efficient algorithms
- [ ] No unnecessary computations
- [ ] Proper caching where beneficial
- [ ] Resource cleanup
### Testing
- [ ] Tests included for new code
- [ ] Edge cases covered
- [ ] Error cases tested
- [ ] Integration tests if needed
- [ ] Tests are maintainable
- [ ] No flaky tests
### Maintainability
- [ ] Code is self-documenting
- [ ] Complex logic is explained
- [ ] No premature optimization
- [ ] Follows SOLID principles
- [ ] Dependencies are appropriate
- [ ] Backwards compatibility considered
## Tools
- **Linters**: ESLint, Pylint, RuboCop
- **Security**: Snyk, OWASP Dependency Check, Bandit
- **Code Quality**: SonarQube, Code Climate
- **Coverage**: Istanbul, Coverage.py
- **Static Analysis**: TypeScript, Flow, mypy
## Best Practices
### โ
DO
- Be constructive and respectful
- Explain the "why" behind suggestions
- Provide code examples
- Ask questions if unclear
- Acknowledge good practices
- Focus on important issues
- Consider the context
- Offer to pair program on complex issues
### โ DON'T
- Be overly critical or personal
- Nitpick minor style issues (use automated tools)
- Block on subjective preferences
- Review too many changes at once (>400 lines)
- Forget to check tests
- Ignore security implications
- Rush the review
## Examples
See the refactor-legacy-code skill for detailed refactoring examples that often apply during code review.