refactor

GitHub

分析代码中的SOLID原则违规、代码异味及复杂度,提供针对性的重构建议和优先级排序,帮助提升代码质量和可维护性。

examples/skills/refactor/SKILL.md FlorianBruniaux/claude-code-ultimate-guide

Trigger Scenarios

请求分析代码的SOLID原则合规性 识别代码异味和反模式 评估代码复杂度和重复率 获取重构建议和优化方案

Install

npx skills add FlorianBruniaux/claude-code-ultimate-guide --skill refactor -g -y
More Options

Non-standard path

npx skills add https://github.com/FlorianBruniaux/claude-code-ultimate-guide/tree/main/examples/skills/refactor -g -y

Use without installing

npx skills use FlorianBruniaux/claude-code-ultimate-guide@refactor

指定 Agent (Claude Code)

npx skills add FlorianBruniaux/claude-code-ultimate-guide --skill refactor -a claude-code -g -y

安装 repo 全部 skill

npx skills add FlorianBruniaux/claude-code-ultimate-guide --all -g -y

预览 repo 内 skill

npx skills add FlorianBruniaux/claude-code-ultimate-guide --list

SKILL.md

Frontmatter
{
    "name": "refactor",
    "effort": "medium",
    "description": "Analyze code for SOLID violations and suggest targeted improvements",
    "when_to_use": "Use when a module has SOLID violations, code smells, or duplication to address.",
    "argument-hint": "<file_or_module> [--pattern <name>]",
    "disable-model-invocation": true
}

SOLID Refactoring Assistant

Analyze code for SOLID violations and suggest targeted improvements.

Purpose

Identify refactoring opportunities based on:

  • SOLID principle violations
  • Code smells and anti-patterns
  • Complexity metrics
  • Duplication detection

Instructions

Step 1: Scope Analysis

Determine the refactoring scope from user input:

  • Single file: Deep analysis
  • Directory: Pattern detection across files
  • Function/class: Focused extraction suggestions
# Get file/directory stats
if [ -f "$TARGET" ]; then
  wc -l "$TARGET"
  echo "Single file analysis"
elif [ -d "$TARGET" ]; then
  find "$TARGET" -type f \( -name "*.ts" -o -name "*.js" -o -name "*.py" \) | wc -l
  echo "Directory analysis"
fi

Step 2: SOLID Violations Detection

S - Single Responsibility

Look for:

  • Files > 300 lines
  • Functions > 50 lines
  • Classes with > 10 methods
  • Mixed concerns (data + UI + business logic)
# Find large files
find . -name "*.{ts,js,py}" -exec wc -l {} + 2>/dev/null | sort -rn | head -10

# Functions with high line count (approximate)
grep -rn "function\|def \|fn " --include="*.{ts,js,py,rs}" . | head -20

O - Open/Closed Principle

Look for:

  • Switch/case statements on types
  • Repeated if/else type checking
  • Direct modifications vs extensions

L - Liskov Substitution

Look for:

  • Overridden methods that throw "not implemented"
  • Type checks before method calls
  • Empty method overrides

I - Interface Segregation

Look for:

  • Large interfaces (> 10 methods)
  • Classes implementing unused interface methods
  • Fat service classes

D - Dependency Inversion

Look for:

  • Direct instantiation of dependencies (new Service())
  • Hardcoded class references
  • Missing dependency injection

Step 3: Code Smells

# Duplication patterns
grep -rn --include="*.{ts,js,py}" . 2>/dev/null | \
  awk -F: '{print $3}' | sort | uniq -c | sort -rn | head -10

# Long parameter lists (> 4 params)
grep -rn "function.*,.*,.*,.*," --include="*.{ts,js}" . 2>/dev/null | head -10

# Deep nesting (4+ levels)
grep -rn "^\s\{16,\}" --include="*.{ts,js,py}" . 2>/dev/null | head -10

Step 4: Complexity Assessment

For each issue found, assess:

  • Impact: How much code is affected?
  • Risk: What could break?
  • Effort: Lines to change, tests needed?

Output Format


🔧 Refactoring Analysis

Target: [file/directory] Lines Analyzed: [count]

📊 SOLID Scorecard

Principle Status Issues Found
Single Responsibility 🟡 3 large classes
Open/Closed 🟢 OK
Liskov Substitution 🟢 OK
Interface Segregation 🔴 2 fat interfaces
Dependency Inversion 🟡 5 direct instantiations

🎯 Priority Refactorings

1. [Highest Impact] - Extract class from UserService

Violation: Single Responsibility Current: 450 lines handling auth + profile + notifications Suggested:

UserService.ts (450 lines)
    ↓ Extract
AuthService.ts (~150 lines)
ProfileService.ts (~150 lines)
NotificationService.ts (~100 lines)

Risk: Medium (update imports) Tests Needed: Update dependency injection in tests

2. [Second Priority] - Replace switch with polymorphism

Location: src/handlers/payment.ts:45 Current:

switch (paymentType) {
  case 'card': // 50 lines
  case 'bank': // 50 lines
  case 'crypto': // 50 lines
}

Suggested: Strategy pattern with PaymentProcessor interface Risk: Low (isolated change)

📝 Code Smells

Smell Location Severity
Long Method api.ts:calculateTotal (120 lines) 🟠 High
Duplicate Code utils/*.ts (3 similar blocks) 🟡 Medium
Deep Nesting parser.ts:parse (6 levels) 🟡 Medium

🚀 Quick Wins (Low Risk, High Value)

  1. Extract validateEmail() to shared utils (used in 4 places)
  2. Replace magic numbers with named constants
  3. Add early returns to reduce nesting in processOrder()

⚠️ Technical Debt Notes

  • [Item to track for future sprints]

Refactoring Safety Checklist

Before applying suggestions:

  • Tests exist for affected code
  • Create feature branch
  • Commit current state
  • Apply one refactoring at a time
  • Run tests after each change
  • Review diff before committing

Usage

Analyze specific file:

/refactor src/services/user.ts

Analyze directory:

/refactor src/api/

Focus on specific principle:

/refactor --focus=srp src/services/

With complexity threshold:

/refactor --threshold=high

References

  • Martin Fowler's Refactoring Catalog
  • Clean Code by Robert C. Martin
  • SOLID principles by Robert C. Martin

$ARGUMENTS

Version History

  • fa57065 Current 2026-07-25 10:19

Same Skill Collection

.agents/skills/guide-recap/SKILL.md
.agents/skills/self-assessment/SKILL.md
.agents/skills/source-command-audit-prose/SKILL.md
.agents/skills/source-command-audit-whitepapers/SKILL.md
.agents/skills/source-command-ccguide-daily/SKILL.md
.agents/skills/source-command-ccguide-diff-docs/SKILL.md
.agents/skills/source-command-ccguide-init-docs/SKILL.md
.agents/skills/source-command-ccguide-refresh-docs/SKILL.md
.agents/skills/source-command-methodology-advisor/SKILL.md
.agents/skills/source-command-track-mentions/SKILL.md
.agents/skills/source-command-update-infos-release/SKILL.md
.claude/skills/self-assessment/SKILL.md
examples/skills/audit-agents-skills/SKILL.md
examples/skills/audit-codebase/SKILL.md
examples/skills/autoresearch/SKILL.md
examples/skills/canary/SKILL.md
examples/skills/catchup/SKILL.md
examples/skills/ccboard/SKILL.md
examples/skills/check-cache-bugs/SKILL.md
examples/skills/ci-all/SKILL.md
examples/skills/ci-pipeline/SKILL.md
examples/skills/ci-status/SKILL.md
examples/skills/ci-tests/SKILL.md
examples/skills/commit/SKILL.md
examples/skills/cyber-defense-team/SKILL.md
examples/skills/design-patterns/SKILL.md
examples/skills/diagnose/SKILL.md
examples/skills/eval-agents/SKILL.md
examples/skills/eval-hooks/SKILL.md
examples/skills/eval-rules/SKILL.md
examples/skills/eval-skills/SKILL.md
examples/skills/explain/SKILL.md
examples/skills/git-ai-archaeology/SKILL.md
examples/skills/git-worktree-clean/SKILL.md
examples/skills/git-worktree-remove/SKILL.md
examples/skills/git-worktree-status/SKILL.md
examples/skills/git-worktree/SKILL.md
examples/skills/guide-recap/SKILL.md
examples/skills/handoff-create/SKILL.md
examples/skills/handoff-resume/SKILL.md
examples/skills/handoff-update/SKILL.md
examples/skills/investigate/SKILL.md
examples/skills/issue-triage/SKILL.md
examples/skills/land-and-deploy/SKILL.md
examples/skills/landing-page-generator/SKILL.md
examples/skills/learn-alternatives/SKILL.md
examples/skills/learn-quiz/SKILL.md
examples/skills/learn-teach/SKILL.md
examples/skills/mcp-integration-reference/SKILL.md
examples/skills/methodology-advisor/SKILL.md

Metadata

Files
0
Version
a8d88fe
Hash
50273be9
Indexed
2026-07-25 10:19

- 위키
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-25 15:19
浙ICP备14020137号-1 $방문자$