AI-Assisted Code Review: Practices and Limitations

May 29, 2023

AI code review tools promise to catch bugs, enforce standards, and speed up reviews. The reality is more nuanced: AI excels at certain review tasks and fails at others. Understanding these boundaries lets you use AI review effectively.

Here’s how to integrate AI into your code review process.

What AI Code Review Can Do

Strengths

ai_review_strengths:
  pattern_recognition:
    - Common bug patterns
    - Known anti-patterns
    - Security vulnerability patterns
    - Performance anti-patterns

  consistency_checking:
    - Style guide adherence
    - Naming conventions
    - Code formatting
    - Documentation presence

  boilerplate_review:
    - Standard error handling
    - Logging patterns
    - Test structure
    - Configuration files

  knowledge_access:
    - API usage patterns
    - Library best practices
    - Language idioms
    - Framework conventions

Effective Use Cases

good_ai_review_tasks:
  security_scanning:
    examples:
      - SQL injection vulnerabilities
      - XSS potential
      - Hardcoded secrets
      - Insecure defaults

  style_enforcement:
    examples:
      - Naming conventions
      - Import ordering
      - Comment formatting
      - Line length

  simple_bug_detection:
    examples:
      - Null pointer potential
      - Off-by-one errors
      - Unused variables
      - Resource leaks

  documentation_review:
    examples:
      - Missing docstrings
      - Outdated comments
      - README gaps
      - API documentation

What AI Code Review Cannot Do

Limitations

ai_review_limitations:
  business_logic:
    - Does this implement the requirements correctly?
    - Is this the right approach for our use case?
    - Does this align with our product goals?

  architecture:
    - Does this fit our system design?
    - Are the abstractions appropriate?
    - Will this scale with our needs?

  context:
    - How does this interact with existing code?
    - What are the historical reasons for current patterns?
    - What are the team's conventions beyond style?

  judgment:
    - Is this over-engineered?
    - Is this the right trade-off?
    - Should we do this differently?

Anti-Patterns

ai_review_anti_patterns:
  rubber_stamping:
    problem: AI says it's fine, so ship it
    reality: AI misses business logic issues
    solution: Human review still required

  over_reliance:
    problem: Skip human review for "simple" changes
    reality: Simple changes can have complex impacts
    solution: AI augments, doesn't replace

  ignoring_context:
    problem: AI flags "issue" that's intentional
    reality: AI doesn't know your codebase history
    solution: Teach team to evaluate AI suggestions

  noise_fatigue:
    problem: Too many low-value AI comments
    reality: Team ignores all AI feedback
    solution: Configure thresholds, focus on high-value

Implementation

AI Review Workflow

review_workflow:
  automated_checks:
    when: On PR open
    tools: AI review, linters, security scanners
    blocking: Only for critical issues

  human_review:
    when: After automated checks pass
    focus: Business logic, architecture, context
    required: At least one approval

  ai_assisted_human:
    approach: AI highlights areas for human attention
    benefit: Faster human review
    example: "AI flagged potential race condition at line 45"

Prompt Engineering for Review

CODE_REVIEW_PROMPT = """
Review this code change for:
1. Bugs and logic errors
2. Security vulnerabilities
3. Performance issues
4. Code style (based on our guidelines below)

Focus on actionable feedback. For each issue:
- Line number(s)
- Issue description
- Severity (info/warning/error)
- Suggested fix

Be concise. Only comment on actual issues, not style preferences.

Our guidelines:
{style_guide}

Code change:
```{language}
{diff}

Review: """

def ai_review(diff, language, style_guide): prompt = CODE_REVIEW_PROMPT.format( diff=diff, language=language, style_guide=style_guide )

response = llm.generate(prompt, temperature=0)
return parse_review_comments(response)

### Integration with PR Systems

```python
class AIReviewBot:
    def on_pr_opened(self, pr):
        # Get the diff
        diff = pr.get_diff()

        # Run AI review
        comments = self.ai_review(diff)

        # Filter by severity
        significant = [c for c in comments if c.severity in ['warning', 'error']]

        # Post as review comments
        for comment in significant:
            pr.add_review_comment(
                path=comment.file,
                line=comment.line,
                body=self.format_comment(comment)
            )

        # Add summary
        pr.add_comment(self.generate_summary(comments))

    def format_comment(self, comment):
        severity_emoji = {
            'info': 'ℹ️',
            'warning': '⚠️',
            'error': '🚨'
        }
        return f"{severity_emoji[comment.severity]} **AI Review**: {comment.description}\n\n{comment.suggestion}"

Configuring Thresholds

# ai-review.yml
ai_review:
  enabled: true

  checks:
    security:
      enabled: true
      severity: error
      blocking: true

    bugs:
      enabled: true
      severity: warning
      blocking: false

    style:
      enabled: true
      severity: info
      blocking: false

    performance:
      enabled: true
      severity: warning
      blocking: false

  exclusions:
    - "*.test.js"
    - "*.spec.ts"
    - "**/fixtures/**"

  suppressions:
    - pattern: "// ai-review-ignore"
      reason_required: true

Measuring Effectiveness

Metrics

ai_review_metrics:
  true_positives:
    - Issues found by AI that humans agreed with
    - Track: Count, severity distribution

  false_positives:
    - AI comments dismissed by humans
    - Track: Rate, common patterns

  false_negatives:
    - Issues found by humans that AI missed
    - Track: Category analysis

  review_time:
    - Time to first human review
    - Total review cycle time
    - Track: Before/after AI

  developer_sentiment:
    - Survey: Is AI review helpful?
    - Track: Regularly

Continuous Improvement

improvement_process:
  collect_feedback:
    - Track dismissed AI comments
    - Survey developers quarterly
    - Analyze merged bugs

  refine_prompts:
    - Update based on false positive patterns
    - Add examples of good catches
    - Improve context provision

  adjust_thresholds:
    - Raise thresholds for noisy checks
    - Lower for high-value checks
    - Per-team customization

  share_learnings:
    - What AI catches well
    - What needs human review
    - Best practices for working with AI

Key Takeaways

AI code review is a tool. Like all tools, its value depends on how you use it.