Refactor for Readability

Improve the structure and naming of code without changing what it does, with a list of changes.

refactoringreadabilityclean code All models

Prompt

Prompt
Refactor the {language} code below for readability. Do not change its behavior.

<code>
{code}
</code>

<rules>
- Conventions: {conventions}
- Must not change: {must_not_change}
- Do not add features, new dependencies or new abstractions that are not needed.
- Prefer small, clear steps over a full rewrite.
</rules>

Give me:
1. A short list of what makes this code hard to read, ordered by impact.
2. The refactored code in one block.
3. A list of the changes, each with the reason (for example: "split the validation into its own function so the main flow reads top to bottom").
4. Behavior check: explain why the refactored code behaves the same as the original, and name any case where you are not sure.
5. Tests I should run, or write, to confirm nothing changed.

If you notice a bug in the original, do not silently fix it. Mention it separately under "Possible bugs", and keep the original behavior in the refactored version unless I tell you to fix it.

Variables

VariableWhat to put inExample
{language}The programming language.JavaScript
{code}The code to refactor.Paste the function or module here.
{conventions}Style rules and conventions.Use const, arrow functions and descriptive names; max 30 lines per function.
{must_not_change}Behavior or interfaces that must stay the same.The function signature and the returned object shape.

How to use

  1. Make sure you have tests or a way to run the code before and after. A refactor without a safety net is risky.
  2. Read the behavior check carefully. It is the part that tells you where to look for differences.
  3. Apply the changes in small commits so you can undo one step at a time.

Example result

Illustrative example

Hard to read: one 70-line function mixes validation, calculation and formatting; names like d and tmp. Change: extracted validateOrder, calculateTotal and formatReceipt. Behavior check: the same operations run in the same order. Not sure: the original mutates the input object; the new version keeps that behavior. Possible bug: the discount is applied twice for orders above 100.

Model notes

Claude

Claude tends to keep behavior intact when told what must not change. Be explicit about interfaces used elsewhere.

ChatGPT

Works as-is. For larger files, ask for one function at a time to keep changes reviewable.

Gemini

If you can attach the file and its tests, name both so the refactor can respect the existing tests.

Grok

Keep the instruction not to add features. A conversational assistant may offer extras you did not ask for.

Refine the result

  • Prompt
    Show the refactor as a diff instead of a full block.
  • Prompt
    Keep the original structure but only improve the names.
  • Prompt
    Which part of this refactor is the riskiest?

Related templates

  • Code Review

    Get a prioritized code review that covers correctness, security, readability and tests.

    code reviewqualitysecurity All models
  • Write Unit Tests

    Generate unit tests that cover normal cases, edge cases and failures, with the reasoning for each.

    testingunit testsquality All models

Related guide