Skip to content

Latest commit

 

History

History
344 lines (272 loc) · 11.5 KB

File metadata and controls

344 lines (272 loc) · 11.5 KB

Code Review Guidelines

Overview

Code reviews are essential for maintaining code quality, sharing knowledge, and ensuring consistency across our codebase. This guide establishes standards and best practices for both reviewers and authors.

Code Review Philosophy

Core Principles

  1. Constructive Feedback: Focus on code, not the person
  2. Learning Opportunity: Share knowledge and learn from others
  3. Quality Assurance: Catch bugs and improve maintainability
  4. Consistency: Ensure adherence to team standards
  5. Security: Identify potential security vulnerabilities

Review Goals

  • Correctness: Does the code do what it's supposed to do?
  • Design: Is the code well-designed for its purpose?
  • Functionality: Does it work as intended for users?
  • Complexity: Is it more complex than necessary?
  • Tests: Are there appropriate tests?
  • Naming: Are variables, functions, and classes well-named?
  • Comments: Are comments clear and useful?
  • Documentation: Is documentation updated?

Review Process

1. Pre-Review Checklist (Author)

Before Creating PR

  • Self-review: Review your own code first
  • Tests pass: All automated tests are passing
  • Linting: Code passes all linting checks
  • Documentation: Update relevant documentation
  • Commit messages: Follow conventional commit format
  • PR description: Clear description of changes
  • Screenshots: Include for UI changes
  • Breaking changes: Clearly marked and documented

PR Description Template

## Description
Brief description of changes and why they were made.

## Type of Change
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Documentation update

## Testing
- [ ] Unit tests added/updated
- [ ] Integration tests added/updated
- [ ] Manual testing completed
- [ ] Tests pass locally

## Screenshots (if applicable)
Include screenshots for UI changes.

## Checklist
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes

2. Review Assignment

Automatic Assignment

  • Required reviewers: At least 2 team members
  • Code owners: Automatically assigned for specific areas
  • Senior developers: Required for architectural changes

Manual Assignment

  • Domain experts: For specific technology areas
  • Security team: For security-related changes
  • DevOps team: For infrastructure changes

3. Review Timeline

Response Times

  • Initial review: Within 24 hours
  • Follow-up reviews: Within 4 hours
  • Urgent fixes: Within 2 hours
  • Documentation: Within 48 hours

Review Priorities

  1. Critical bug fixes - Immediate review
  2. Security patches - Within 2 hours
  3. Feature development - Standard timeline
  4. Refactoring - Standard timeline
  5. Documentation - Flexible timeline

Review Checklist

Functionality

  • Requirements: Code meets acceptance criteria
  • Edge cases: Handles error conditions appropriately
  • Performance: No obvious performance issues
  • Security: No security vulnerabilities
  • Accessibility: UI changes follow accessibility guidelines

Code Quality

  • Single responsibility: Functions/classes have single purpose
  • DRY principle: No unnecessary code duplication
  • YAGNI: No over-engineering for future needs
  • Naming: Clear, descriptive names for variables/functions
  • Magic numbers: Constants are properly defined
  • Error handling: Appropriate error handling implemented

Testing

  • Unit tests: Critical functions have unit tests
  • Integration tests: API endpoints have integration tests
  • Test coverage: Coverage doesn't decrease significantly
  • Test quality: Tests are readable and maintainable
  • Mock usage: Appropriate use of mocks and stubs

Frontend Specific

  • Component structure: Components are well-structured
  • State management: Proper state management patterns
  • Performance: No unnecessary re-renders
  • Accessibility: ARIA labels and keyboard navigation
  • Responsive design: Works on different screen sizes
  • Bundle size: No significant increase in bundle size

Backend Specific

  • API design: RESTful API design principles
  • Database queries: Efficient database queries
  • Migration safety: Database migrations are safe
  • Authentication: Proper authentication/authorization
  • Rate limiting: API endpoints have appropriate rate limiting
  • Input validation: All inputs are properly validated

Documentation

  • Code comments: Complex logic is commented
  • API documentation: New endpoints documented
  • README updates: README updated for new features
  • Architecture docs: Architecture changes documented

Review Comments

Comment Types

Blocking Issues (Must Fix)

🚫 **BLOCKING**: This could cause a security vulnerability
  • Security issues
  • Breaking changes without migration
  • Performance regressions
  • Critical bugs

Suggestions (Should Fix)

💡 **SUGGESTION**: Consider using a Map instead of Object for better performance
  • Code quality improvements
  • Performance optimizations
  • Better design patterns
  • Maintainability improvements

Questions (Clarification)

❓ **QUESTION**: What happens if this API call fails?
  • Understanding the approach
  • Clarifying requirements
  • Learning opportunities

Nitpicks (Optional)

🎨 **NITPICK**: Consider renaming this variable for clarity
  • Style preferences
  • Minor improvements
  • Non-critical suggestions

Comment Examples

Good Comments

💡 **SUGGESTION**: Consider extracting this logic into a separate function to improve readability and testability.

```javascript
// Instead of:
const processUserData = (users) => {
  // 50 lines of complex logic
}

// Consider:
const validateUsers = (users) => { /* validation logic */ }
const transformUsers = (users) => { /* transformation logic */ }
const processUserData = (users) => {
  const validUsers = validateUsers(users);
  return transformUsers(validUsers);
}

QUESTION: How do we handle the case where the API returns a 429 (rate limited) response? Should we implement retry logic?

🚫 BLOCKING: This SQL query is vulnerable to injection attacks. Please use parameterized queries.


#### Poor Comments
```markdown
// Bad - Not constructive
This is wrong.

// Bad - Personal preference without explanation
I don't like this approach.

// Bad - Too vague
Can you improve this?

Best Practices

For Authors

Writing Reviewable Code

  1. Small PRs: Keep PRs focused and under 400 lines when possible
  2. Atomic commits: Each commit should represent a logical change
  3. Clear intent: Make the purpose of changes obvious
  4. Test coverage: Include appropriate tests
  5. Documentation: Update docs for public APIs

Responding to Reviews

  1. Acknowledge feedback: Respond to all comments
  2. Ask for clarification: If feedback is unclear
  3. Explain decisions: When you disagree with suggestions
  4. Mark resolved: Mark comments as resolved when addressed
  5. Thank reviewers: Appreciate the time spent reviewing

For Reviewers

Effective Reviewing

  1. Understand context: Read the PR description and related issues
  2. Check out code: Test complex changes locally
  3. Focus on important issues: Don't nitpick trivial matters
  4. Provide alternatives: Suggest concrete improvements
  5. Recognize good work: Acknowledge well-written code

Review Efficiency

  1. Review promptly: Don't let PRs sit too long
  2. Batch comments: Provide all feedback in one review cycle
  3. Use tools: Leverage IDE and diff tools
  4. Focus areas: Prioritize security, correctness, and design
  5. Approve when ready: Don't delay approval for minor issues

Tools and Automation

Automated Checks

  • Linting: ESLint, Pylint, Black
  • Type checking: TypeScript, mypy
  • Security scanning: CodeQL, Snyk
  • Test coverage: Coverage reports
  • Performance: Bundle analysis, load testing

Review Tools

  • GitHub: Pull request reviews with suggestions
  • IDE plugins: Review code in your editor
  • CodeStream: Real-time collaboration
  • SonarQube: Code quality analysis

Conflict Resolution

Disagreements

  1. Discussion: Have a respectful discussion
  2. Team input: Involve other team members if needed
  3. Tech lead decision: Escalate to technical leadership
  4. Documentation: Document the decision for future reference

Review Bottlenecks

  1. Multiple reviewers: Don't wait for all reviewers
  2. Follow-up PRs: Address non-blocking issues in follow-up
  3. Pair review: Review together for complex changes
  4. Time limits: Set reasonable time limits for reviews

Metrics and Improvement

Review Metrics

  • Review turnaround time: Time from PR creation to merge
  • Number of review cycles: How many back-and-forth rounds
  • Bug escape rate: Bugs found in production that should have been caught
  • Review participation: Ensure all team members participate

Continuous Improvement

  • Retrospectives: Discuss review process in team retrospectives
  • Training: Provide training on effective code reviews
  • Tool updates: Keep review tools up to date
  • Process refinement: Regularly update these guidelines

Examples

Good PR Example

## Fix user authentication timeout issue

### Problem
Users were being logged out after 30 minutes of inactivity, even when actively using the application.

### Solution
- Updated session management to reset timeout on user activity
- Added client-side activity detection
- Implemented proper token refresh mechanism

### Testing
- Added unit tests for session management
- Manual testing with different activity patterns
- Verified in staging environment

### Breaking Changes
None

### Screenshots
[Include screenshots of login flow]

Good Review Example

Overall looks good! The session management improvements are well thought out. A few suggestions:

💡 **SUGGESTION**: Consider debouncing the activity detection to avoid excessive API calls
- Line 45: The current implementation fires on every mouse move
- Could debounce to every 30 seconds to reduce server load

❓ **QUESTION**: What happens if the token refresh fails?
- Should we show a user-friendly message?
- How do we handle network errors during refresh?

🎨 **NITPICK**: The function name `checkUserActivity` could be more descriptive
- Maybe `trackUserActivityForSession` or `updateSessionOnActivity`?

✅ **APPROVED**: Great work on the comprehensive tests!

Conclusion

Effective code reviews are a skill that improves with practice. Remember that the goal is to improve code quality while fostering a positive team culture. Focus on being constructive, thorough, and respectful in all interactions.

This document is a living guide. Please contribute improvements based on your experience and team feedback.