AgentSkillsCN

incremental-refactor

为安全、渐进式的重构提供约束。适用于用户想要清理代码、去除重复、提炼概念或改善结构而不改变行为。在Claude Code会话中,生成一个约束块,用于卡片的Context部分。触发条件包括“重构这个”、“清理这个”、“这里有重复”、“这感觉不对”、“提炼这个”,或在代码审查中发现结构性问题时。

SKILL.md
--- frontmatter
name: incremental-refactor
description: >
  Provides constraints for safe, incremental refactoring. Use when the user wants
  to clean up code, remove duplication, extract a concept, or improve structure
  without changing behavior. In Claude Code sessions, produces a constraint block
  for the card's Context section. Trigger on phrases like "refactor this",
  "clean this up", "there's duplication here", "this feels wrong", "extract this",
  or when code has structural problems identified during review.

Incremental Refactoring

Core idea

Refactoring is changing structure without changing behavior. The safety net is the test suite. If tests aren't green before you start, fix that first.


How to use this skill in Claude Code

In Claude Code sessions, paste the constraint block below into the card's ## Context before starting the session. Claude Code applies it during planning — not as an interactive loop.


Constraint block

Copy this into the card's ## Context:

code
## Design constraints — incremental refactoring

- Tests must be green before the first change. If they aren't, stop and report.
- One logical change at a time. Run tests after each change.
- If tests go red: undo the last change immediately. Understand why. Try smaller.
- Do not mix refactoring with behavior changes in the same task.
- Do not extract an abstraction until the duplication is obvious.
  Apply the flocking rules: find the most alike things, find the smallest difference,
  make the simplest change to remove that difference. Repeat.
- Do not refactor code that has no tests. Write a characterization test first.
- No speculative abstractions — only extract what two or more concrete cases demand.

The flocking rules

When facing duplication or unclear abstractions:

  1. Find the things that are most alike.
  2. Find the smallest difference between them.
  3. Make the simplest change to eliminate that difference.

Repeat until the pattern is obvious enough to name and extract cleanly. The right abstraction is discovered, not designed.


Common safe moves (each is one step)

MoveSafe when
Rename variable/functionTests still pass
Extract variableBehavior identical, name is more expressive
Extract functionNew function does exactly what the old code did
Inline functionCalled once or name adds no clarity
Move functionCohesion improves, no circular dependencies introduced
Replace magic value with named constantAll uses updated

When there are no tests

  1. Write a characterization test — captures current behavior, whatever it is.
  2. Use it as the safety net.
  3. Then refactor.

Never refactor untested code without a safety net.


Signals the constraint is being violated

  • Multiple structural changes in a single task
  • Behavior change mixed with a structural change
  • Abstraction extracted before two concrete cases exist
  • Tests skipped "because the refactor is obviously safe"

When you see these, stop. Make the change smaller.


Interactive use (pairing sessions, not Claude Code)

If working interactively: one change, run tests, repeat. The cycle is the discipline. Never accumulate untested changes. If uncertain whether a change is safe, make it smaller.