Coding prompts · Prompt

Fix code smells safely (prompt)

fix-code-smells-prompt

A prompt that restructures messy existing code without changing what it does - pin behaviour with tests, name the code smell (long function, duplication, god module, nested conditionals, magic values) and apply one small refactor at a time. Use when the user wants to refactor hard-to-change code, break up a giant function or file, remove duplication, or add types.

Works with: Any AI Not yet model-tested; testing Jan 2027

Paste it into ChatGPT, Claude, Gemini or any AI chat, then tell it about your project. How it works

Install in Claude Code, Cursor or Codex (optional)

Only for AI coding tools that read skill folders. Run one line in a terminal, in your project folder. It downloads this skill's text files from TheSkillVault and nothing else; you can read them first with “View raw SKILL.md”.

Claude Code (all your projects)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/fix-code-smells-prompt/raw' -o ~/'.claude/skills/fix-code-smells-prompt/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/fix-code-smells-prompt/files/LICENSE.txt' -o ~/'.claude/skills/fix-code-smells-prompt/LICENSE.txt'

Cursor (this project)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/fix-code-smells-prompt/raw' -o '.cursor/skills/fix-code-smells-prompt/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/fix-code-smells-prompt/files/LICENSE.txt' -o '.cursor/skills/fix-code-smells-prompt/LICENSE.txt'

Codex and other Agent Skills tools (this project)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/fix-code-smells-prompt/raw' -o '.agents/skills/fix-code-smells-prompt/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/fix-code-smells-prompt/files/LICENSE.txt' -o '.agents/skills/fix-code-smells-prompt/LICENSE.txt'

Or connect the whole library over MCP

Add TheSkillVault to Cursor or add this to your app's MCP settings (read-only; help):

{
  "mcpServers": {
    "theskillvault": {
      "url": "https://theskillvault.ai/mcp"
    }
  }
}

Refactoring changes the shape of code, never its behaviour. The safe way is boring: tests first, one small move, tests again, commit.

When to use

  • A function or file is so long that every change feels risky.
  • The same logic is copied in several places.
  • Adding a feature keeps breaking unrelated parts.
  • You want to add types to loose JavaScript or Python.

When not to use

  • You only want to tidy what you just changed. Use turn-messy-diff-into-clean-code-prompt.
  • You want to redesign module boundaries across the codebase. Use review-codebase-architecture-prompt first.
  • The code is about to be deleted or never changes. Leave it.

Steps

  1. Commit or stash your work so you can roll back.
  2. Point your AI tool at the file or folder and paste the prompt below.
  3. Approve the smell list and plan before it edits anything.
  4. Let it make one refactor at a time, run the tests after each, and commit when green.
  5. Stop when the code is easy to change for the feature you need next.

Prompt

Refactor [file or folder] without changing its behaviour.

1. First, describe what the code does today and list its inputs, outputs and side effects.
2. Check for tests that cover this behaviour. If they are missing, write characterisation tests that pin the current behaviour (including odd cases) and run them before any change.
3. List the code smells you see, each with file and line, using these names:
   long function, duplicated logic, god module, long parameter list, feature envy, primitive obsession, magic values, deep nesting, dead code, missing types.
4. For each smell, propose the smallest fix:
   long function -> extract named functions; duplication -> one shared function; god module -> split by responsibility;
   long parameter list -> one options object; feature envy -> move the logic to the data it uses;
   primitive obsession -> a small type or value object; magic values -> named constants;
   deep nesting -> guard clauses and early returns; dead code -> delete it; missing types -> add types at the boundaries first.
5. Show me the plan, ordered from safest to riskiest, and wait for my go-ahead.
6. Then apply one item at a time. After each: run the tests, show the diff, and stop if anything fails.

Rules: no new features, no behaviour changes, no renaming of public APIs unless I agree, and no mixing refactors with bug fixes. If you find a bug, list it separately.

How to adapt

  • No test runner set up yet? Ask it to add a minimal one first, such as Vitest, Jest or pytest.
  • Large codebase? Run it on one module per session.
  • Want a pattern change, such as replacing a big switch with a strategy map? Name it in step 4.

Gotchas

  • "Refactors" without tests are just edits. Insist on step 2.
  • Big-bang rewrites hide behaviour changes in huge diffs. Keep each commit small.
  • Do not let the AI reformat untouched files. It buries the real change in noise.
  • Never paste secrets or production config into the chat. Point it at the code only.

Portability

Works with any language, framework or AI coding tool. If you use a language without types, skip the "missing types" item or use type hints or JSDoc instead.

Done when

  • Tests passed before and after every step, and public behaviour is unchanged.
  • Each smell you approved is fixed in its own small commit.
  • Bugs found along the way are listed separately, not silently fixed.

Example prompt

"Refactor src/checkout/processOrder.ts without changing behaviour. Add tests first, then break the 300-line function into named steps."

---

Adapted from "refactor" in github/awesome-copilot (MIT, Copyright GitHub, Inc.). See LICENSE.txt.

Bundled files

Licence

Adapted from AungMyoKyaw (github/awesome-copilot, refactor); spelling fix by jeremykohn (Copyright GitHub, Inc.), MIT License. Adapted by TheSkillVault on 2026-11-02: the 645-line smell catalogue and pattern examples were condensed into one original prompt with a smell-to-fix table and a test-first loop; added when-not-to-use, steps, gotchas and done-when. Not the original file.

Licence: MIT License. Attribution when you reuse it: Based on work by AungMyoKyaw (github/awesome-copilot, refactor); spelling fix by jeremykohn (Copyright GitHub, Inc., MIT License), https://github.com/github/awesome-copilot/tree/5269f7ea2d6c3a9bfda5ed878b6726cf509121fd/skills/refactor; adapted by TheSkillVault, https://theskillvault.ai