Code Review and Refactoring Chains
AI can systematically review and improve code through multi-step chains. This lesson covers workflows for automated code review and refactoring.
The Review Pipeline
Code → Static Analysis → Style Check → Logic Review → Security Scan → Report
Multi-Aspect Code Review
Style and Conventions
Loading Prompt Playground...
Logic and Correctness
async function reviewLogic(code, requirements) {
const prompt = `
Review this code for logic and correctness issues.
CODE:
${code}
REQUIREMENTS:
${requirements}
Check for:
1. Does the code meet all requirements?
2. Are there logic errors or bugs?
3. Are edge cases handled?
4. Are there potential race conditions?
5. Is error handling complete?
Report format:
{
"issues": [
{
"type": "bug|missing_requirement|edge_case|race_condition",
"description": "",
"location": "line number or function name",
"severity": "critical|high|medium|low",
"suggested_fix": ""
}
],
"requirements_coverage": {
"covered": [],
"missing": []
}
}
`;
return await llm.chat({ content: prompt });
}
Security Review
Loading Prompt Playground...
Refactoring Chains
Identify Refactoring Opportunities
async function identifyRefactoring(code) {
const prompt = `
Analyze this code for refactoring opportunities.
CODE:
${code}
Look for:
1. Code duplication (DRY violations)
2. Long functions that should be split
3. Complex conditionals to simplify
4. Magic numbers/strings to extract
5. Opportunities for patterns (strategy, factory, etc.)
6. Dead code to remove
7. Naming improvements
Return:
{
"opportunities": [
{
"type": "duplication|complexity|naming|pattern|dead_code",
"location": "",
"description": "",
"impact": "high|medium|low",
"effort": "simple|moderate|complex"
}
],
"priority_order": ["ordered list of refactoring IDs"]
}
`;
return await llm.chat({ content: prompt });
}
Step-by-Step Refactoring
Loading Prompt Playground...
Safe Refactoring Pipeline
async function safeRefactor(code, tests) {
// Step 1: Verify tests pass before refactoring
const initialTestResult = await runTests(tests);
if (!initialTestResult.allPassed) {
return { error: 'Tests must pass before refactoring' };
}
// Step 2: Identify refactoring opportunities
const opportunities = await identifyRefactoring(code);
// Step 3: Apply refactoring one at a time
let refactoredCode = code;
const applied = [];
for (const opp of opportunities.priority_order) {
const refactored = await applyRefactoring(refactoredCode, opp);
// Step 4: Run tests after each refactoring
const testResult = await runTests(tests, refactored);
if (testResult.allPassed) {
refactoredCode = refactored;
applied.push(opp);
} else {
// Skip this refactoring, tests broke
console.log(`Skipping ${opp.id}: broke tests`);
}
}
return {
original: code,
refactored: refactoredCode,
appliedRefactorings: applied
};
}
Review Report Generation
Comprehensive Report
async function generateReviewReport(reviews) {
const report = {
summary: {
totalIssues: countIssues(reviews),
criticalIssues: countBySeverity(reviews, 'critical'),
passedChecks: countPassed(reviews)
},
byCategory: {
style: reviews.style,
logic: reviews.logic,
security: reviews.security,
performance: reviews.performance
},
actionItems: prioritizeIssues(reviews),
recommendations: generateRecommendations(reviews)
};
return report;
}
Exercise: Build a Review Chain
Loading Prompt Playground...
Key Takeaways
- Review code in multiple passes, each with specific focus
- Style, logic, and security reviews are distinct steps
- Identify refactoring opportunities systematically
- Refactor in small, testable steps
- Run tests after each refactoring
- Generate comprehensive reports with prioritized action items
- Include automated security scanning for common vulnerabilities
In the next module, we'll cover production considerations for prompt chains.

