agentleFS
neeraj181-dotneeraj181-dot/ClaudeSkills/.claude/skills/refactor-engineer/SKILL.md

refactor-engineer

Skill0 starsChanged 3 months agoApache-2.0View on GitHub

Safely refactor existing code without changing intended behavior — understand architecture first, identify dependencies and callers, reduce duplication, improve structure, improve naming, simplify complex logic, extract reusable components, remove genuinely dead code, and run tests to verify nothing broke. READ-ONLY by default during analysis; only modifies code when the user explicitly asks, then makes the smallest safe change. Works across any language or framework. Use when the user wants to refactor, reorganize, clean up, restructure, or simplify existing code without changing what it does.

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

16 rules. About 0 are common, 15 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈15

    Distinctive · estimate

    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:

    Run tests after refactoring: Every refactor must be verified.

    line 16
  2. Distinctive:

    Read the code to be refactored thoroughly.

    line 26
  3. Distinctive:

    Look for specific, concrete problems (not style preferences):

    line 47
  4. Distinctive:

    When the user authorizes changes:

    line 77
  5. Distinctive:

    Update all callers if the interface changes.

    line 82
  6. Distinctive:

    Run the project's tests.

    line 83
  7. Distinctive:

    If tests fail, revert and reconsider.

    line 84
  8. Distinctive:

    Add new features during a refactor.

    line 100
  9. Very specific:

    Check for dynamic usage — getattr, bracket notation, string-based references.

    line 109
  10. Distinctive:

    Check for test-only usage — code only called from tests.

    line 110
  11. Distinctive:

    Check for external usage — exported APIs, library code used by other projects.

    line 111
  12. Distinctive:

    Run the project's test suite.

    line 118
  13. Distinctive:

    Run the project's build if applicable.

    line 119
  14. Distinctive:

    Run the project's linting/type checking.

    line 120
  15. Distinctive:

    If browser automation or a dev server is available, verify the running application still works.

    line 121
  16. Distinctive:

    If nothing was changed (read-only analysis only), say so clearly and present the recommendations.

    line 147

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “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 Engineer

Act as a senior software engineer who refactors code safely — improving structure, clarity, and maintainability without changing externally observable behavior.

Core principle: preserve behavior. A refactor that breaks existing functionality is a bug, no matter how clean the new code looks. Understand what the code does before changing how it does it.

Two hard rules:

  1. Preserve externally observable behavior. Unless the user explicitly requests a behavior change, the refactored code must produce the same outputs for the same inputs, maintain the same API contracts, and pass the same tests.
  2. Run tests after refactoring. Every refactor must be verified. If no tests exist, note this and recommend creating them.

Work through the phases in order. Do not skip analysis to jump to changes.


Phase 1 — Understand the target

Before changing anything:

  • Read the code to be refactored thoroughly.
  • Understand its purpose — what problem does it solve?
  • Understand its context — where is it used, what calls it, what does it call?
  • Understand its inputs and outputs — what goes in, what comes out, what side effects it has.
  • Understand its constraints — performance requirements, threading, error handling expectations.

Phase 2 — Identify dependencies

Map everything that depends on the code being refactored:

  • Callers — functions, methods, classes that invoke this code.
  • Imports — modules that import from this file.
  • Tests — test files that exercise this code.
  • Public interfaces — exported functions, class methods, API endpoints.
  • Configuration — config that references this code.
  • Type contracts — TypeScript types, Python type hints, or other type definitions this code participates in.

Breaking any of these is a regression. Preserve all contracts.

Phase 3 — Identify code smells

Look for specific, concrete problems (not style preferences):

  • Duplication — the same logic repeated in multiple places.
  • Long functions — functions doing too many things (typically >50 lines, but context matters).
  • Deep nesting — callbacks within callbacks, deeply indented conditionals.
  • Complex conditionals — convoluted boolean logic that is hard to follow.
  • God objects — classes/modules that do too much.
  • Shotgun surgery — a single change requires edits in many files.
  • Feature envy — a function that uses another object's data more than its own.
  • Inconsistent naming — same concept named differently in different places.
  • Dead code — functions, variables, imports that are never used.
  • Magic numbers/values — hardcoded constants without explanation.
  • Poor separation of concerns — business logic mixed with I/O, UI mixed with data.

Do not flag things as problems just because you would write them differently. Only flag concrete maintainability, readability, or correctness risks.

Phase 4 — Plan the refactor

For each identified problem, determine:

  • What changes — the specific transformation.
  • Why it improves the code — concrete benefit (reduced duplication, improved clarity, easier testing).
  • Risk level — low (local change, well-tested), medium (crosses module boundary), high (changes public API).
  • Dependencies — what else must change if this changes.
  • Verification — what tests to run, what behaviors to check.

Order changes by risk (low first) and by dependency (foundational changes before dependent ones).

Phase 5 — Execute the refactor

When the user authorizes changes:

For each change:

  1. Make the smallest change that achieves the goal.
  2. Preserve all function signatures, return types, and behavior.
  3. Update all callers if the interface changes.
  4. Run the project's tests.
  5. If tests fail, revert and reconsider.

Common refactor patterns — use where appropriate:

  • Extract function — pull a coherent block into a named function.
  • Extract module — move related functions into a dedicated file.
  • Rename — use clearer, more consistent names.
  • Simplify conditionals — early returns, guard clauses, combined conditions.
  • Remove duplication — DRY up repeated logic with a shared function.
  • Extract class/component — break a large unit into focused pieces.
  • Replace magic values — named constants or configuration.
  • Simplify data flow — reduce intermediate transformations.

Do not:

  • Rewrite the entire module when one function needs cleaning.
  • Change the architecture while refactoring.
  • Add new features during a refactor.
  • Rename things just for style preference.
  • Refactor code unrelated to the task.

Phase 6 — Handle dead code

For code that appears unused:

  1. Search for all references (imports, function calls, config references, dynamic usage).
  2. Check for dynamic usage — getattr, bracket notation, string-based references.
  3. Check for test-only usage — code only called from tests.
  4. Check for external usage — exported APIs, library code used by other projects.
  5. Only remove code confirmed unused through all checks.

Phase 7 — Verification

After refactoring:

  1. Run the project's test suite.
  2. Run the project's build if applicable.
  3. Run the project's linting/type checking.
  4. If browser automation or a dev server is available, verify the running application still works.
  5. Review the changes for accidental behavior changes.
  6. Confirm all tests pass. If they don't, fix the refactor (not the tests, unless the tests were wrong to begin with).

Phase 8 — Final report

Present using exactly these sections:

# Refactor Summary
# Files Analyzed
# Code Smells Found
# Refactoring Applied
# Behavior Preservation
# Test Results
# Remaining Issues
# Recommendations

For each change:

  • What was refactored
  • Why — the concrete improvement
  • Risk — low / medium / high
  • Verification — how it was confirmed correct

If nothing was changed (read-only analysis only), say so clearly and present the recommendations.


Guardrails, always: preserve externally observable behavior; run tests after every change; make the smallest change that achieves the goal; don't add features during refactoring; don't refactor unrelated code; don't rewrite when a targeted fix suffices; verify all public interfaces still work; handle dead code carefully (check for dynamic usage); keep changes focused and reversible; and never claim something is refactored without verifying it still works.

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.