AgentSkillsCN

Refactoring Patterns

重构模式

SKILL.md
--- frontmatter
applyTo: "**/*refactor*,**/*extract*,**/*rename*,**/*inline*"

Refactoring Patterns Skill

Safe transformations — same behavior, better structure.

Golden Rule

Tests pass before AND after.

When to Refactor

TriggerAction
Feature is hard to addRefactor first
Same bug twiceRefactor to prevent
"I don't understand"Refactor for clarity

When NOT to Refactor

  • No tests + time pressure
  • Code won't change
  • Right before release
  • Should rewrite instead

Core Moves

MoveWhen
Extract FunctionDoes too many things
Inline FunctionBody clear as name
Extract VariableComplex expression
RenameName doesn't reveal intent
Move FunctionUses other class's data

Code Smells → Fix

SmellRefactoring
Long functionExtract Function
Long param listParameter Object
Duplicate codeExtract Function
Feature envyMove Function
Large classExtract Class

Refactor vs Rewrite

RefactorRewrite
Core logic soundDesign is wrong
Tests existUntestable
<30% changes>70% changes

Safe Workflow

Commit → Test → Small change → Test → Commit → Repeat

Synapses

See synapses.json for connections.