Review code changes for quality, complexity, and duplication. Invoke with /simplify
Analyze code changes for quality issues, simplification opportunities, and duplicate code patterns.
git rev-parse --git-dir 2>&1
If this fails, respond with:
Error: Not a git repository. Navigate to a git repo to use /simplify.
# Get current branch
BRANCH=$(git branch --show-current)
# Check for main/master
git rev-parse --verify main 2>/dev/null
git rev-parse --verify master 2>/dev/null
main/master → analyze uncommitted changes (git diff HEAD)main exists → compare to main (git diff main...HEAD)master exists → compare to master (git diff master...HEAD)Get the diff and changed files based on context:
On main/master (uncommitted changes):
git diff HEAD
git diff --name-status HEAD
On feature branch:
git diff main...HEAD # or master...HEAD
git diff --name-status main...HEAD
If no changes found, report: "No changes to analyze."
For each changed file, read the full current content to understand context:
Review each changed file for these categories:
Analyze each function for complexity indicators:
Identify extraction candidates - logic that can become pure functions:
Pure function characteristics to enforce:
Example extraction:
BEFORE (mixed concerns):
function processOrder(order) {
// validation
if (!order.items || order.items.length === 0) throw new Error('Empty');
// calculation
const subtotal = order.items.reduce((sum, i) => sum + i.price * i.qty, 0);
const tax = subtotal * 0.1;
const total = subtotal + tax;
// side effect
await db.save({ ...order, total });
return total;
}
AFTER (extracted pure functions):
const validateOrder = (order) => {
if (!order.items?.length) throw new Error('Empty');
return order;
};
const calculateOrderTotal = (items, taxRate = 0.1) => {
const subtotal = items.reduce((sum, i) => sum + i.price * i.qty, 0);
return { subtotal, tax: subtotal * taxRate, total: subtotal * (1 + taxRate) };
};
async function processOrder(order) {
validateOrder(order);
const totals = calculateOrderTotal(order.items);
await db.save({ ...order, ...totals });
return totals.total;
}
Present findings grouped by category with confidence levels:
## Code Simplification Report
**Branch:** [branch name]
**Comparing:** [what vs what]
**Files analyzed:** [count]
---
### Quality Issues
#### [filename]:[line]
**Issue:** [description]
**Confidence:** High/Medium/Low
**Suggestion:**
```[language]
[suggested fix]
Complexity: [line count] lines, [nesting depth] levels deep, [responsibility count] responsibilities Problem: [why it's too complex - mixed concerns, multiple responsibilities, etc.] Extract as pure functions:
[extracted pure function(s)]
Refactored original:
[simplified orchestrating function]
Current: [what it does now] Problem: [why it's complex] Suggested:
[simplified version]
Found in:
Suggestion: Extract to shared utility in [suggested location]
[extracted helper]
Priority fixes: [top 3 most impactful items]
## Guidelines
### What to Flag
- Functions over 30 lines (extract pure functions)
- Functions with multiple responsibilities (validation + transformation + I/O)
- Functions mixing pure logic with side effects
- Nesting deeper than 2 levels
- Nested ternaries (always)
- Copy-pasted code blocks
- Missing error handling at boundaries
- Unclear variable names
- Complex boolean expressions
### What NOT to Flag
- Style preferences without clear benefit
- Micro-optimizations that reduce readability
- Working code that's "fine" but not perfect
- Patterns consistent with rest of codebase
- Edge cases already handled elsewhere
### Confidence Levels
- **High**: Clear improvement, low risk of breaking
- **Medium**: Likely improvement, needs verification
- **Low**: Subjective or context-dependent
### Tone
- Be direct and actionable
- Explain the "why" briefly
- Provide concrete code suggestions
- Focus on maintainability over cleverness