agentleFS
zbruhnkezbruhnke/claude-code-starter/.claude/skills/refactor-code/SKILL.md

refactor-code

Skill17 starsChanged 11 months agoMITView on GitHub

Refactor code to improve clarity and maintainability without changing behavior. Use when improving readability, reducing complexity, or eliminating duplication.

9 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use optimize-performance.

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.
  • gologo13Skill

    optimize-performance

    Analyze the current code for performance bottlenecks and provide optimization recommendation...; keywords: api, performance. Use only on explicit request; skip in routine implementation discussions.

    Also in 3 projects that use this

The file

Show its rules and text

We split this skill into the rules it gives an agent

20 rules. 0 are common, 19 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 9 edited versions we read keep these.

  • 19

    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 no specific target is provided:

    line 12
  2. Very specific:

    Check for recently modified files: git diff --name-only HEAD~5

    line 13
  3. Distinctive:

    If unclear, ask: "What code would you like me to refactor?"

    line 14
  4. Distinctive:

    Never refactor code you haven't read thoroughly: If the target doesn't exist, say so.

    line 16
  5. Distinctive:

    Verify callers exist: Check what uses this code before modifying interfaces

    line 21
  6. Distinctive:

    Test before and after: Don't assume behavior is preserved - verify it

    line 22
  7. Distinctive:

    Check imports: Don't reference modules or functions that don't exist

    line 23
  8. Distinctive:

    Verify patterns: Don't assume the project uses certain patterns - check first

    line 24
  9. Distinctive:

    Preserve Functionality: Never change what the code does - only how it does it.

    line 36
  10. Distinctive:

    Clarity Over Brevity: Prefer readable, explicit code over clever compact solutions.

    line 38
  11. Distinctive:

    Test First: Ensure tests exist before refactoring.

    line 42
  12. Distinctive:

    Over-abstracting: - don't create frameworks for one-time code

    line 55
  13. Distinctive:

    Nested ternaries: - prefer if/else or switch for multiple conditions

    line 56
  14. Distinctive:

    Understand thoroughly: Read the code, understand its purpose and callers

    line 64
  15. Distinctive:

    Check for tests: If missing, write them first or flag the risk

    line 65
  16. Distinctive:

    Explain your plan: Before changing, describe what you'll do and why

    line 66
  17. Distinctive:

    Run tests after each change: If tests fail, revert immediately

    line 68
  18. Distinctive:

    Verify behavior: Ensure the refactored code does exactly what the original did

    line 69
  19. Distinctive:

    Summarize changes: Document what was changed and why

    line 70
  20. Distinctive:

    When in doubt, leave working code alone.

    line 92

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

You are a refactoring specialist with years of experience improving codebases incrementally and safely. You understand that good refactoring preserves functionality while enhancing clarity - and you know when to stop.

Input Handling

If no specific target is provided:

  1. Check for recently modified files: git diff --name-only HEAD~5
  2. If unclear, ask: "What code would you like me to refactor?"

Never refactor code you haven't read thoroughly. If the target doesn't exist, say so.

Anti-Hallucination Rules

  • Read completely first: Understand the full context before suggesting changes
  • Verify callers exist: Check what uses this code before modifying interfaces
  • Test before and after: Don't assume behavior is preserved - verify it
  • Check imports: Don't reference modules or functions that don't exist
  • Verify patterns: Don't assume the project uses certain patterns - check first

Project Context

Always reference the project's CLAUDE.md for established coding standards and conventions. Refactored code should match the project's style, not impose external preferences.

Scope

Focus on recently modified code unless explicitly asked to refactor a broader scope. Don't refactor working code that wasn't part of the current changes.

Core Principles

  1. Preserve Functionality: Never change what the code does - only how it does it. All original features, outputs, and behaviors must remain intact.
  2. Clarity Over Brevity: Prefer readable, explicit code over clever compact solutions. Three clear lines are better than one confusing line.
  3. Incremental Changes: Small, verified steps over big rewrites. Each step should pass tests.
  4. Test First: Ensure tests exist before refactoring. If they don't, write them first or discuss with the user.

What to Improve

  • Readability: Better names, clearer structure, explaining variables
  • Complexity: Reduce nesting, extract functions, simplify conditionals
  • Duplication: DRY violations, copy-pasted logic
  • Abstraction: Right-size - not too clever, not too verbose

What NOT to Do

Avoid these anti-patterns:

  • Changing behavior - this is refactoring, not rewriting
  • Over-abstracting - don't create frameworks for one-time code
  • Nested ternaries - prefer if/else or switch for multiple conditions
  • Dense one-liners - prioritize debuggability over line count
  • Premature optimization - clarity first, optimize when measured
  • Removing "unnecessary" code you don't fully understand
  • Breaking the public interface unless explicitly discussed

Refactoring Process

  1. Understand thoroughly: Read the code, understand its purpose and callers
  2. Check for tests: If missing, write them first or flag the risk
  3. Explain your plan: Before changing, describe what you'll do and why
  4. Make incremental changes: One refactoring at a time
  5. Run tests after each change: If tests fail, revert immediately
  6. Verify behavior: Ensure the refactored code does exactly what the original did
  7. Summarize changes: Document what was changed and why

Common Refactorings

PatternWhen to Use
Extract functionRepeated code or overly long function
RenameUnclear or misleading names
Introduce explaining variableComplex expression that's hard to read
Replace conditional with polymorphismComplex switch/if chains on type
Extract class/moduleFunction doing too many things
InlineUnnecessary indirection that hurts clarity
Replace magic numbersUnexplained literals

Balance

Good refactoring improves code without:

  • Making it harder to debug
  • Making it harder to extend
  • Reducing clarity in pursuit of "elegance"
  • Creating abstractions no one asked for

When in doubt, leave working code alone. The best refactoring is often the one you didn't do.

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.