agentleFS
VelimirMuellerVelimirMueller/ai-development-skills/skills/craft/refactor-safely/SKILL.md

refactor-safely

Skill7 starsChanged 15 days agoMITView on GitHub

Use when a function or module has grown — long method, feature envy, god class, duplicated code — and must change shape without changing behavior.

19 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use audit-test-suite, change-data-safely 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.
  • yaohuangguanSkill

    audit-test-suite

    Find where an existing test suite is weak and decide what to test next, ranked by risk. Use this when asked to add missing tests, find gaps, raise coverage, assess test quality or see which areas are thin, even if the request only mentions a coverage percentage.

    Also in 3 projects that use this

  • yaohuangguanSkill

    change-data-safely

    Change a database schema or stored data without breaking running code or losing data. Use this for any migration, new or changed column, backfill, bulk update, delete, index, or change to a stored format, including in small projects. Data mistakes are often impossible to undo.

    Also in 3 projects that use this

  • SauravSrivastavSkill

    debug-failure

    Diagnose a failing test, command, or runtime behavior using evidence and produce a minimal verified fix.

    Also in 3 projects that use this

  • NVIDIASkill

    debug-issue

    Use when the user asks to debug, investigate, or fix a GitHub issue or supplies a repository bug report.

    Also in 5 projects that use this

  • ficaviolaodorata520Skill

    security-pass

    Review an MCP server for common security gaps: LLM-facing surfaces as injection vector (tools, resources, prompts, descriptions), scope blast radius, destructive ops without consent, upstream auth shape, input sinks (URL / path / roots / shell / sampling / schema strictness / ReDoS), tenant isolation, leakage through errors and telemetry, unbounded resources, and HTTP-mode deployment surface. Use before a release, after a batch of handler changes, or when the user asks for a security review, audit, or hardening pass. Produces grouped findings and a numbered options list.

    Also in 3 projects that use this

  • Codagent-AISkill

    implement-change

    Implements an approved change in one continuous session, with tiered test runs. Use when asked to implement a change from its approved definition, or to invoke codagent:implement-change.

    Also in 5 projects that use this

  • +6 more from yaohuangguan

The file

We split this skill into the rules it gives an agent

3 rules. 0 are common, 3 are distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 16 edited versions we read keep these.

  • 3

    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
  1. Distinctive:

    Prefer mechanical, reversible moves (extract, rename, inline, move) over clever rewrites.

    line 41
  2. Distinctive:

    Leave the Boy-Scout rule as the default: make the touched code cleaner than you found it, but do not expand the blast radius to unrelated …

    line 42
  3. Distinctive:

    No tests and no easy way to add one: make the smallest mechanical move and label it unverified.

    line 52

“Common” is measured: kept by at least 6 in 10 of the 16 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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Refactor Safely

Changes the structure of code without changing its behaviour, one smell at a time, under a green test. The smell vocabulary and Boy-Scout rule are here; readability rules are in write-readable-code.

1. Audit (change nothing)

cat .claude/stack-profile.md 2>/dev/null | head -20
git status --short 2>/dev/null
ls tests 2>/dev/null || find . -name "*_test.*" -o -name "*.test.*" 2>/dev/null | head

Confirm two preconditions before touching anything: the tree is clean (or changes are committed) and a test covers the code you will move. If neither holds, stop and say so — refactoring without a safety net is editing, not refactoring.

2. Name the smell

SmellSymptomRefactor
Long methoda function that scrollsExtract method, then extract until each does one thing
Feature envya method calls another object's data more than its ownMove the method to the object it envies
God classone class with many reasons to changeExtract class by responsibility
Duplicationthe same block in two placesExtract and link; one source of truth
Magic numbera bare literal in logicName the constant
Dead codea path nothing reachesDelete (git keeps it)
Data clumpthe same 2–3 fields always passed togetherGroup into one value/struct
Switch over typea conditional on a discriminatorReplace with a union + per-kind function

3. Refactor in discipline

  • One smell at a time, and run the tests after each move — never a batch that changes many things at once.
  • Separate refactoring from behaviour change: refactor and feature land in different PRs.
  • Prefer mechanical, reversible moves (extract, rename, inline, move) over clever rewrites.
  • Leave the Boy-Scout rule as the default: make the touched code cleaner than you found it, but do not expand the blast radius to unrelated files.

Verify

The tests pass unchanged (you did not edit them except to add one), the public behaviour is identical, and the diff reads as moves and renames. State the smell found and the refactor applied in the note.

When to deviate

  • No tests and no easy way to add one: make the smallest mechanical move and label it unverified.
  • The smell is actually load-bearing performance: measure before changing (see engineering-principles.md — measure before optimizing).

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.