agentleFS
arjunprabhulalarjunprabhulal/agent-skills/skills/build/refactoring/SKILL.md

refactoring

Skill4 starsChanged 4 months agoMITView on GitHub

Changes the structure of code without changing what it does — extracting, renaming, splitting, collapsing, and untangling. Use this whenever the user asks to clean up, tidy, simplify, restructure, extract, rename, or reduce duplication, and whenever code must be made workable before a feature can land. Also use when a change is turning into a rewrite. For adding behaviour, use feature-implementation; for judging someone else's change, use code-review.

39 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use performance, ci-cd and 7 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.
  • pluginagentmarketplaceSkill

    performance

    Optimize application performance through caching strategies, load balancing, database scaling, and monitoring. Build systems handling thousands of concurrent users.

    Also in 7 projects that use this

  • JohnJBorenSkill

    ci-cd

    CI/CD pipeline design, GitHub Actions, testing automation, and deployment strategies

    Also in 4 projects that use this

  • CloudAI-XSkill

    database-design

    Designs database schemas, indexing strategies, query optimization, and migration patterns for SQL and NoSQL databases. Use when designing tables, optimizing queries, fixing N+1 problems, planning migrations, or when asked about database performance, normalization, ORMs, or data modeling.

    Also in 5 projects that use this

  • AnthropicSkill

    documentation

    Write and maintain technical documentation. Trigger with "write docs for", "document this", "create a README", "write a runbook", "onboarding guide", or when the user needs help with any form of technical writing — API docs, architecture docs, or operational runbooks.

    Also in 8 projects that use this

  • OpenAISkill

    observability

    Vercel Observability expert guidance — Drains (logs, traces, speed insights, web analytics), Web Analytics, Speed Insights, runtime logs, custom events, OpenTelemetry integration, and monitoring dashboards. Use when instrumenting, debugging, or optimizing application performance and user experience on Vercel.

    Also in 5 projects that use this

  • ElasticSkill

    accessibility

    Accessibility guidance for Kibana. Use this skill when working with or reviewing EUI components, resolving a11y-related (@elastic/eui) ESLint issues, and ensuring proper use of ARIA attributes, focus management, keyboard interactions, and accessible naming conventions.

    Also in 5 projects that use this

The file

We split this skill into the rules it gives an agent

5 rules. 0 are common, 4 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 34 edited versions we read keep these.

  • 4

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    If a step cannot be made small, that is information: the code is more tangled than you thought.

    line 63
  2. Distinctive:

    Done when: every commit in the series passes tests on its own.

    line 66
  3. Distinctive:

    Use the tool's automated refactoring where available rather than editing by hand.

    line 73
  4. Very specific:

    When you mix them, the reviewer must find three risky lines inside four hundred trivial ones, and they will not.

    line 77
  5. Distinctive:

    Done when: no commit contains both kinds.

    line 80

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

Refactoring

Refactoring means the observable behaviour is identical afterwards. The moment behaviour changes you are doing something else, and mixing the two is what makes refactors dangerous — a diff that moves 400 lines and alters a condition is unreviewable, and nobody can tell which half broke production.

The one rule: never refactor and change behaviour in the same commit. Everything below exists to make that rule practical.

1. Get a safety net first

You need a way to know you did not break anything. In order of preference:

  • Existing tests covering the area — run them now and confirm they pass, so you know the baseline is green before you touch anything.
  • Characterization tests you write first, pinning current behaviour including behaviour that looks wrong. See unit-testing.
  • A type checker, for mechanical changes like renames.
  • Nothing: then the refactor must be small enough to verify by reading, or you stop and write tests. "I'll be careful" is not a safety net.

Done when: you can run one command that tells you whether behaviour changed.

2. Name the smell, then pick the move

Refactor toward something specific. "Cleaning up" without a target produces churn and a diff nobody can evaluate.

What's wrongThe move
One function does several thingsExtract function
The same logic in three placesExtract and call, but see below
A name lies about what it doesRename
A parameter list keeps growingIntroduce a parameter object
Conditionals nested four deepGuard clauses, early return
A module knows too much about anotherMove the behaviour to the data
A comment explains a blockExtract that block, name it after the comment

On duplication: three occurrences before you extract. Two similar blocks are often coincidence, and a premature abstraction over two cases produces a function with a boolean parameter that both callers have to reason about. That is worse than the duplication.

Done when: you can state what is wrong and which move fixes it.

3. Work in reversible steps

Each step should leave the code working. Run the tests after each one, not at the end.

  • Rename before you move. A rename plus a move in one commit shows up as delete-and-add, and the diff becomes unreadable.
  • Add the new path, migrate callers, then delete the old. For anything with several callers, this keeps every intermediate state green.
  • Commit each step separately. A refactor in twelve small commits is reviewable; the same work squashed is not.

If a step cannot be made small, that is information: the code is more tangled than you thought. Back up and find a smaller first move.

Done when: every commit in the series passes tests on its own.

4. Keep the mechanical separate from the judgement

Two categories, and they must not share a commit:

  • Mechanical: renames, moves, formatting, import reordering. Large diffs, near-zero risk. Use the tool's automated refactoring where available rather than editing by hand.
  • Structural: extracting, inverting dependencies, changing an interface. Small diffs, real risk. These need reading.

When you mix them, the reviewer must find three risky lines inside four hundred trivial ones, and they will not. Record mechanical commits in .git-blame-ignore-revs so history stays useful.

Done when: no commit contains both kinds.

5. Know when to stop

Refactoring has no natural end, which is its main hazard. Stop when:

  • The thing you came to do is now possible. That was the point.
  • You are changing code you have no reason to touch. Note it and leave it.
  • You cannot articulate what is better. "More abstract" is not better.
  • The tests are getting harder to write. That usually means you are refactoring toward the wrong shape — abstraction that makes testing harder is abstraction in the wrong place.

Done when: the original task is unblocked and the diff is defensible line by line.

What not to do

  • Do not refactor code you are not about to change. It is risk without payoff, and it invalidates everyone's in-flight branches.
  • Do not "improve" while implementing a feature. Separate commits, ideally separate PRs.
  • Do not rename public API as a tidy-up. That is a breaking change wearing a refactor's clothes.
  • Do not chase a pattern. Introducing a factory because the code "should" have one adds indirection without removing a problem.

Report

Say what you changed structurally, what proves behaviour is unchanged (which tests, run when), and what you deliberately left alone. If you found a real bug while refactoring — it happens often — report it separately and do not fix it in the same commit.

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.