← 返回列表
全部 Skill›工具 Tools›refactor

refactor

作者 @luongnv89 · 语言 en
Systematic code refactoring based on Martin Fowler's methodology. Use when users ask to refactor code, improve code structure, reduce technical debt, clean up legacy code, eliminate code smells, or improve code maintainability. This skill guides through a phased approach with research, planning, and safe incremental implementation.
工具 Tools生产力工具通用执行命令访问网络待翻译
最近源活动
—
检测 SKILL.md 语言
en
星标 ★
47,566
分支 Fork
0
安装选项
提示
指令
使用 AI 助手安装
将下面的提示复制到你正在使用的 AI 助手中。
下载完整 Skill 目录(含全部配套文件)。 下载压缩包 ↓
SKILL.md 原文预览

Code Refactoring Skill

A systematic approach to refactoring code based on Martin Fowler's *Refactoring: Improving the Design of Existing Code* (2nd Edition). This skill emphasizes safe, incremental changes backed by tests.

"Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure." — Martin Fowler

Core Principles

  1. Behavior Preservation: External behavior must remain unchanged
  2. Small Steps: Make tiny, testable changes
  3. Test-Driven: Tests are the safety net
  4. Continuous: Refactoring is ongoing, not a one-time event
  5. Collaborative: User approval required at each phase

Workflow Overview

Phase 1: Research & Analysis
    ↓
Phase 2: Test Coverage Assessment
    ↓
Phase 3: Code Smell Identification
    ↓
Phase 4: Refactoring Plan Creation
    ↓
Phase 5: Incremental Implementation
    ↓
Phase 6: Review & Iteration

Phase 1: Research & Analysis

Objectives

  • Understand the codebase structure and purpose
  • Identify the scope of refactoring
  • Gather context about business requirements

Questions to Ask User

Before starting, clarify:

  1. Scope: Which files/modules/functions need refactoring?
  2. Goals: What problems are you trying to solve? (readability, performance, maintainability)
  3. Constraints: Are there any areas that should NOT be changed?
  4. Timeline pressure: Is this blocking other work?
  5. Test status: Do tests exist? Are they passing?

Actions

  • [ ] Read and understand the target code
  • [ ] Identify dependencies and integrations
  • [ ] Document current architecture
  • [ ] Note any existing technical debt markers (TODOs, FIXMEs)

Output

Present findings to user:

  • Code structure summary
  • Identified problem areas
  • Initial recommendations
  • Request approval to proceed

Phase 2: Test Coverage Assessment

Why Tests Matter

"Refactoring without tests is like driving without a seatbelt." — Martin Fowler

Tests are the key enabler of safe refactoring. Without them, you risk introducing bugs.

Assessment Steps

  1. Check for existing tests

```bash

# Look for test files

find . -name "*test*" -o -name "*spec*" | head -20

```

  1. Run existing tests

```bash

# JavaScript/TypeScript

npm test

# Python

pytest -v

# Java

mvn test

```

  1. Check coverage (if available)

```bash

# JavaScript

npm run test:coverage

# Python

pytest --cov=.

```

Decision Point: Ask User

If tests exist and pass:

  • Proceed to Phase 3

If tests are missing or incomplete:

Present options:

  1. Write tests first (recommended)
  2. Add tests incrementally during refactoring
  3. Proceed without tests (risky - requires user acknowledgment)

If tests are failing:

  • STOP. Fix failing tests before refactoring
  • Ask user: Should we fix tests first?

Test Writing Guidelines (if needed)

For each function being refactored, ensure tests cover:

  • Happy path (normal operation)
  • Edge cases (empty inputs, null, boundaries)
  • Error scenarios (invalid inputs, exceptions)

Use the "red-green-refactor" cycle:

  1. Write failing test (red)
  2. Make it pass (green)
  3. Refactor

Phase 3: Code Smell Identification

What Are Code Smells?

Symptoms of deeper problems in code. They're not bugs, but indicators that the code could be improved.

Common Code Smells to Check

See references/code-smells.md for the complete catalog.

#### Quick Reference

| Smell | Signs | Impact |

|-------|-------|--------|

| Long Method | Methods > 30-50 lines | Hard to understand, test, maintain |

| Duplicated Code | Same logic in multiple places | Bug fixes needed in multiple places |

| Large Class | Class with too many responsibilities | Violates Single Responsibility |

| Feature Envy | Method uses another class's data more | Poor encapsulation |

| Primitive Obsession | Overuse of primitives instead of objects | Missing domain concepts |

| Long Parameter List | Methods with 4+ parameters | Hard to call correctly |

| Data Clumps | Same data items appearing together | Missing abstraction |

| Switch Statements | Complex switch/if-else chains | Hard to extend |

| Speculative Generality | Code "just in case" | Unnecessary complexity |

| Dead Code | Unused code | Confusion, maintenance burden |

Analysis Steps

  1. Automated Analysis (if scripts available)

```bash

python scripts/detect-smells.py <file>

```

  1. Manual Review
  • Walk through code systematically
  • Note each smell with location and severity
  • Categorize by impact (Critical/High/Medium/Low)
  1. Prioritization

Focus on smells that:

  • Block current development
  • Cause bugs or confusion
  • Affect most-changed code paths

Output: Smell Report

Present to user:

  • List of identified smells with locations
  • Severity assessment for each
  • Recommended priority order
  • Request approval on priorities

Phase 4: Refactoring Plan Creation

Selecting Refactorings

For each smell, select an appropriate refactoring from the catalog.

See references/refactoring-catalog.md for the complete list.

#### Smell-to-Refactoring Mapping

| Code Smell | Recommended Refactoring(s) |

|------------|---------------------------|

| Long Method | Extract Method, Replace Temp with Query |

| Duplicated Code | Extract Method, Pull Up Method, Form Template Method |

| Large Class | Extract Class, Extract Subclass |

评论
Lluongnv89
luongnv89
luongnv89/claude-howto
查看 GitHub 仓库 查看作者的其他 Skill
来源信息
作者@luongnv89
所属大类工具 Tools / 生产力工具
国内可用性通用
最近源活动—
风险提示执行命令、访问网络
提示:风险标签为程序静态扫描结果,仅作提示,不代表安全认证。安装前请阅读 SKILL.md 及其引用的脚本、权限,自行判断是否可信。