← All skills
Prompt · Slash command✓ Verified

Refactor

Suggest and apply targeted refactoring improvements.

What happens when you install it

1

You run the install command

mcp install-skill refactor

MCPHub CLI downloads this prompt from the registry.

2

Saved as a file in Claude Code

~/.claude/commands/refactor.md

Claude Code reads all .md files in this folder as slash commands.

3

Use it in any conversation

/refactorin Claude Code (after restart)

Claude runs the prompt against your current file or selection — no copy-paste needed.

Content

Refactor

Refactor the selected code to improve clarity, maintainability, and simplicity — without changing behavior.

Constraints

  • Preserve existing behavior exactly
  • Keep the same public API and function signatures unless they're clearly broken
  • Don't add features or handle new cases
  • Don't add abstractions for hypothetical future use — only abstract what's needed now

What to look for

  • Naming — variables, functions, and classes that don't communicate intent
  • Complexity — deeply nested conditionals, long functions, cognitive overload
  • Duplication — repeated logic that belongs in a shared function
  • Dead code — unused variables, unreachable branches, commented-out code
  • Premature optimization — complexity added for performance that isn't measured

What not to do

  • Don't refactor just to match your preferred style
  • Don't over-abstract — three similar lines of code is often better than a premature helper
  • Don't change unrelated code just because it's nearby

Output format

Show the refactored code. After each meaningful change, a brief comment explaining:

  • What changed
  • Why it's better