AgentSkillsCN

refactoring

借助“提取方法”“重命名”“移动方法”等技巧,在不改变程序行为的前提下,安全地重构代码。在为新功能做准备,或逐步提升代码质量时,这一方法尤为适用。

SKILL.md
--- frontmatter
name: refactoring
description: Safely restructure code without changing behavior using Extract Method, Rename, Move Method techniques. Use when preparing code for new features or improving code quality incrementally.

Applicability Rubric

ConditionPassFail
Existing code modificationNeed to change existing codeWriting new code only
Code comprehension issuesCode hard to understand/extendCode is clear
Feature preparationPreparing for new functionalityDirect implementation possible
Incremental improvementImproving quality step by stepNo improvement needed

Apply when: Any condition passes

Core Principles

The Refactoring Cycle

code
1. Ensure tests exist (or add them)
   ↓
2. Make small change
   ↓
3. Run tests
   ↓
4. Commit if green
   ↓
5. Repeat

Golden Rules

  • Never refactor and change behavior simultaneously
  • Always have tests before refactoring
  • Small steps, frequent commits
  • If tests fail, revert immediately

Common Refactoring Techniques

Code Organization

TechniqueWhen to UseBefore → After
Extract MethodLong method, repeated codeInline code → Named method
Extract ClassClass has multiple responsibilitiesOne class → Two classes
Move MethodMethod uses another class moreA.method() → B.method()
RenameName doesn't reveal intentdelapsedDays

Simplification

TechniqueWhen to UseBefore → After
Replace Conditional with PolymorphismType-based switchingif/switch → Subclasses
Replace Magic NumberUnexplained literals86400SECONDS_PER_DAY
Remove Dead CodeUnused codeCode → Nothing
Simplify ConditionalComplex boolean logicNested ifs → Guard clauses

Dealing with Dependencies

TechniqueWhen to UseBefore → After
Extract InterfaceNeed to mock or swapConcrete → Interface + Concrete
Inject DependencyHard-coded dependencynew Dep() → Constructor param
Replace Inheritance with DelegationInheritance misusedextends → has-a

Safe Refactoring Steps

Extract Method

  1. Identify code to extract
  2. Create new method with descriptive name
  3. Copy code to new method
  4. Replace original code with method call
  5. Run tests
  6. Commit

Rename

  1. Find all usages
  2. Rename (use IDE refactoring if available)
  3. Update documentation/comments
  4. Run tests
  5. Commit

Move Method

  1. Copy method to target class
  2. Adjust for new context
  3. Update original to delegate
  4. Run tests
  5. Remove original method
  6. Run tests
  7. Commit

Completion Rubric

Before Refactoring

CriterionPassFail
Test coverageTests exist and passNo tests or failing tests
Behavior understandingCurrent behavior understoodUnclear behavior
Clear goalRefactoring goal definedNo clear objective
Team awarenessTeam knows the scopeUndisclosed changes

During Refactoring

CriterionPassFail
Single focusOne refactoring at a timeMultiple simultaneous changes
Test validationTests run after each changeNo test verification
Incremental commitsCommit after each stepLarge uncommitted changes
Behavior preservationNo behavior changesBehavior modified

After Refactoring

CriterionPassFail
Tests passingAll tests still passTests failing
Code clarityCode is cleaner/clearerSame or worse clarity
No new featuresNo functionality addedFeatures added
Review completedChanges reviewedNo review

Code Smells to Watch For

SmellIndicationRefactoring
Long MethodMethod > 20 linesExtract Method
Large ClassClass > 200 linesExtract Class
Long Parameter List> 3 parametersIntroduce Parameter Object
Duplicated CodeSame code in multiple placesExtract Method/Class
Feature EnvyMethod uses other class's dataMove Method
Data ClumpsSame data groups appear togetherExtract Class