Code Review with AI
AI can significantly enhance code review processes when used correctly. This guide covers best practices for AI-assisted code reviews.
Overview
AI-assisted code review combines:
- Speed: AI can quickly scan large amounts of code
- Consistency: AI applies rules consistently
- Coverage: AI can check for patterns humans might miss
- Human Judgment: Final decisions by experienced reviewers
When to Use AI in Code Review
✅ Good Use Cases
- Initial screening: Quick pass before human review
- Pattern detection: Finding common anti-patterns
- Style checking: Enforcing code style guidelines
- Security scanning: Identifying potential vulnerabilities
- Documentation review: Checking comment quality
- Test coverage analysis: Identifying untested code paths
⚠️ Use with Caution
- Architecture decisions: Requires human expertise
- Business logic: Needs domain knowledge
- Performance optimization: Context-dependent
- Complex refactoring: Needs holistic understanding
AI Review Process
1. Automated Initial Review
Run AI review before human reviewers:
typescript
async function aiPreReview(diff: string): Promise<ReviewReport> {
const prompt = `
Review this code diff for:
1. **Critical Issues**
- Security vulnerabilities
- Logic errors
- Resource leaks
2. **Code Quality**
- Readability issues
- Code duplication
- Complex functions (cyclomatic complexity > 10)
3. **Best Practices**
- Error handling
- Input validation
- Proper use of types
Diff:
${diff}
Provide structured feedback with severity levels.
`;
const response = await llm.complete({
model: 'claude-3-5-sonnet-20241022',
messages: [{ role: 'user', content: prompt }],
});
return parseReviewReport(response.content);
}2. Human Review
Human reviewers then:
- Verify AI findings
- Add context and domain knowledge
- Make final decisions
- Provide mentorship
3. Feedback Loop
- Track AI accuracy
- Refine AI prompts based on feedback
- Document patterns that work
Review Checklist
Security
Ask AI to check for:
typescript
const securityChecks = `
Review for security issues:
- [ ] SQL injection vulnerabilities
- [ ] XSS (Cross-Site Scripting) risks
- [ ] Authentication/authorization issues
- [ ] Hardcoded credentials or secrets
- [ ] Insecure data handling
- [ ] CSRF vulnerabilities
- [ ] Unvalidated redirects
- [ ] Insecure dependencies
`;Code Quality
typescript
const qualityChecks = `
Review code quality:
- [ ] Functions are focused (single responsibility)
- [ ] Variables have descriptive names
- [ ] No magic numbers or strings
- [ ] DRY principle followed (no duplication)
- [ ] Proper error handling
- [ ] Edge cases considered
- [ ] Comments explain "why", not "what"
`;Testing
typescript
const testingChecks = `
Review testing:
- [ ] Unit tests included
- [ ] Edge cases tested
- [ ] Error conditions tested
- [ ] Mocks used appropriately
- [ ] Test names are descriptive
- [ ] Sufficient code coverage
`;Example Prompts
Comprehensive Review
Review this pull request comprehensively:
**Focus Areas:**
1. Logic correctness
2. Security vulnerabilities
3. Performance implications
4. Code maintainability
5. Test coverage
**Code Changes:**
${diff}
**For each issue found, provide:**
- Severity: critical/high/medium/low
- Category: security/performance/maintainability/etc
- Description: What's wrong and why
- Suggestion: How to fix it
- Example: Show the corrected code if applicable
Format as structured JSON for easy parsing.Security-Focused Review
Perform a security review of these changes:
${diff}
Check specifically for:
- Input validation issues
- Authentication/authorization problems
- Data exposure risks
- Injection vulnerabilities
- Insecure dependencies
- Cryptography misuse
Provide severity ratings and remediation steps.Performance Review
Analyze these changes for performance implications:
${diff}
Consider:
- Time complexity of algorithms
- Space complexity and memory usage
- Database query efficiency
- Network request optimization
- Caching opportunities
- Resource cleanup
Identify bottlenecks and suggest optimizations.Structured Review Format
Request AI to provide structured output:
typescript
interface ReviewComment {
file: string;
line?: number;
severity: 'critical' | 'high' | 'medium' | 'low' | 'suggestion';
category: string;
title: string;
description: string;
suggestion?: string;
example?: string;
}
interface ReviewReport {
summary: string;
score: number; // 0-100
comments: ReviewComment[];
positives: string[]; // What's done well
suggestions: string[]; // General improvements
}Integration with CI/CD
GitHub Actions Example
yaml
name: AI Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
ai-review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Get PR diff
id: diff
run: |
git diff origin/${{ github.base_ref }}...HEAD > pr.diff
- name: AI Review
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
node scripts/ai-review.js pr.diff > review.json
- name: Post Review Comments
uses: actions/github-script@v6
with:
script: |
const review = require('./review.json');
// Post comments to PRReview Script
typescript
// scripts/ai-review.js
import { readFileSync } from 'fs';
import Anthropic from '@anthropic-ai/sdk';
async function reviewPR(diffPath: string) {
const diff = readFileSync(diffPath, 'utf-8');
// Sanitize diff (remove secrets if any)
const sanitized = sanitizeDiff(diff);
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
const prompt = buildReviewPrompt(sanitized);
const response = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 4096,
messages: [{ role: 'user', content: prompt }],
});
const review = parseReview(response.content[0].text);
console.log(JSON.stringify(review, null, 2));
}
reviewPR(process.argv[2]);Best Practices
1. Combine AI with Other Tools
Layer AI review with:
- Static analysis (ESLint, SonarQube)
- Security scanners (Snyk, Dependabot)
- Test coverage tools
- Type checkers
2. Provide Context
Give AI context about:
- What the PR is trying to accomplish
- Relevant business requirements
- Related code or documentation
- Previous discussions
Context: This PR implements user authentication using OAuth 2.0.
Requirements: Must support Google and GitHub providers.
Related: See docs/auth.md for architecture.
Review these changes:
${diff}3. Focus AI on Specific Aspects
Don't try to do everything at once:
Focus only on security aspects of this code:
- Authentication logic
- Authorization checks
- Data validation
- Secure storage
${code}4. Use AI to Generate Review Questions
Generate thoughtful code review questions about these changes:
- What edge cases might be missed?
- What could break in production?
- What's the performance impact?
- What security concerns exist?
${diff}5. Learn from AI Reviews
Document patterns AI catches well:
- Common security mistakes
- Performance anti-patterns
- Style inconsistencies
Use these to improve:
- Team coding guidelines
- Automated linters
- Developer training
Limitations
What AI Can't Do Well
- Understand business context without extensive explanation
- Make subjective decisions about architecture
- Evaluate user experience implications
- Assess team dynamics and communication
- Make final approval decisions
Common AI Mistakes
- False positives: Flagging valid code as problematic
- Missing context: Not understanding special cases
- Over-engineering: Suggesting unnecessarily complex solutions
- Inconsistent standards: Different suggestions for similar code
Mitigation Strategies
- Always have human review after AI
- Calibrate AI feedback based on team standards
- Teach team to critically evaluate AI suggestions
- Track and fix recurring false positives
- Refine prompts based on experience
Review Templates
Standard PR Review
markdown
## AI-Assisted Code Review
**Summary:** [AI-generated summary]
### Critical Issues
- [ ] Issue 1
- [ ] Issue 2
### Suggestions
- [ ] Suggestion 1
- [ ] Suggestion 2
### Positive Aspects
- Well-tested code
- Clear variable names
### Human Reviewer Notes
[Add human context and final decisions]Security Review
markdown
## Security Review
**Overall Security Score:** [0-100]
### Vulnerabilities Found
1. **[Severity]** [Issue]
- **Impact:** [Description]
- **Remediation:** [Fix]
### Security Best Practices Followed
- Input validation present
- Authentication implemented
### Requires Security Team Review
- [ ] Cryptography changes
- [ ] Authentication logicMetrics and Improvement
Track These Metrics
- AI review time vs. human review time
- Issues found by AI vs. issues found by humans
- False positive rate of AI reviews
- Developer satisfaction with AI reviews
Continuous Improvement
- Weekly: Review AI accuracy, update prompts
- Monthly: Analyze patterns, update guidelines
- Quarterly: Assess impact, adjust strategy
Resources
Remember: AI is a powerful assistant for code review, but human judgment and expertise remain essential. Use AI to augment, not replace, human reviewers.