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 Recommender4metadata:5 source: https://skillmd.com/skills/uitbreidenos/refactoring-recommender6---7# Refactoring Recommender89## When to activate1011- Review a diff or changed code for simplification, performance, or maintainability improvements12- Refactor legacy code to apply modern patterns or framework idioms13- Reduce cyclomatic complexity, eliminate duplication, or improve testability14- Modernize API surface, consolidate utility functions, or standardize naming conventions15- Assess code quality after feature development or merge conflicts1617## When NOT to use1819- For security vulnerability detection — use `/security-review` instead20- For automated bug detection — use `/code-review` instead21- For minor style fixes or linting — use standard formatters (Prettier, Black, gofmt)22- For architectural design decisions spanning multiple files — start with `/deep-research` to gather requirements first23- When the codebase has explicit anti-refactoring policies or immutability constraints2425## Instructions26271. **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.28292. **Apply the refactoring ladder** (in order):30 - **Naming**: Rename variables, functions, classes to clarify intent — `response.d` → `response.duration_ms`31 - **Extract**: Pull repeated logic, deeply nested blocks, or single-responsibility violations into dedicated functions or classes32 - **Consolidate**: Merge similar functions differing only in type or parameter order; use generics or polymorphism33 - **Eliminate**: Remove dead code, redundant null checks after type narrowing, unused parameters34 - **Patterns**: Apply established patterns (Builder for complex objects, Strategy for conditional logic, Dependency Injection for testability)35363. **Prioritize by ROI**: Recommend high-impact changes first (reducing complexity, unblocking tests, improving readability for critical paths). Lower priority: stylistic improvements or micro-optimizations.37384. **Preserve intent**: Refactoring must not alter observable behavior. If uncertain, propose as a suggestion with caveats.39405. **Provide concrete diffs**: Show before/after code snippets for each recommendation. For large refactors, offer a staged approach.41426. **Test impact**: If tests exist, confirm the refactoring doesn't break them. Flag edge cases that the refactoring exposes.4344## Example4546Given a TypeScript utility file with overlapping functions:4748```typescript49function parseJSON(input: string) {50 try {51 return JSON.parse(input);52 } catch (e) {53 return null;54 }55}5657function parseCSV(input: string) {58 try {59 const lines = input.split('\n');60 return lines.map(line => line.split(','));61 } catch (e) {62 return null;63 }64}6566function parseXML(input: string) {67 try {68 return new DOMParser().parseFromString(input, 'text/xml');69 } catch (e) {70 return null;71 }72}73```7475**Recommendation**: Extract error handling into a higher-order function, apply Strategy pattern for parsers:7677```typescript78const safelyParse = <T>(parser: (input: string) => T) => (input: string): T | null => {79 try {80 return parser(input);81 } catch {82 return null;83 }84};8586const parsers = {87 json: safelyParse(input => JSON.parse(input)),88 csv: safelyParse(input => input.split('\n').map(line => line.split(','))),89 xml: safelyParse(input => new DOMParser().parseFromString(input, 'text/xml')),90};91```9293**Impact**: Eliminates three duplicate try-catch blocks, centralizes error handling, enables easy addition of new parsers, improves testability of parsers in isolation.