‹ 首页

refactoring

@developersglobal · 收录于 1 周前

Safe, behavior-preserving code transformation backed by tests. Refactor with evidence, not instinct.

适合你,如果需要在修改代码结构时确保功能不变

/ 下载安装
refactoring.skill双击,或拖进 Claude 桌面版 / Cowork,即完成安装↓ .skill↓ .zip
用别的 agent?下载 .zip 解压,把文件夹放进它的技能目录
Claude Code~/.claude/skills/(项目级 .claude/skills/)
Codex CLI~/.codex/skills/
Cursor自动读取上面两处目录
其他工具见其文档的「skills」目录;两个下载是同一份文件,只是名字不同
/ 通过 npx 安装 校验哈希
npx oh-my-skill add developersglobal/ai-agent-skills/refactoring
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- developersglobal/ai-agent-skills/refactoring
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify developersglobal/ai-agent-skills/refactoring
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
64GitHub stars
~600上下文体积 · 单文件
镜像托管

怎么用

技能原文 SKILL.md作者撰写 · MIT · f47f948
Overview

Refactoring changes the internal structure of code without changing its external behavior. The keyword is "without" — if behavior changes, it's not refactoring, it's modification. This skill enforces safe refactoring with tests as the safety net.

When to Use
  • When code is hard to understand or extend
  • When duplication makes maintenance risky
  • Before adding a feature to a messy area of code
  • Never: "while also adding feature X" — refactor separately
Process
Step 1: Establish a Safety Net
  1. Before touching a single line: ensure there are tests covering the code to be refactored.
  2. If tests are missing: add characterization tests first. These capture current behavior, not desired behavior.
  3. Run the tests. They should all pass. This is your baseline.

Verify: Tests pass. They cover the code being refactored.

Step 2: Refactor in Micro-Steps
  1. Make the smallest meaningful change.
  2. Run tests after EVERY change — not after 10 changes.
  3. If tests break: revert immediately and take a smaller step.
  4. Never batch multiple refactoring changes together.

Verify: Tests pass after every individual change.

Step 3: One Thing at a Time
  1. Refactoring types cannot be mixed in one step:
  2. Extract method → separate commit
  3. Rename → separate commit
  4. Move → separate commit
  5. "Refactor and also fix this" is not refactoring — it's two PRs.

Verify: This commit does exactly one type of refactoring.

Step 4: Verify No Behavior Change
  1. Run the full test suite.
  2. If integration/E2E tests exist: run them too.
  3. Compare external API responses before and after (if applicable).

Verify: All tests pass. No observable behavior change.

Common Rationalizations (and Rebuttals)

| Excuse | Rebuttal | |--------|----------| | "I'll add tests after refactoring" | You can't verify a behavior-preserving refactor without tests before the refactor. | | "This change is obviously safe" | Obvious safety is how production incidents happen. Run the tests. | | "I'll just do a quick cleanup" | "Quick cleanup" that changes behavior is a bug, not a refactor. |

Verification
  • [ ] Tests existed before any code was changed
  • [ ] Tests run after every individual change
  • [ ] Only one type of refactoring per commit
  • [ ] Full test suite passes at the end
  • [ ] No behavior change observable externally
References
  • [test-driven-development skill](../test-driven-development/SKILL.md)
  • [simplicity-first skill](../simplicity-first/SKILL.md)
  • Martin Fowler, "Refactoring"
按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。