refactoring-best-practices
Skill5 starsChanged 30 days agoMITView on GitHub
Use when changing existing structure without changing behavior, adding characterization tests, isolating hard dependencies, consolidating APIs, migrating callers, introducing value objects or domain events, replacing null/string/generic exceptions with typed failure contracts, or removing legacy paths safely. Applies to protected incremental change, not an unverified rewrite or unrelated cleanup.
In this repository11 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- boostSkill
- data-migration-best-practicesSkill
- ddd-best-practicesSkill
- design-patterns-best-practicesSkill
- flowSkill
- fp-best-practicesSkill
- infrastructure-designSkill
- oop-best-practicesSkill
- rest-api-best-practicesSkill
- tdd-best-practicesSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
20 rules. About 0 are common, 6 are distinctive, and 14 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
14
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 12
Use this skill when the main challenge is changing existing code safely.
- Distinctive: line 17
Prefer small reversible moves over dramatic rewrites.
- Distinctive: line 18
Add feedback before adding abstraction.
- Distinctive: line 29
Prefer characterization tests around visible behavior.
- Distinctive: line 30
Add logs or temporary probes only when tests are not enough.
- Distinctive: line 34
Create the narrowest possible boundary around the risky dependency.
- Very specific: line 83
Read references/compatibility-and-migration.md when changing callers, deleting legacy paths, or migrating persisted representations.
- Very specific: line 85
Read references/safe-change-workflow.md for seam-based refactoring guidance, sensing and separation, and the legacy code change algorithm.
- Very specific: line 86
Read references/refactoring-moves.md for tactical moves, including the incremental Value Object migration sequence, and when to use them.
- Very specific: line 87
Read references/code-smells.md when recognizing a problem, using temporal co-change as design evidence, and choosing the right move.
- Very specific: line 88
Read references/legacy-code-techniques.md for Sprout, Wrap, Extract and Override, and other techniques for working without tests.
- Very specific: line 89
Read references/characterization-tests.md for how to write tests before refactoring untested code.
- Very specific: line 90
Read references/domain-event-migration.md for incrementally moving legacy side effects to events/subscribers, preserving failure semantics, …
- Very specific: line 91
Read references/error-contract-migration.md for safely replacing nulls, strings, and generic exceptions while preserving failure timing, …
- Very specific: line 92
Read references/fran-iglesias-refactoring-guidance.md for practical refactoring heuristics distilled from Fran Iglesias.
- Very specific: line 93
Read references/language-examples.md for before/after style examples in multiple languages.
- Very specific: line 97
Use oop-best-practices for everyday new code decisions.
- Very specific: line 98
Use design-patterns-best-practices when the main issue is choosing an object collaboration pattern.
- Very specific: line 99
Use ddd-best-practices when moving invariants, splitting a God Aggregate, introducing a root, changing a consistency boundary, or shaping a …
- Very specific: line 100
Use data-migration-best-practices for moving or backfilling persisted data.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Refactoring Best Practices
Use this skill when the main challenge is changing existing code safely.
Working Style
- Protect behavior before improving design.
- Prefer small reversible moves over dramatic rewrites.
- Add feedback before adding abstraction.
- Change one responsibility at a time.
- Let the current pain point decide the next move.
Safe Refactoring Workflow
- Observe current behavior.
- Identify outputs, side effects, and error paths.
- Identify what must not change.
- Add feedback.
- Prefer characterization tests around visible behavior.
- Add logs or temporary probes only when tests are not enough.
- Find a seam.
- Isolate time, file system, network, framework globals, singletons, and external APIs.
- Create the narrowest possible boundary around the risky dependency.
- Choose the next move.
- extract method
- extract class
- introduce value object
- introduce first-class collection
- move method
- replace conditional with polymorphism
- separate construction from behavior
- extract a Domain Event and one secondary subscriber
- Re-run feedback after every meaningful step.
High-Value Refactoring Moves
- Replace a cohesive domain parameter group with a Value Object; use a Parameter Object when the group has no shared domain meaning or invariant.
- Break large services into role-focused collaborators.
- Move business rules out of controllers, scripts, and utility classes.
- Replace type codes and unstable conditionals with explicit roles.
- Wrap infrastructure behind ports or adapters.
- Split classes when different method clusters change for different reasons.
Red Flags
- Big-bang rewrites.
- New abstractions without a protected behavior baseline.
- Splitting code into tiny classes without a clearer model.
- Introducing inheritance only to make tests easier.
- Refactoring based on aesthetics alone while ignoring risk.
Decision Rules
Refactor now when
- the same knowledge is duplicated in multiple places
- the code is blocking a real change
- the next feature would deepen coupling or duplication
- the current structure makes defects likely
Wait when
- there is no feedback loop yet
- the pain is hypothetical
- the abstraction is not yet stable enough to deserve a new type
- the change is broad but the understanding is still weak
References
- Read
references/compatibility-and-migration.mdwhen changing callers, deleting legacy paths, or migrating persisted representations. - Read
references/safe-change-workflow.mdfor seam-based refactoring guidance, sensing and separation, and the legacy code change algorithm. - Read
references/refactoring-moves.mdfor tactical moves, including the incremental Value Object migration sequence, and when to use them. - Read
references/code-smells.mdwhen recognizing a problem, using temporal co-change as design evidence, and choosing the right move. - Read
references/legacy-code-techniques.mdfor Sprout, Wrap, Extract and Override, and other techniques for working without tests. - Read
references/characterization-tests.mdfor how to write tests before refactoring untested code. - Read
references/domain-event-migration.mdfor incrementally moving legacy side effects to events/subscribers, preserving failure semantics, durable handoff, and CDC as a migration bridge. - Read
references/error-contract-migration.mdfor safely replacing nulls, strings, and generic exceptions while preserving failure timing, diagnostics, redaction, and public contracts. - Read
references/fran-iglesias-refactoring-guidance.mdfor practical refactoring heuristics distilled from Fran Iglesias. - Read
references/language-examples.mdfor before/after style examples in multiple languages.
Related Skills
- Use
oop-best-practicesfor everyday new code decisions. - Use
design-patterns-best-practiceswhen the main issue is choosing an object collaboration pattern. - Use
ddd-best-practiceswhen moving invariants, splitting a God Aggregate, introducing a root, changing a consistency boundary, or shaping a domain Repository extracted from legacy persistence. - Use
data-migration-best-practicesfor moving or backfilling persisted data; this skill owns only the safe code seams and compatibility paths around that operational migration.
Source Influences
This skill is synthesized from ideas emphasized in:
Working Effectively with Legacy Codeby Michael Feathers99 Bottles of OOPby Sandi MetzPractical Object-Oriented Design in Rubyby Sandi Metz- Fran Iglesias's
Object Calisthenicsseries - CodelyTV Aggregates course (temporal coupling and Aggregate evolution)
- CodelyTV Value Objects course (incremental primitive-to-domain-value refactoring)
- CodelyTV Repository Pattern course (incremental direct-SQL-to-port refactoring)
- CodelyTV Domain Events course (legacy event seams and CDC counterexamples)
- CodelyTV Domain Modeling Errors course (incremental exception-to-Result and boundary-contract lessons)
- CodelyTV Four Rules of Simple Design course (behavior-preserving tests, speculative-element deletion, and duplication counterexamples)
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
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.

