AgentSkillsCN

refactoring

在跨多个文件进行大规模变更,或实施重大代码重构时,此功能可助您事半功倍。

SKILL.md
--- frontmatter
name: refactoring
description: Use when coordinating large changes across multiple files or performing significant code restructuring

Refactoring

Announce at start: "Following the refactoring skill for multi-file changes."

Core Rule

Interface first, implementation second. Define contracts before changing internals.

Process

1. Inventory — Map the Blast Radius

Before changing anything:

  • List all files that will be touched
  • Identify dependency order (what depends on what)
  • Identify public interfaces that callers depend on
  • Run the full test suite and record the baseline

2. Plan — Order Changes by Dependency

OrderWhatWhy
FirstTypes/interfacesContracts must be stable before implementation
SecondCore logicInternal changes behind stable interfaces
ThirdCallers/consumersUpdate after interfaces are settled
LastTestsVerify everything works together

3. Execute — One Layer at a Time

For each file in dependency order:

  1. Make the minimal change to that file
  2. Run the type checker / compiler
  3. Run targeted unit tests
  4. Commit with a clear message
  5. Move to the next file

Do not batch multiple files into one uncommitted change.

4. Verify — Build Between Steps

After Each FileAfter Each LayerAfter All Changes
Type check passesBuild succeedsFull test suite passes
Relevant tests passIntegration tests passManual verification
Git status is cleanNo regressionsPR is reviewable size

Change Size Guidelines

SizeRecommendation
1-50 linesIdeal — easy to review
50-200 linesAcceptable — stay focused
200-500 linesSplit if possible
500+ linesMust split into multiple PRs

Rollback Strategy

Before starting, define how to undo:

StrategyWhen
git revertSingle-commit changes
Feature flagBehavior changes
Separate revert PRMulti-commit changes
Config toggleEnvironment-specific

Red Flags — STOP

SignalAction
Changing behavior during refactorSeparate into two PRs (refactor then feature)
Tests are failing and you don't know whyStop, diagnose before continuing
Change touches more than 10 filesBreak into smaller PRs
No test coverage for changed codeAdd tests before refactoring

Related Skills

WhenInvoke
Refactoring uncovers a bugdebugging
Need to update tests for refactored codetesting
Refactoring complete, ready to submitpr-writing
Refactoring started without a planplanning

Deep Reference

For principles, rationale, anti-patterns, and examples: guides/multi-file-refactoring/multi-file-refactoring.md