Refactor code while preserving behavior
Improve the readability and maintainability of a code snippet without changing what it does, with an explanation of every change. Designed for ChatGPT, Claude and Microsoft Copilot. It includes 4 customizable variables and you can copy it for free in one click.
Intermediate · Code · Pending editorial review
Prompt
You are an expert in clean code and safe refactoring in {{language}}.
Refactor the following code with these goals, in order of priority: {{goals}}.
```
{{code}}
```
Rules:
- Observable behavior must be identical (same inputs → same outputs and side effects).
- Do not add new dependencies.
- Respect the project style: {{style}}.
- If the intent of any part is unclear, or the code seems to contain a bug, flag it instead of silently changing it.
Response format:
1. Detected problems (short list, with the reason).
2. Complete refactored code.
3. A “change → benefit → risk” table.
4. Minimal unit tests proving the behavior has not changed.How to use it
Refactor bounded snippets (one function or a small module). Run your tests before and after applying the changes.
Usage example
Goals: reduce nesting, clearer names and separate the validation logic.
Sample output
Assuming a typical function that computes order totals with repeated blocks:
1. Problems found
- Opaque names (
d,t,x): you have to read everything to know what they mean. - Duplicated logic: the tax calculation is repeated in two branches.
- Magic number
0.21with no explanation.
2. Refactored code
from decimal import Decimal
VAT_RATE = Decimal("0.21")
def line_total(price: Decimal, quantity: int) -> Decimal:
return price * quantity
def order_total(lines: list[tuple[Decimal, int]], apply_vat: bool = True) -> Decimal:
subtotal = sum((line_total(p, q) for p, q in lines), Decimal("0"))
return subtotal * (1 + VAT_RATE) if apply_vat else subtotal
A single expression computes the subtotal, and tax is applied in exactly one place.
3. Change → benefit → risk
| Change | Benefit | Risk |
|---|---|---|
Extract line_total | Testable on its own | None |
VAT_RATE constant | Rate changes in one place | Low |
| Remove duplicate branch | Less code to maintain | Check rounding |
4. Minimal test
def test_order_total_keeps_behavior():
lines = [(Decimal("10"), 2), (Decimal("5"), 1)]
assert order_total(lines) == Decimal("30.25")
assert order_total(lines, apply_vat=False) == Decimal("25")Key facts
- Category
- Programming & development · Refactoring
- Output type
- Code
- Level
- Intermediate
- Designed for
- ChatGPT, Claude and Microsoft Copilot
- Verified in
- Pending editorial verification
- Variables
- Language, Goals, Code and Style / conventions
- Language
- English
- Price
- Free, no sign-up
- Updated
- Nov 7, 2026
Frequently asked questions
How do I know the refactor did not break anything?
Run your existing tests plus the ones it generates against both versions. If the code has no tests, add them and commit before applying the refactor.
Can I paste a whole file?
Yes, but results are more reliable with one module or a few functions at a time. With very long files the AI tends to skip or summarize parts of the code.
What is this prompt for?
Improve the readability and maintainability of a code snippet without changing what it does, with an explanation of every change.
Which AI tools does it work with?
It is designed for ChatGPT, Claude and Microsoft Copilot. We have not verified it in these tools yet, so results may vary between them.
How do I customize the prompt?
Click “Customize” and fill in the variables (Language, Goals, Code and Style / conventions). The text updates instantly and you can then copy it in one click. Values are stored only in your browser.
Do I need experience to use it?
It is intermediate: it helps to know the topic so you can provide good context and review the result critically.
Is this prompt free?
Yes. Every HomeCasa Prompts prompt is free, with no sign-up or subscription. You can also use them in professional projects.
Can I trust the AI output?
Treat it as a high-quality draft: always check data, figures and important claims before publishing or making decisions.