agentleFS
irahardiantoirahardianto/awesome-agv/.agents/skills/refactoring-patterns/SKILL.md

refactoring-patterns

Skill157 starsChanged 3 months agoMITView on GitHub

Safe refactoring pattern catalog: code smell taxonomy, transformation techniques, behavior preservation strategies, and metrics tracking.

6 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. 0 are common, 4 are distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 5 edited versions we read keep these.

  • 4

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Before refactoring, write tests that capture current behavior:

    line 41
  2. Distinctive:

    Create abstraction (interface) over existing implementation

    line 56
  3. Distinctive:

    Create new implementation behind abstraction

    line 58
  4. Distinctive:

    Run old and new implementations simultaneously, compare outputs:

    line 63

“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Refactoring Patterns

Catalog of safe, incremental code transformation patterns.

When to Invoke

  • Executing /refactor command
  • Technical debt remediation
  • Pattern migration (e.g., callbacks → async/await)
  • Code smell resolution from static analysis findings

Code Smell Taxonomy

Structural Smells

SmellDetectionRefactoring
Long method>30 linesExtract method
Large class>300 lines or >5 responsibilitiesExtract class
Long parameter list>4 parametersIntroduce parameter object
Feature envyMethod uses another class's data more than its ownMove method
Data clumpsSame fields appear together repeatedlyExtract class
Primitive obsessionPrimitives where domain types belongIntroduce value object

Coupling Smells

SmellDetectionRefactoring
God classOne class knows/does too muchExtract classes by responsibility
Circular dependencyA → B → AIntroduce interface, dependency inversion
Inappropriate intimacyClasses access each other's internalsMove method, extract interface
Shotgun surgeryOne change requires touching many filesMove related code together

Safe Transformation Techniques

1. Characterization Tests

Before refactoring, write tests that capture current behavior:

Run existing code → record outputs → assert in tests → now refactor safely

2. Strangler Fig Pattern

Gradually replace old implementation with new:

Old Code ← requests
         ↓ gradually route to
New Code ← requests (eventually all traffic)
Old Code ← delete

3. Branch by Abstraction

  1. Create abstraction (interface) over existing implementation
  2. Modify all clients to use abstraction
  3. Create new implementation behind abstraction
  4. Switch to new implementation
  5. Remove old implementation

4. Parallel Run

Run old and new implementations simultaneously, compare outputs:

Request → Old Implementation → Response (returned to user)
       → New Implementation → Response (logged, compared)

Behavior Preservation Checklist

  • Tests pass before starting
  • Each step is a single, atomic change
  • Tests pass after each step
  • Same inputs produce same outputs
  • Coverage ≥ pre-refactoring level
  • No new public API surface (unless intentional)
  • Complexity metrics improved or unchanged

Metrics

MetricToolTarget
Cyclomatic complexitySonarQube, radon, gocyclo≤ 10 per function
Cognitive complexitySonarQube≤ 15 per function
Coupling (afferent/efferent)JDepend, NDependDecreasing trend
DuplicationSonarQube, jscpd< 3%
Test coveragelcov, coverage.py≥ pre-refactoring

Related

  • Code Review @.agents/skills/code-review/SKILL.md
  • Guardrails @.agents/skills/guardrails/SKILL.md
  • Testing Strategy .agents/rules/testing-strategy.md

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.