Back to Prompt Library
Coding
refactoringclean codecode qualitymaintenancebest practices

Code Refactoring Assistant

Cleaner, more maintainable code with a documented change log explaining every refactoring decision.

Best For

  • Developers inheriting a messy legacy file who need it cleaned up without risking a behavior change.
  • Teams doing a pre-release cleanup pass who want a documented list of every change for the PR description.
  • Developers learning clean code practices who want to see the reasoning behind each refactoring decision, not just the result.

Prompt Template

Refactor the following [LANGUAGE] code to improve readability, maintainability, and performance without changing its external behavior. Code to refactor: [CODE_BLOCK]. Apply these specific improvements: (1) eliminate duplicate logic using appropriate abstractions, (2) replace magic numbers and strings with named constants, (3) break functions longer than [MAX_FUNCTION_LINES] lines into smaller, single-responsibility functions, (4) rename variables and functions to clearly express intent, (5) add inline comments only where the "why" is non-obvious. Output the refactored code first, then provide a bulleted summary of every change made and the reason for each change. If you identify any bugs or edge cases in the original code, flag them separately under a "Potential Issues" heading but do not fix them unless instructed.

Pro Tip

Set a concrete MAX_FUNCTION_LINES value like 30 rather than leaving it open-ended, since without a limit the model tends to under-split long functions.

Example Output

A sample of what this prompt produces once you fill in the placeholders.

For a 90-line order-processing function, the output splits it into validateOrder, calculateTotals, and applyDiscounts helper functions, replaces a hardcoded 0.08 tax rate with a TAX_RATE constant, and lists each change in a bulleted summary. A 'Potential Issues' section separately flags an unhandled negative-quantity edge case without fixing it.

How to use this prompt

  1. Copy the prompt template using the button above.
  2. Paste it into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.).
  3. Replace all bracketed placeholders like [TOPIC] with your specific details.
  4. Send the prompt and refine the output as needed.