refactoring-safely
Skill1 starsChanged 3 months agoApache-2.0View on GitHub
Change the shape of code without changing what it does. Load when you extract, rename, move, deduplicate, or restructure a module safely across its call sites, and whenever a diff mixes restructuring with new behaviour. Requires a characterisation net before the first edit and keeps structural and behavioural changes in separate commits.
4 other projects use a version of this.
Adopted by
Most recently taken up by
- AnastasiyaW/codex-claude-code-configtheir own version, first seen Oct 30, 2026
- AnastasiyaW/claude-code-configtheir own version, first seen Oct 8, 2026
- xuanjgcarryyou/claude-skilltheir own version, first seen May 29, 2026
- withzombies/hyperpowerstheir own version, first seen Mar 30, 2026
Other versions4 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 4
- AnastasiyaW/claude-code-config★ 214same name
- AnastasiyaW/codex-claude-code-config★ 214same name
- withzombies/hyperpowers★ 80same name
- xuanjgcarryyou/claude-skill★ 0same name
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 repository19 other files it gives its agents.
- AGENTS.mdAGENTS.md
- agent-loop-designSkill
- ai-supply-chain-securitySkill
- anti-slop-pr-and-writingSkill
- authoring-portable-skillsSkill
- brainstormingSkill
- cross-language-portSkill
- giving-code-reviewSkill
- handoffSkill
- incident-responseSkill
- pr-review-and-releaseSkill
- receiving-code-reviewSkill
- systematic-debuggingSkill
- test-driven-developmentSkill
- using-agtmlsSkill
- verification-before-completionSkill
- vetting-a-skill-before-installSkill
- web-research-and-source-triageSkill
- writing-plansSkill
The file
We split this skill into the rules it gives an agent
6 rules. 0 are common, 6 are distinctive, and 0 are very specific to this project.
0
Common
Most of the 4 edited versions we read keep these.
6
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
- Distinctive: line 50
No concrete reason means leave it alone.
- Distinctive: line 54
You cannot refactor code you cannot test: If coverage is thin, write characterisation tests first — tests that assert what the code does …
- Distinctive: line 79
Run the tests after each: Not at the end. The whole point is a short distance between "green" and "red" so the cause is obvious.
- Distinctive: line 97
Renaming a public symbol?: Add the new name as an alias, migrate call sites, then remove the old one — three commits, all green.
- Distinctive: line 99
Changing a signature?: Add the parameter with a default, migrate, then make it required.
- Distinctive: line 114
Changing tests to match: If the tests fail, behaviour changed.
“Common” is measured: kept by at least 6 in 10 of the 4 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 safely
Trigger. You are about to restructure working code: extract a function, rename across call sites, move a module, collapse duplication, replace a conditional with polymorphism, split a class. Also load it when you notice a diff doing restructuring and a fix at the same time.
Reference material (the net-building recipe, sequences for the common refactorings, large-scale and cross-repo strategy): see
reference.md.
The rule
A refactor that changes behaviour is not a refactor. It is an undocumented change wearing a refactor's diff.
The value of the word is that it tells a reviewer they do not need to reason about behaviour. The moment that stops being true, you have spent their trust and hidden a real change inside a large mechanical diff — the single easiest place for a defect to survive review.
Before the first edit
1. Say why
"It's messy" is not a reason; it is taste, and taste alone does not justify churn or merge conflicts. A reason is concrete:
- a change you need to make is hard in the current shape;
- the same logic has diverged in three places and drifted;
- the structure allows an illegal state that keeps causing bugs;
- new people consistently misread it the same way.
No concrete reason means leave it alone.
2. Build the net
You cannot refactor code you cannot test. If coverage is thin, write characterisation tests first — tests that assert what the code does today, including behaviour you think is wrong.
They pass immediately, which is expected: they are a net, not a specification. So verify the net actually catches things:
# Break the implementation on purpose — invert a condition, return a
# constant — and confirm the suite goes red. A net that survives sabotage
# is not protecting anything.This step is the one people skip, and skipping it is how "pure refactors" ship defects.
3. Check nobody is building on it
A large restructure of a file someone else has open is a merge conflict you chose. Check for in-flight work; land or coordinate first.
The loop
- One transformation at a time. Extract or rename or move — not all three. Compound refactors cannot be bisected when they go wrong.
- Run the tests after each. Not at the end. The whole point is a short distance between "green" and "red" so the cause is obvious.
- Prefer the tool. Your IDE's rename and extract are mechanical and will not typo one of forty call sites. Hand-editing across many files is where errors enter.
- Commit each step. Small, green, revertible commits. A twelve-file mechanical commit is reviewable; a twelve-file mixed one is not.
- Never mix in behaviour. Spot a bug mid-refactor? Note it, finish the refactor, then fix it in its own commit with its own failing test.
That last rule is the one that pays. A reviewer can skim a mechanical diff in a minute and read a three-line behavioural one carefully — but not a four-hundred-line diff where the behavioural change is on line 213.
Keeping the tree green
Sequence so every intermediate state builds and passes:
- Renaming a public symbol? Add the new name as an alias, migrate call sites, then remove the old one — three commits, all green.
- Changing a signature? Add the parameter with a default, migrate, then make it required.
- Moving a module? Move it, leave a re-export, migrate importers, delete the re-export.
The expand–migrate–contract shape works for almost every restructure and means you can stop at any point without leaving the tree broken.
Anti-patterns
- The rewrite in disguise. "Refactor" that deletes and reimplements. That is a rewrite; call it one and test it like one.
- Refactoring to no purpose. Restructuring because you are in the file.
- The big-bang branch. Two weeks of restructuring merged at once. It will conflict, and nobody can review it.
- Changing tests to match. If the tests fail, behaviour changed. Fix the code, or admit it is a behavioural change.
- Refactoring under deadline pressure, alongside the fix you actually need. Ship the fix; restructure after.
When not to use
- You are adding or changing behaviour — that is
test-driven-development. - The code is going to be deleted shortly.
- You do not have and cannot build a test net, and the code is business-critical. Say so; propose adding characterisation tests as work in its own right.
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.

