agentleFS
smileynetsmileynet/code-spice/skills/refactoring-patterns/SKILL.md

refactoring-patterns

Skill0 starsChanged 9 months agoMITView on GitHub

Refactoring patterns, techniques, and decision frameworks. Use when improving existing code structure, reducing complexity, eliminating code smells, choosing between refactoring approaches, or deciding when refactoring is worthwhile. Covers extraction, encapsulation, type-based refactoring, strategy patterns, modularity improvements, and compiler-guided transformation.

9 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

Refactoring Patterns

The Refactoring Pattern Catalog

PatternWhat It DoesWhen to UseRisk
Extract MethodBreak long function into named stepsFunction exceeds 5-7 lines or mixes abstraction levelsLow
Inline MethodRemove unnecessary indirectionMethod adds no clarity; just delegatesLow
Replace Type Code with ClassesTurn enums/constants into class hierarchiesif/else or switch on type codesMedium
Push Code into ClassesMove logic from callers into the class it belongs toLogic about a class lives outside itMedium
Specialize MethodCreate focused versions of overly general methodsMethod has parameters used only in some call sitesLow
Try Delete Then CompileDelete code and let the compiler verify safetySuspected dead code, unused parametersLow
Unify Similar ClassesMerge classes that differ only in constantsNear-duplicate classes with identical structureMedium
Combine IfsMerge consecutive ifs with identical bodiesRepeated conditional blocksLow
Introduce Strategy PatternExtract varying behavior into strategy objectsMultiple classes share structure but differ in behaviorHigh
Extract InterfaceCreate interface from existing implementationNeed to decouple consumers from concrete classMedium
Eliminate Getter/SetterMove behavior to where the data livesExternal code pulls data, operates, pushes backMedium
Encapsulate DataWrap related variables in a classLoose variables travel together across functionsMedium
Enforce SequenceUse constructors to guarantee operation orderingMethods must be called in a specific orderMedium

When to Refactor

Is there a concrete problem?
├── No → Don't refactor. "If it ain't broke, don't fix it."
└── Yes → What kind of problem?
    ├── Hard to understand → Extract Method, rename, Encapsulate Data
    ├── Hard to change → Push Code into Classes, Extract Interface
    ├── Duplicated logic → Unify Similar Classes, Introduce Strategy
    ├── Scattered conditionals → Replace Type Code with Classes
    ├── Wrong abstraction level → Inline Method, Specialize Method
    └── Dead or unused code → Try Delete Then Compile

Smell-to-Pattern Mapping

Structural Smells

SmellSignalRefactoring Response
Long methodFunction exceeds 5-7 lines of logicExtract Method
Mixed abstraction levelsFunction both calls methods and does low-level workExtract Method
Deep nestingConditionals nested 3+ levelsExtract Method, guard clauses
if/else chainsType-checking with if/else on enumsReplace Type Code with Classes
Getters and settersExternal code pulls data, transforms, pushes backEliminate Getter/Setter
Common prefixes/suffixesMultiple functions share a prefix like player_*Encapsulate Data
Primitive obsessionRelated primitives passed togetherEncapsulate Data

Dependency Smells

SmellSignalRefactoring Response
Hard-coded dependenciesConstructor creates its own collaboratorsInject dependencies through constructor
Concrete class couplingCode depends on implementation, not interfaceExtract Interface
Feature envyMethod uses another class's data more than its ownPush Code into Classes
Leaking implementation detailsReturn types expose internal layersWrap in domain-appropriate types
Global stateShared mutable state makes code unsafeInject shared state as explicit dependency

The Invariant Hierarchy

Push invariants as high up this hierarchy as possible. Levels 1-2 are dramatically more reliable than 3-6.

LevelMechanismReliability
1. Eliminate the invariantRedesign so the problem can't occurHighest
2. Compiler-enforcedType system prevents violationsHigh
3. Runtime-enforcedAssertions, guards, validationMedium
4. DocumentedComments, README, wikiLow
5. VerbalTold during onboardingVery low
6. Hoped forUndocumented assumptionNone

"Which refactoring do I need?"

You're struggling with...Primary PatternSupporting Pattern
Function too long to understandExtract MethodSpecialize Method
if/else chains on type codesReplace Type Code with ClassesPush Code into Classes
Changing one thing breaks anotherExtract InterfaceDependency injection
Duplicated code across classesUnify Similar ClassesIntroduce Strategy Pattern
Data exposed through gettersEliminate Getter/SetterPush Code into Classes
Related variables scatteredEncapsulate DataEnforce Sequence
Suspected dead codeTry Delete Then Compile—
Methods must be called in orderEnforce SequenceEncapsulate Data
Deep inheritance treeExtract Interface + compositionIntroduce Strategy Pattern

Refactoring Checklist

Before:

  • Tests exist for the code being refactored (or write them first)
  • Problem is concrete — not refactoring for aesthetics
  • Scope is bounded — refactoring one thing at a time

During:

  • One pattern at a time — apply, verify, commit, then next
  • Tests pass after each step
  • Behavior unchanged — refactoring changes structure, not behavior

After:

  • All tests still pass
  • Code is simpler — if complexity increased, reconsider
  • Names updated — new structure deserves new names
  • Dead code deleted — refactoring often reveals unnecessary code

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.