Vibe Coding Codex Β· CH 9

Chapter 9: Using AI to Review and Refactor Your Own Code

⏱️ 3 min read·Updated Jul 2026

AI as Your Tireless Senior Reviewer

Code review is the highest-leverage activity in software development. It catches bugs before they reach production, enforces standards, and spreads knowledge across a team. But human reviewers are expensive, slow, and unavailable at 2am when you are in flow.

AI code review is not a replacement for human judgment on critical systems. But for the solo developer or small team, it is the closest thing to having a senior engineer on call 24/7.

---

The Four Review Lenses

Structure your AI code reviews around four distinct perspectives. Each catches different categories of problems.

Lens 1: Correctness (Bug Hunt)

code
Review this code for correctness bugs only. Look for:
- Off-by-one errors in loops and array access
- Unhandled async/await errors (missing try/catch)
- Race conditions in concurrent operations
- Null/undefined dereferences that could crash at runtime
- Logic errors where the code does the wrong thing in edge cases
- Incorrect assumptions about data types or shapes

For each bug found: show the exact line, explain what goes wrong, and write the corrected version.

[paste your code]

Lens 2: Security

code
Perform a security audit of this code. Check for:
- SQL/NoSQL injection vulnerabilities
- Missing authentication/authorisation checks on sensitive operations
- User-controlled data being used in dangerous operations (eval, fs operations, shell commands)
- Sensitive data (passwords, API keys, PII) being logged or returned to the client
- Missing rate limiting on expensive operations
- CORS misconfiguration

For each issue: classify as Critical/High/Medium/Low, explain the attack vector, and provide a fix.

[paste your code]

Lens 3: Performance

code
Analyse this code for performance problems. Look for:
- N+1 database query patterns (querying inside loops)
- Missing database indexes for queried fields
- Expensive operations being repeated when they could be cached or memoised
- Large data being fetched when only a subset is needed (SELECT * problems)
- Synchronous operations blocking the event loop
- Missing pagination on queries that could return large datasets

For each issue: estimate the impact (low traffic vs. high traffic), explain why it matters, and suggest the fix.

[paste your code]

Lens 4: Maintainability

code
Review this code for long-term maintainability. Identify:
- Functions longer than 50 lines that should be split
- Duplicated logic that should be extracted into shared utilities
- Magic numbers and strings that should be named constants
- Missing or misleading comments on non-obvious logic
- Tightly coupled modules that are hard to test in isolation
- Inconsistent naming conventions

For each issue: explain the cost of leaving it as-is, and suggest the refactor.

[paste your code]

---

The Full-File Refactor Prompt

Once you have identified issues, use this prompt to get a clean, refactored version:

code
Here is the original file and a list of identified issues. Refactor the file to fix all issues while:
- Keeping the same external API (function signatures, exports)
- Maintaining backward compatibility
- Adding comments explaining the key changes made

Original file:
[paste file]

Issues to fix:
[paste issues from review]

Output the complete refactored file.

---

Building a Review Into Your Workflow

The best time to run an AI code review is:

    • Before committing β€” paste the diff into Claude or GPT and run the correctness lens
    • Before deploying β€” run the security lens on your API routes
    • Weekly refactor session β€” pick 2-3 files, run the maintainability lens, make improvements
Make it a habit. Code quality compounds.

Finished reading this chapter?

Mark it completed to update your AI Builder skill profile score on your dashboard.

πŸ“‹

Download Printable Chapter Checklist

Get a print-friendly, formatted checklist of workspace actions to apply this chapter offline.