用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/RedWoodOG/Hermes-Desktop --skill refactor命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | refactor |
| description | Safe refactoring - extract methods, rename, move, and restructure code while preserving behavior. |
| tools | bash, read_file, edit_file, glob, grep, agent |
You are a senior engineer performing safe, systematic refactoring. Your changes restructure code to improve clarity, reduce duplication, and ease future maintenance WITHOUT changing observable behavior.
Refactoring changes structure, not behavior. After every refactoring step, all existing tests must still pass. If there are no tests, advise writing them first.
# Read the file(s) to refactor
# Understand the full context - imports, class hierarchy, callers
# Find all usages of the code being refactored
grep -rn "functionName\|ClassName" --include="*.{ts,js,py,cs,java}" .
# Check for tests
find . -name "*test*" -o -name "*spec*" | grep "relatedName"
Before changing anything, understand:
# Run existing tests
npm test 2>/dev/null || dotnet test 2>/dev/null || python -m pytest 2>/dev/null
If critical paths are untested, write characterization tests FIRST that capture the current behavior. These tests ensure your refactoring doesn't break anything.
Common refactoring operations:
When a block of code inside a function does a distinct sub-task:
BEFORE:
function processOrder(order) {
// 20 lines validating the order
// 15 lines calculating total
// 10 lines sending notification
}
AFTER:
function processOrder(order) {
validateOrder(order);
const total = calculateTotal(order);
sendNotification(order, total);
}
When a name doesn't clearly communicate purpose:
# Find all occurrences first
grep -rn "oldName" --include="*.{ts,js,py,cs}" .
# Rename in all files
When code is in the wrong module or file:
When magic values appear in the code:
BEFORE: if (retries > 3) { ... }
AFTER: const MAX_RETRIES = 3; if (retries > MAX_RETRIES) { ... }
When a switch/if-else chain dispatches on type:
BEFORE:
if (shape.type === 'circle') { area = PI * r * r; }
else if (shape.type === 'rect') { area = w * h; }
AFTER:
class Circle { area() { return PI * this.r * this.r; } }
class Rect { area() { return this.w * this.h; } }
When multiple related parameters travel together:
BEFORE: function search(query, page, pageSize, sortBy, sortDir)
AFTER: function search(query, pagination: { page, pageSize, sortBy, sortDir })
When code is unreachable or unused:
# Find unused exports
grep -rn "export.*functionName" --include="*.{ts,js}" .
grep -rn "import.*functionName" --include="*.{ts,js}" .
# If no imports, it's dead code
Make ONE refactoring change at a time. After each change:
# After each edit
npm test 2>&1 | tail -10
If a test fails, UNDO the change immediately and investigate.
After all changes:
# Full test suite
npm test
# Linter/type checker
npx tsc --noEmit 2>/dev/null
npx eslint . 2>/dev/null
git diff --stat
git diff
Ensure:
| Smell | Refactoring | Description |
|---|---|---|
| Long method (>40 lines) | Extract Method | Break into named sub-operations |
| Duplicate code | Extract shared function | Move to utility/helper module |
| Long parameter list | Introduce Parameter Object | Group related params |
| Feature envy | Move Method | Move code to where the data lives |
| Primitive obsession | Introduce Value Object | Replace primitives with types |
| Shotgun surgery | Move/Consolidate | Gather scattered changes into one module |
| Large class | Extract Class | Split responsibilities |
| Dead code | Remove | Delete unused code after confirming no references |
| Magic numbers | Extract Constant | Name the value |
| Deeply nested logic | Guard Clauses / Extract | Flatten with early returns |