Refactoring Recommender
When to activate
- Review a diff or changed code for simplification, performance, or maintainability improvements
- Refactor legacy code to apply modern patterns or framework idioms
- Reduce cyclomatic complexity, eliminate duplication, or improve testability
- Modernize API surface, consolidate utility functions, or standardize naming conventions
- Assess code quality after feature development or merge conflicts
When NOT to use
- For security vulnerability detection — use
/security-review instead
- For automated bug detection — use
/code-review instead
- For minor style fixes or linting — use standard formatters (Prettier, Black, gofmt)
- For architectural design decisions spanning multiple files — start with
/deep-research to gather requirements first
- When the codebase has explicit anti-refactoring policies or immutability constraints
Instructions
Scope the changeset: Read all modified files. Understand the git diff context or entire file if no diff exists. Identify hotspots: deep nesting, long functions, repeated conditionals, overlapping abstractions.
Apply the refactoring ladder (in order):
- Naming: Rename variables, functions, classes to clarify intent —
response.d → response.duration_ms
- Extract: Pull repeated logic, deeply nested blocks, or single-responsibility violations into dedicated functions or classes
- Consolidate: Merge similar functions differing only in type or parameter order; use generics or polymorphism
- Eliminate: Remove dead code, redundant null checks after type narrowing, unused parameters
- Patterns: Apply established patterns (Builder for complex objects, Strategy for conditional logic, Dependency Injection for testability)
Prioritize by ROI: Recommend high-impact changes first (reducing complexity, unblocking tests, improving readability for critical paths). Lower priority: stylistic improvements or micro-optimizations.
Preserve intent: Refactoring must not alter observable behavior. If uncertain, propose as a suggestion with caveats.
Provide concrete diffs: Show before/after code snippets for each recommendation. For large refactors, offer a staged approach.
Test impact: If tests exist, confirm the refactoring doesn't break them. Flag edge cases that the refactoring exposes.
Example
Given a TypeScript utility file with overlapping functions:
function parseJSON(input: string) {
try {
return JSON.parse(input);
} catch (e) {
return null;
}
}
function parseCSV(input: string) {
try {
const lines = input.split('\n');
return lines.map(line => line.split(','));
} catch (e) {
return null;
}
}
function parseXML(input: string) {
try {
return new DOMParser().parseFromString(input, 'text/xml');
} catch (e) {
return null;
}
}
Recommendation: Extract error handling into a higher-order function, apply Strategy pattern for parsers:
const safelyParse = <T>(parser: (input: string) => T) => (input: string): T | null => {
try {
return parser(input);
} catch {
return null;
}
};
const parsers = {
json: safelyParse(input => JSON.parse(input)),
csv: safelyParse(input => input.split('\n').map(line => line.split(','))),
xml: safelyParse(input => new DOMParser().parseFromString(input, 'text/xml')),
};
Impact: Eliminates three duplicate try-catch blocks, centralizes error handling, enables easy addition of new parsers, improves testability of parsers in isolation.
1---2name: refactoring-recommender3description: Refactoring Recommender4---5# Refactoring Recommender67## When to activate89- Review a diff or changed code for simplification, performance, or maintainability improvements10- Refactor legacy code to apply modern patterns or framework idioms11- Reduce cyclomatic complexity, eliminate duplication, or improve testability12- Modernize API surface, consolidate utility functions, or standardize naming conventions13- Assess code quality after feature development or merge conflicts1415## When NOT to use1617- For security vulnerability detection — use `/security-review` instead18- For automated bug detection — use `/code-review` instead19- For minor style fixes or linting — use standard formatters (Prettier, Black, gofmt)20- For architectural design decisions spanning multiple files — start with `/deep-research` to gather requirements first21- When the codebase has explicit anti-refactoring policies or immutability constraints2223## Instructions24251. **Scope the changeset**: Read all modified files. Understand the git diff context or entire file if no diff exists. Identify hotspots: deep nesting, long functions, repeated conditionals, overlapping abstractions.26272. **Apply the refactoring ladder** (in order):28 - **Naming**: Rename variables, functions, classes to clarify intent — `response.d` → `response.duration_ms`29 - **Extract**: Pull repeated logic, deeply nested blocks, or single-responsibility violations into dedicated functions or classes30 - **Consolidate**: Merge similar functions differing only in type or parameter order; use generics or polymorphism31 - **Eliminate**: Remove dead code, redundant null checks after type narrowing, unused parameters32 - **Patterns**: Apply established patterns (Builder for complex objects, Strategy for conditional logic, Dependency Injection for testability)33343. **Prioritize by ROI**: Recommend high-impact changes first (reducing complexity, unblocking tests, improving readability for critical paths). Lower priority: stylistic improvements or micro-optimizations.35364. **Preserve intent**: Refactoring must not alter observable behavior. If uncertain, propose as a suggestion with caveats.37385. **Provide concrete diffs**: Show before/after code snippets for each recommendation. For large refactors, offer a staged approach.39406. **Test impact**: If tests exist, confirm the refactoring doesn't break them. Flag edge cases that the refactoring exposes.4142## Example4344Given a TypeScript utility file with overlapping functions:4546```typescript47function parseJSON(input: string) {48 try {49 return JSON.parse(input);50 } catch (e) {51 return null;52 }53}5455function parseCSV(input: string) {56 try {57 const lines = input.split('\n');58 return lines.map(line => line.split(','));59 } catch (e) {60 return null;61 }62}6364function parseXML(input: string) {65 try {66 return new DOMParser().parseFromString(input, 'text/xml');67 } catch (e) {68 return null;69 }70}71```7273**Recommendation**: Extract error handling into a higher-order function, apply Strategy pattern for parsers:7475```typescript76const safelyParse = <T>(parser: (input: string) => T) => (input: string): T | null => {77 try {78 return parser(input);79 } catch {80 return null;81 }82};8384const parsers = {85 json: safelyParse(input => JSON.parse(input)),86 csv: safelyParse(input => input.split('\n').map(line => line.split(','))),87 xml: safelyParse(input => new DOMParser().parseFromString(input, 'text/xml')),88};89```9091**Impact**: Eliminates three duplicate try-catch blocks, centralizes error handling, enables easy addition of new parsers, improves testability of parsers in isolation.