refactoring
Skill146 starsChanged 7 months agoMITView on GitHub
MUST USE when restructuring existing code without changing behavior: extracting functions/modules, inlining, simplifying, decoupling, reorganizing files, renaming across the codebase, or cleaning up code structure. Enforces behavior-locking tests before any structural change and incremental verification after each move. Distinct from brainstorming (which designs new behavior) and bug fixing (which changes behavior). Triggers on: "refactor", "restructure", "reorganize", "clean up this code", "extract into", "inline this", "simplify", "decouple", "modularize", "move these files", "split this module", "consolidate", "reduce complexity". Routed by using-superpowers, or invoke directly via /refactoring.
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
- 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
- SWEStash/swe-workflow-skillstheir own version, first seen Nov 8, 2026
Used with
Projects with it also use ci-cd, multi-agent-orchestration and 9 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
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
AWSSkillplanning
Discovers user intent and generates a structured, step-by-step plan for model customization workflows. This skill must always be activated alongside any other skill when the user's request relates to model customization — including fine-tuning, training, building, customizing, reviewing data, or getting advice on approach, regardless of domain. Do not skip this skill even if the immediate ask is narrow (e.g., reviewing data format or a single workflow step), because planning discovers the full scope of work needed. Also activate when the user wants to resume, continue, or modify an existing plan.
Also in 6 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
- extra-org/extra★ 119same name
- tmcfarlane/team-avatar★ 112same name
- larchanka/skills★ 50same name
- nimadorostkar/Claude-Skills-collection★ 33same name
- dan-the-dev/xp-developer-skills★ 31same 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 repository23 other files it gives its agents.
- brainstormingSkill
- claude-md-creatorSkill
- context-managementSkill
- deliberationSkill
- dependency-managementSkill
- dispatching-parallel-agentsSkill
- error-recoverySkill
- executing-plansSkill
- finishing-a-development-branchSkill
- frontend-designSkill
- performance-investigationSkill
- premise-checkSkill
- receiving-code-reviewSkill
- requesting-code-reviewSkill
- self-consistency-reasonerSkill
- subagent-driven-developmentSkill
- systematic-debuggingSkill
- test-driven-developmentSkill
- token-efficiencySkill
- using-git-worktreesSkill
- using-superpowersSkill
- verification-before-completionSkill
- writing-plansSkill
The file
We split this skill into the rules it gives an agent
12 rules. 0 are common, 10 are distinctive, and 2 are very specific to this project.
0
Common
Most of the 53 edited versions we read keep these.
10
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 24
Before touching any structure, lock the current behavior with tests.
- Distinctive: line 32
If yes: verify it passes. That test is now your behavior lock.
- Distinctive: line 33
If no: write a characterization test that captures current behavior.
- Very specific: line 52
If any answer reveals a behavior change is required, pause: that portion is not a refactor.
- Very specific: line 67
Run the tests covering the modules in scope: All must pass. Bias wider here than verification-before-completion would for an equivalent …
- Distinctive: line 76
Verify the passing state: before moving to the next structural change.
- Distinctive: line 78
Never batch multiple structural moves into one step.
- Distinctive: line 82
After all structural moves are done:
- Distinctive: line 91
Test files and mocks (test helpers, fixture data, mock implementations)
- Distinctive: line 100
Never weaken a test to make a refactoring pass.
- Distinctive: line 101
If you discover a bug during refactoring, do not fix it in the same commit.
- Distinctive: line 102
If the refactoring scope grows beyond what was defined in Phase 2, stop and re-scope before continuing.
“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
Change structure without changing behavior. Prove it at every step.
Why This Exists
Refactoring feels safe — "I'm just moving things around." That's exactly why it's dangerous. Without a behavior lock, structural changes silently break contracts, reorder side effects, or drop edge cases. This skill enforces the discipline that makes refactoring actually safe.
Phase 1: Behavior Lock
Before touching any structure, lock the current behavior with tests.
- Run the existing test suite. Detect the project's test runner (check
package.jsonscripts,Makefile,pytest.ini,Cargo.toml, etc.) and run it. Every test must pass. If tests fail before you start, you're debugging, not refactoring — switch tosystematic-debugging. - Identify the refactoring surface — which functions, modules, or files will be structurally changed.
Audit test coverage on that surface. For each function/module in scope:
- Is there a test that exercises its public API or observable behavior?
- If yes: verify it passes. That test is now your behavior lock.
- If no: write a characterization test that captures current behavior. This test doesn't assert "correct" behavior — it asserts "current" behavior. The point is to detect unintended changes, not to validate design.
Writing characterization tests: Call the function with representative inputs and assert on the exact output. For side-effectful code (writes files, sends requests, modifies state), wrap the side effect in a spy/mock and assert it was called with the expected arguments. For error cases, trigger the error and assert on the error type/message. If the function is too tightly coupled to test in isolation, that's useful information — note it as a reason the refactoring is needed, and test at the integration level (HTTP request → response, CLI input → output) instead.
- Confirm: all tests green. This is your baseline. Any structural change that breaks a test has changed behavior — stop and investigate.
Phase 2: Scope Definition
Explicitly state the refactoring boundary before writing any code.
- What changes: List the specific structural moves (extract function X, move file Y to Z, inline helper W, rename A to B across the codebase).
- What stays the same: List the public APIs, contracts, and behaviors that must not change. Be concrete — "the HTTP response shape stays identical" is better than "behavior stays the same."
- Boundary check: For each structural move, ask:
- Does this cross a module boundary? (Higher risk — more callers to update)
- Does this change an import path that other packages depend on? (Requires coordinated update)
- Does this affect serialization, persistence, or wire format? (Not a refactor — that's a migration)
If any answer reveals a behavior change is required, pause: that portion is not a refactor. Split it into a separate task that goes through brainstorming → writing-plans.
Phase 3: Incremental Steps
One structural change at a time. Tests green after each.
For each move in the scope definition:
- Make exactly one structural change. Examples:
- Extract a function/class
- Move a file and update all imports
- Rename a symbol across the codebase
- Inline a helper that adds indirection without value
- Split a module into two
- Run the tests covering the modules in scope. All must pass. Bias wider here than
verification-before-completionwould for an equivalent edit — structural moves cross module boundaries more often than they look like they do. The full suite still runs once in Phase 4. - If a test breaks: The structural change altered behavior. Do not fix the test to match the new structure — that's adjusting the lock, not proving safety. Instead:
- Revert the structural change.
- Investigate why it changed behavior.
- Either fix the structural move to preserve behavior, or acknowledge this is a behavior change and route it through TDD.
- Verify the passing state before moving to the next structural change. If the user has asked for commits, commit each step independently so it's revertable. Otherwise, proceed to the next change — the test suite is the safety net.
Never batch multiple structural moves into one step. "Extract and rename and move" is three steps, not one. If one of them breaks behavior, you need to know which.
Phase 4: Completion Gate
After all structural moves are done:
- Full test suite green — no exceptions, no skipped tests, no "known failures."
- Behavioral equivalence confirmed — the characterization tests from Phase 1 still pass with identical assertions.
Import/reference audit — for each renamed/moved symbol, run separate Grep searches for:
- Direct calls and type references (the old function/class/variable name)
- String literals containing old names (config keys, error messages, logs)
- Dynamic imports and
require()calls (string-based references the rename didn't catch) - Re-exports and barrel file entries (
export { OldName },index.tsfiles) - Test files and mocks (test helpers, fixture data, mock implementations)
- Documentation and comments (inline references, JSDoc @see tags)
A single Grep cannot prove absence — search each category separately.
- Code review — if the refactoring touched 5+ files or crossed module boundaries, review for structural issues a test suite won't flag: circular dependencies, layering violations, naming inconsistencies. For large refactors, suggest the user invoke
requesting-code-reviewfor an independent second opinion.
Rules
- A refactoring that changes behavior is not a refactoring — it's a feature change or bug fix. Route accordingly.
- Never weaken a test to make a refactoring pass. Tests are the lock. If the lock breaks, the change is wrong.
- If you discover a bug during refactoring, do not fix it in the same commit. Note it, finish the refactor, then fix the bug separately with TDD.
- If the refactoring scope grows beyond what was defined in Phase 2, stop and re-scope before continuing. Scope creep during refactoring is how "quick cleanup" becomes a multi-day yak shave.
Related Skills
test-driven-development— when the refactoring reveals missing test coveragesystematic-debugging— when pre-refactoring tests are already failingbrainstorming→writing-plans— when the "refactoring" actually requires behavior changesverification-before-completion— final gate after the refactoring is complete
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.

