AgentSkillsCN

refactor

在测试护城河的保障下进行安全重构。当用户说“/refactor”、“重构这段代码”、“清理这段代码”,或希望在不改变代码行为的前提下重新组织代码时使用。在重构前后运行测试,确保代码修改的安全性。

SKILL.md
--- frontmatter
name: refactor
description: Safe refactoring with test guardrails. Use when the user says "/refactor", "refactor this", "clean up this code", or wants to restructure code without changing behavior. Runs tests before and after to ensure safety.
allowed-tools:
  - Bash
  - Read
  - Write
  - Edit
  - Glob
  - Grep

Refactor

安全重构:测试基线 → 重构 → 验证绿灯 → 展示 diff。

Workflow

1. Understand Scope

Clarify with the user:

  • What to refactor (file, function, module)
  • What kind of refactoring (extract, rename, simplify, reorganize)
  • What NOT to change

2. Baseline Tests

Run the test suite BEFORE any changes:

bash
[test command]

All tests must pass. If they don't, fix tests first (or ask user) before refactoring.

Record the baseline: N tests, all passing.

3. Read the Code

Read the target code thoroughly. Understand:

  • Current structure and responsibilities
  • Dependencies (who calls this, what does it call)
  • Edge cases handled

4. Plan the Refactoring

Describe what you'll change and why. Common refactoring types:

TypeWhen
Extract function函数太长或有重复逻辑
Rename命名不清晰
Simplify conditionals嵌套过深或逻辑复杂
Move/reorganize职责错位
Remove dead code未使用的代码
Reduce duplicationDRY 原则

5. Execute

Apply changes incrementally:

  • One refactoring step at a time
  • Run tests after each step
  • If tests break, revert and retry differently

6. Verify

Run the full test suite:

bash
[test command]

All baseline tests must still pass. Show the output.

7. Show Diff

bash
git diff

Present the diff so the user can review the changes.

Rules

  • 不改变行为——所有现有测试必须通过
  • 不添加功能——重构和 feature 分开做
  • 不一次改太多——小步快跑,每步验证
  • 如果没有测试覆盖,先建议添加测试再重构