Files
Claude Agent 45e28e7e94 feat(workflow): Add workflow plugin v1.0.0
Git workflow, code quality, context management and testing commands:
- commit, push, pr, issue - git operations
- simplify, refactor, verify, check - code quality
- catchup, onboard, save, cleanup - context management
- test, format, sync - development utilities

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-22 15:23:48 +00:00

3.9 KiB

description: Detect code patterns and suggest improvements argument-hint: [file-or-pattern] - Optional file or area to focus refactoring on

Refactor

Pre-computed Context

Project Structure: !find . -type f -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.py" 2>/dev/null | head -20 | xargs -I{} dirname {} | sort -u | head -10

Recent Changes: !git diff --name-only HEAD~10 2>/dev/null | head -15

Large Files (potential refactor targets): !find . -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.py" 2>/dev/null | xargs wc -l 2>/dev/null | sort -rn | head -10


Instructions

You are analyzing code for refactoring opportunities. Focus on improving maintainability without changing behavior.

Analysis Targets

If argument provided, focus on that file/area. Otherwise, analyze recent changes and large files.

Patterns to Detect

1. Code Duplication

Look for similar code blocks that could be extracted:

// Before: duplicated in multiple places
const userA = await db.query('SELECT * FROM users WHERE id = ?', [idA]);
const userB = await db.query('SELECT * FROM users WHERE id = ?', [idB]);

// After: extracted function
const getUser = (id) => db.query('SELECT * FROM users WHERE id = ?', [id]);
const userA = await getUser(idA);
const userB = await getUser(idB);

2. Long Functions

Functions over 50 lines are candidates for splitting:

// Before: 100-line function doing multiple things
function processOrder(order) {
  // validate (20 lines)
  // calculate totals (30 lines)
  // apply discounts (25 lines)
  // save to database (25 lines)
}

// After: composed smaller functions
function processOrder(order) {
  validateOrder(order);
  const totals = calculateTotals(order);
  const finalPrice = applyDiscounts(totals);
  return saveOrder(order, finalPrice);
}

3. Deep Nesting

More than 3 levels of nesting hurts readability:

// Before: deeply nested
if (user) {
  if (user.isActive) {
    if (user.hasPermission) {
      // do something
    }
  }
}

// After: early returns
if (!user || !user.isActive || !user.hasPermission) return;
// do something

4. Magic Numbers/Strings

Replace with named constants:

// Before
if (status === 3) { ... }
setTimeout(fn, 86400000);

// After
const STATUS_COMPLETED = 3;
const ONE_DAY_MS = 24 * 60 * 60 * 1000;
if (status === STATUS_COMPLETED) { ... }
setTimeout(fn, ONE_DAY_MS);

5. God Objects/Files

Files doing too much should be split:

  • Over 500 lines
  • Multiple unrelated responsibilities
  • Too many imports from different domains

6. Inconsistent Patterns

Different patterns for the same thing:

// Inconsistent: some use async/await, some use .then()
const dataA = await fetchA();
fetchB().then(dataB => { ... });

// Consistent: all async/await
const dataA = await fetchA();
const dataB = await fetchB();

Process

  1. Analyze - Read target files, identify patterns
  2. Prioritize - Rank by impact and risk
  3. Propose - Explain each refactoring opportunity
  4. Implement - Apply changes if user approves
  5. Verify - Run tests to ensure no regressions

Report Format

## Refactoring Opportunities

### High Priority
1. **Extract duplicated user fetching** (5 occurrences)
   - Files: src/api/*.ts
   - Benefit: Remove 45 duplicated lines

2. **Split UserService class** (380 lines)
   - Current: handles auth, profile, preferences
   - Proposed: UserAuthService, UserProfileService, UserPreferencesService

### Medium Priority
3. **Replace magic numbers in config.ts**
   - 12 magic numbers found

### Low Priority
4. **Standardize error handling pattern**
   - Mix of try/catch and .catch()

Important Notes

  • Refactoring should NOT change behavior
  • Always run tests after refactoring
  • Do one refactoring at a time for easy rollback
  • Document why the refactoring improves the code