refactoring
Skill4 starsChanged 10 days agoMITView on GitHub
Use when the user asks to refactor, clean up, simplify, extract, rename, remove duplication, improve readability, split files, improve separation of concerns, or make code easier to test while preserving externally observable behavior. This skill guides conservative, test-protected refactoring through Baseline → Transform → Same Tests by defining the behavior boundary, running baseline tests before editing, applying one small safe transformation at a time, re-running the same tests, and stopping if behavior preservation cannot be verified. Do not use for feature development, bug fixing, intentional behavior changes, speculative rewrites, architectural redesigns, dependency upgrades, or broad cleanup or repository-wide audits for comprehension risks; use `cognitive-debt` for the latter.
58 other projects use a version of this.
Adopted by
Most recently taken up by
- ishandutta2007/Awesome-Claude-Skillstheir own version, first seen Nov 8, 2026
- larchanka/skillstheir own version, first seen Nov 8, 2026
- REPOZY/superpowers-optimizedtheir own version, first seen Nov 8, 2026
- nisarth/claude-skills-storetheir own version, first seen Nov 8, 2026
- ericwang200608-beep/LANYANT-Skillstheir own version, first seen Nov 8, 2026
- AnyOrgName/php-skillstheir own version, first seen Nov 8, 2026
Used with
Projects with it also use ci-cd, dependency-management and 10 more.
Other files people use alongside this one.
Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
JohnJBorenSkillci-cd
CI/CD pipeline design, GitHub Actions, testing automation, and deployment strategies
Also in 5 projects that use this
dkprogSkilldependency-management
Apply when wiring dependencies, integrating external services, or designing interfaces between modules. Ensures loose coupling through dependency injection, interface segregation, and proper abstraction boundaries.
Also in 3 projects that use this
javalenciacaiSkillmulti-agent-orchestration
Design and coordinate multi-agent systems where specialized agents work together to solve complex problems. Covers agent communication, task delegation, workflow orchestration, and result aggregation. Use when building coordinated agent teams, complex workflows, or systems requiring specialized expertise across domains.
Also in 3 projects that use this
harrissohail6Skilldatabase-design
Database schema design and SQL best practices. Use when designing tables, writing queries, or optimizing database performance.
Also in 5 projects that use this
MicrosoftSkillerror-handling
统一错误处理系统。在添加 API 端点、修改错误处理、添加前端 API 调用、编写错误相关测试时使用。
Also in 7 projects that use this
klauswgSkillperformance
Optimize web performance for faster loading and better user experience. Use when asked to "speed up my site", "optimize performance", "reduce load time", "fix slow loading", "improve page speed", or "performance audit".
Also in 7 projects that use this
Other versions58 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 58
- handsontable/handsontable★ 22ksame name
- ciembor/agent-rules-books★ 2.8ksame name
- hitobito/hitobito★ 485same name
- databrickslabs/ontobricks★ 457same name
- DevelopersGlobal/ai-agent-skills★ 216same name
- REPOZY/superpowers-optimized★ 146same name
- extra-org/extra★ 119same name
- tmcfarlane/team-avatar★ 112same name
- larchanka/skills★ 50same name
- nimadorostkar/Claude-Skills-collection★ 33same name
48 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository10 other files it gives its agents.
- architectureSkill
- bugfixSkill
- check-draftSkill
- cognitive-debtSkill
- deep-code-reviewSkill
- extract-adrsSkill
- extract-conventionsSkill
- feature-devSkill
- performance-testingSkill
- ultra-grill-meSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
76 rules. 0 are common, 74 are distinctive, and 2 are very specific to this project.
0
Common
Most of the 53 edited versions we read keep these.
74
Distinctive
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 12
Treat refactoring as unsafe until behavior preservation is demonstrated.
- Distinctive: line 16
Run those tests before editing.
- Distinctive: line 21
Stop, revert, or narrow scope if behavior preservation cannot be verified.
- Distinctive: line 25
Use this skill when the request is primarily about changing internal code structure while preserving behavior.
- Distinctive: line 42
Do not use this skill when the request is primarily:
- Distinctive: line 53
If a task mixes refactoring with feature development or bug fixing:
- Distinctive: line 55
Do not mix concerns silently.
- Distinctive: line 56
Use a bugfix workflow first if current behavior is broken.
- Very specific: line 57
Use feature-dev first if new behavior is required.
- Distinctive: line 58
Use this skill only for the behavior-preserving cleanup portion.
- Distinctive: line 59
Keep the refactoring diff separate from behavior-changing diffs whenever possible.
- Distinctive: line 65
Add new product behavior.
- Distinctive: line 75
Delete tests because they fail.
- Distinctive: line 98
Remove dead code only when it is proven unused.
- Distinctive: line 103
Treat a proposed simplification as a hypothesis to verify.
- Distinctive: line 126
If the behavior boundary cannot be identified, narrow the refactor or stop.
- Distinctive: line 130
Before editing, inspect enough repository context to follow local conventions.
- Distinctive: line 132
Check project instructions when present:
- Distinctive: line 140
Check package and build files when present:
- Distinctive: line 152
Check test and CI configuration when present:
- Distinctive: line 172
Prefer existing conventions over inventing new patterns.
- Distinctive: line 191
If no relevant tests exist:
- Distinctive: line 193
Add characterization tests when feasible.
- Distinctive: line 194
Use the smallest possible characterization test that captures current behavior.
- Distinctive: line 195
Avoid inventing new expected behavior.
- Distinctive: line 196
If characterization tests are not feasible, restrict changes to mechanical low-risk transformations.
- Distinctive: line 201
Before editing production code, run the relevant existing tests.
- Distinctive: line 213
If baseline tests pass, proceed with the refactor.
- Distinctive: line 214
If baseline tests fail for reasons unrelated to the refactor, report them and narrow the scope.
- Distinctive: line 215
If baseline tests fail in the target area, do not proceed with broad refactoring.
- Distinctive: line 216
If tests cannot be run, explain why and use the closest available static verification, but do not overstate confidence.
- Distinctive: line 230
Keep the diff small and reviewable.
- Distinctive: line 231
Do not combine unrelated transformations.
- Distinctive: line 232
Do not change tests merely to match new internals unless behavior remains covered.
- Distinctive: line 233
Do not introduce new behavior.
- Distinctive: line 234
Do not fix unrelated issues discovered during refactoring.
- Distinctive: line 235
Do not chase style preferences beyond the stated objective.
- Distinctive: line 236
Check the diff after each transformation for accidental behavior changes.
- Distinctive: line 240
After each meaningful transformation, re-run the same tests used for the baseline.
- Distinctive: line 244
Run the identical command when possible.
- Distinctive: line 246
If tests still pass, continue.
- Distinctive: line 247
If tests fail, identify whether the transformation caused it.
- Distinctive: line 248
If the transformation caused the failure, fix the transformation or revert it.
- Distinctive: line 249
If the failure is unrelated or flaky, record evidence and avoid expanding scope.
- Distinctive: line 250
Do not continue layering additional transformations on top of a failing state.
- Distinctive: line 256
After planned transformations are complete, run the same baseline test first, then broader checks when feasible.
- Distinctive: line 269
If tests are too slow, run the most targeted relevant tests first and clearly state what was not run.
- Distinctive: line 273
Before reporting completion, inspect the final diff and ask:
- Distinctive: line 288
Do not claim behavior preservation unless tests or equivalent checks were actually performed.
- Distinctive: line 319
Delete dead code only when there is strong evidence it is unreachable or unused.
- Distinctive: line 323
Avoid or require explicit extra caution for:
- Distinctive: line 348
No new behavior was introduced.
- Distinctive: line 349
No unrelated bug fix was mixed in.
- Distinctive: line 350
No public contract changed unless explicitly requested.
- Distinctive: line 358
Add characterization tests if coverage is missing and feasible.
- Distinctive: line 359
Run baseline tests and record results.
- Distinctive: line 360
Stop or narrow scope if baseline failures affect the target area.
- Distinctive: line 366
Stop on failures caused by the refactor.
- Distinctive: line 371
Use commands in this order:
- Distinctive: line 382
Treat typecheck or lint alone as insufficient proof of behavior preservation unless no executable behavior test exists and the change is …
- Distinctive: line 386
If no relevant tests exist, add characterization tests before refactoring when feasible.
- Distinctive: line 387
If characterization tests cannot be added, restrict the refactor to mechanical low-risk changes and report weak protection.
- Distinctive: line 388
If baseline tests are red in the target area, stop and use a bugfix workflow first.
- Distinctive: line 389
If baseline tests are red outside the target area, proceed only with narrow scope and report the pre-existing failure.
- Very specific: line 390
If a refactor requires changing public behavior, stop and reclassify the task as feature-dev or bugfix.
- Distinctive: line 391
If the refactor uncovers a bug, do not fix it silently.
- Distinctive: line 392
If a broad architectural redesign is required, stop and ask for explicit approval or create a separate design task.
- Distinctive: line 393
If a mechanical rename touches many files, avoid combining it with logic changes.
- Distinctive: line 395
If a generated file must change, use the project's standard generation command.
- Distinctive: line 396
If behavior preservation cannot be verified, stop expanding the refactor.
- Distinctive: line 402
When a post-transform test fails:
- Distinctive: line 404
Do not continue adding changes while tests are failing.
- Distinctive: line 406
If caused by the refactor, revert or correct the transformation.
- Distinctive: line 407
If unrelated, record evidence and avoid expanding scope.
- Distinctive: line 408
If flaky, rerun once if reasonable, then report uncertainty.
- Distinctive: line 409
If the baseline was not clean, do not claim that the refactor is fully verified.
“Common” is measured: kept by at least 6 in 10 of the 53 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.
Files
refactoring
Purpose
Improve code structure while preserving externally observable behavior.
Treat refactoring as unsafe until behavior preservation is demonstrated. Use Baseline → Transform → Same Tests as the central loop:
- Define the behavior boundary that must remain unchanged.
- Find existing tests that cover the target code or target behavior.
- Run those tests before editing.
- Record the baseline result.
- Apply one small, safe transformation.
- Re-run the same tests after the transformation.
- Continue only if the same tests still pass.
- Stop, revert, or narrow scope if behavior preservation cannot be verified.
When to use this skill
Use this skill when the request is primarily about changing internal code structure while preserving behavior.
Matching requests include:
- "Refactor this module."
- "Clean up this component."
- "Simplify this function."
- "Extract this logic into a helper."
- "Remove duplication."
- "Improve readability."
- "Split this large file."
- "Rename these methods for clarity."
- "Improve separation of concerns."
- "Make this code easier to test without changing behavior."
When not to use this skill
Do not use this skill when the request is primarily:
- Adding new behavior. Use
feature-dev. - Fixing broken behavior. Use a bugfix workflow.
- Investigating an issue only.
- Updating dependencies only.
- Changing architecture intentionally.
- Auditing a codebase or subsystem to find and prioritize comprehension risks. Use
cognitive-debt. - Changing product behavior intentionally.
- Rewriting from scratch.
If a task mixes refactoring with feature development or bug fixing:
- Do not mix concerns silently.
- Use a bugfix workflow first if current behavior is broken.
- Use
feature-devfirst if new behavior is required. - Use this skill only for the behavior-preserving cleanup portion.
- Keep the refactoring diff separate from behavior-changing diffs whenever possible.
Core rule: test-protected refactoring only
Do not:
- Add new product behavior.
- Fix unrelated bugs.
- Change API contracts unless explicitly requested.
- Change UI behavior unless explicitly requested.
- Change database schemas unless explicitly requested.
- Change permission, authorization, billing, privacy, security, or data-loss behavior.
- Rewrite architecture under the label of refactoring.
- Combine refactoring with feature development.
- Combine refactoring with broad dependency upgrades.
- Edit generated, vendor, lock, or build output files unless required by the repo's standard workflow.
- Delete tests because they fail.
- Modify tests only to make the refactor pass unless the test is clearly coupled to internal implementation details and the behavior remains covered.
- Claim behavior preservation without running tests or equivalent verification.
Workflow: Baseline → Transform → Same Tests
1. Refactoring objective
Identify the precise reason for refactoring before editing.
Possible objectives:
- Reduce duplication.
- Improve readability.
- Extract function, class, component, module, or service.
- Improve naming.
- Simplify control flow.
- Isolate side effects.
- Reduce coupling.
- Improve cohesion.
- Separate responsibilities.
- Improve testability.
- Reduce complexity.
- Remove dead code only when it is proven unused.
- Normalize existing patterns.
State the objective in one or two sentences before editing. Vague objectives like "make it better" are not sufficient for broad changes.
Treat a proposed simplification as a hypothesis to verify. File length, AI authorship, or a complexity metric alone is not evidence that code should change. Trace similar-looking behavior before consolidating it; it may protect different domain rules or contracts.
As a decision aid, consider whether behavior can be safely removed, consolidated with an existing implementation, or needs something new—in that order of inquiry, not as an automatic preference for deletion. Add an abstraction only when it reduces a demonstrated source of complexity without obscuring important invariants or boundaries.
2. Behavior boundary
Define what must remain unchanged.
Inspect and preserve:
- Public API shape.
- Function signatures.
- Return values.
- Error types and messages, unless internal-only.
- UI text and visible states.
- Routes, commands, flags, and configuration names.
- Database reads, writes, migrations, and transaction semantics.
- Authentication and authorization behavior.
- Billing, privacy, security, and data-loss behavior.
- Logging and metrics behavior when externally consumed or tested.
- Performance characteristics when relevant.
- Timing, concurrency, caching, and retry behavior.
If the behavior boundary cannot be identified, narrow the refactor or stop.
3. Context discovery
Before editing, inspect enough repository context to follow local conventions.
Check project instructions when present:
AGENTS.mdCLAUDE.md.cursor/rulesREADME.mdCONTRIBUTING.md
Check package and build files when present:
package.jsonpyproject.tomlCargo.tomlgo.modpom.xmlbuild.gradleMakefilejustfileTaskfile.yml
Check test and CI configuration when present:
jest.config.*vitest.config.*pytest.initox.iniplaywright.config.*cypress.config.*rspecphpunit.xml.github/workflows
Inspect:
- Existing test directories and naming conventions.
- Existing tests that cover the target code.
- Similar code paths and similar tests.
- Current public interfaces, API contracts, UI states, data persistence, side effects, logs, metrics, permissions, and error behavior.
- Generated file conventions and formatting or linting commands.
Prefer existing conventions over inventing new patterns.
4. Test discovery
Find existing tests before editing.
Look for:
- Unit tests for the target functions or modules.
- Integration tests covering the affected flow.
- Component tests for UI behavior.
- API or contract tests.
- Snapshot tests.
- End-to-end tests.
- Golden file tests.
- Type-level tests.
- Build, typecheck, or lint checks that protect the change.
- CI workflow commands.
If no relevant tests exist:
- Add characterization tests when feasible.
- Use the smallest possible characterization test that captures current behavior.
- Avoid inventing new expected behavior.
- If characterization tests are not feasible, restrict changes to mechanical low-risk transformations.
- Explicitly report that the refactor is weakly protected.
5. Baseline test run
Before editing production code, run the relevant existing tests.
Record:
- Command run.
- Result.
- Failure summary, if any.
- Whether the failure is related to the target code.
- Whether the baseline is safe enough to continue.
Rules:
- If baseline tests pass, proceed with the refactor.
- If baseline tests fail for reasons unrelated to the refactor, report them and narrow the scope.
- If baseline tests fail in the target area, do not proceed with broad refactoring. Use a bugfix workflow first or ask for direction.
- If tests cannot be run, explain why and use the closest available static verification, but do not overstate confidence.
6. Refactor plan
Split the refactor into small transformations.
Separate mechanical changes from logic-looking transformations whenever possible. A mechanical rename that touches many files should not be combined with logic changes.
7. Transform phase
Apply one meaningful transformation at a time.
Rules:
- Keep the diff small and reviewable.
- Do not combine unrelated transformations.
- Do not change tests merely to match new internals unless behavior remains covered.
- Do not introduce new behavior.
- Do not fix unrelated issues discovered during refactoring.
- Do not chase style preferences beyond the stated objective.
- Check the diff after each transformation for accidental behavior changes.
8. Same Tests phase
After each meaningful transformation, re-run the same tests used for the baseline.
Required behavior:
- Run the identical command when possible.
- Compare the result to baseline.
- If tests still pass, continue.
- If tests fail, identify whether the transformation caused it.
- If the transformation caused the failure, fix the transformation or revert it.
- If the failure is unrelated or flaky, record evidence and avoid expanding scope.
- Do not continue layering additional transformations on top of a failing state.
This phase is strict.
9. Broader verification
After planned transformations are complete, run the same baseline test first, then broader checks when feasible.
Relevant broader checks include:
- Related test suite.
- Typecheck.
- Lint.
- Build.
- Formatting check.
- API contract tests.
- UI, component, or end-to-end tests if UI behavior could be affected.
- Performance checks if performance-sensitive code was touched.
If tests are too slow, run the most targeted relevant tests first and clearly state what was not run.
10. Final review
Before reporting completion, inspect the final diff and ask:
- Did any public behavior change?
- Did any test expectation change?
- Did any public interface change?
- Did any error message or status code change?
- Did any permission or security behavior change?
- Did any persistence behavior change?
- Did any logging or metrics behavior change?
- Did any unrelated cleanup enter the diff?
- Is the diff smaller and clearer than a rewrite?
- Are the verification claims backed by commands actually run?
11. Final report
Do not claim behavior preservation unless tests or equivalent checks were actually performed.
Include:
- Refactoring objective.
- Behavior preservation boundary.
- Files changed.
- Baseline tests run before editing.
- Same tests run after transformation.
- Additional verification commands and outcomes.
- Any characterization tests added.
- Any tests that could not be run.
- Any known risks or follow-ups.
- Explicit note if the refactor was weakly protected due to missing tests.
Safe transformations
Allowed safe transformations include:
- Rename local variables for clarity.
- Extract function.
- Extract component.
- Extract constant.
- Extract helper.
- Inline unnecessary indirection.
- Remove obvious duplication.
- Simplify conditionals without changing truth tables.
- Move code without changing logic.
- Separate pure logic from side effects.
- Replace ad-hoc code with an existing project abstraction.
- Improve module boundaries without changing public contracts.
- Delete dead code only when there is strong evidence it is unreachable or unused.
Risky transformations
Avoid or require explicit extra caution for:
- Changing public APIs.
- Changing database schema or migrations.
- Changing authorization logic.
- Changing error semantics.
- Changing concurrency or async ordering.
- Changing caching behavior.
- Changing retry behavior.
- Changing time, timezone, locale, encoding, or rounding logic.
- Changing serialization or deserialization.
- Changing dependency versions.
- Replacing entire subsystems.
- Rewriting working code from scratch.
- Large file moves combined with logic changes.
Acceptance criteria checklist
The refactor is acceptable only when:
- The refactoring objective is specific.
- The behavior boundary is explicit.
- Baseline verification was run before production edits, or the lack of baseline verification is clearly reported.
- Each meaningful transformation is small and reviewable.
- The same tests were run after transformation when possible.
- No new behavior was introduced.
- No unrelated bug fix was mixed in.
- No public contract changed unless explicitly requested.
- Final verification evidence is recorded.
Test protection checklist
Before editing:
- Identify relevant existing tests.
- Add characterization tests if coverage is missing and feasible.
- Run baseline tests and record results.
- Stop or narrow scope if baseline failures affect the target area.
After each transformation:
- Re-run the same tests.
- Compare with baseline.
- Stop on failures caused by the refactor.
- Keep uncertainty visible.
Verification command strategy
Use commands in this order:
- The smallest targeted test that covers the target behavior.
- The same targeted test after each transformation.
- Related test suite.
- Typecheck.
- Lint.
- Build.
- Formatting check.
- Contract, UI, end-to-end, or performance checks when relevant.
Treat typecheck or lint alone as insufficient proof of behavior preservation unless no executable behavior test exists and the change is purely mechanical.
Decision rules
- If no relevant tests exist, add characterization tests before refactoring when feasible.
- If characterization tests cannot be added, restrict the refactor to mechanical low-risk changes and report weak protection.
- If baseline tests are red in the target area, stop and use a bugfix workflow first.
- If baseline tests are red outside the target area, proceed only with narrow scope and report the pre-existing failure.
- If a refactor requires changing public behavior, stop and reclassify the task as
feature-devor bugfix. - If the refactor uncovers a bug, do not fix it silently. Report it or switch to a bugfix workflow.
- If a broad architectural redesign is required, stop and ask for explicit approval or create a separate design task.
- If a mechanical rename touches many files, avoid combining it with logic changes.
- If tests are too slow, run the most targeted relevant tests first and clearly state what was not run.
- If a generated file must change, use the project's standard generation command.
- If behavior preservation cannot be verified, stop expanding the refactor.
Failure handling
A failing post-transform test is a stop signal.
When a post-transform test fails:
- Do not continue adding changes while tests are failing.
- First determine whether the failure is caused by the refactor.
- If caused by the refactor, revert or correct the transformation.
- If unrelated, record evidence and avoid expanding scope.
- If flaky, rerun once if reasonable, then report uncertainty.
- If the baseline was not clean, do not claim that the refactor is fully verified.
Anti-patterns
Avoid:
- "While I'm here" cleanup.
- Refactoring and feature development in one diff.
- Refactoring and bug fixing in one diff.
- Rewriting a module instead of making safe transformations.
- Updating tests to match changed internals while losing behavior coverage.
- Deleting failing tests.
- Changing public API shape accidentally.
- Changing error messages or status codes accidentally.
- Changing auth, billing, privacy, or data-loss behavior accidentally.
- Moving files and changing logic in the same step.
- Large formatting-only diffs mixed with logic-looking changes.
- Claiming equivalence without running the baseline tests again.
- Treating typecheck or lint alone as behavior preservation.
- Broad cleanup when the requested refactor was local.
Final response template
Summary:
<refactoring objective and result>
Behavior preservation:
- Boundary preserved: <public behavior, API, UI, persistence, errors, permissions, etc.>
- Characterization added: <yes/no, details>
Files changed:
<path>:<reason>
Verification:
- Baseline before editing:
<command>→<result> - Same tests after transform:
<command>→<result> - Additional checks:
<command>→<result>
Notes:
- <pre-existing failures, skipped checks, weak test coverage, risks, or follow-ups>
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
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.

