Refactor code for readability, same behaviour

Cleans up messy code in small, safe steps and explains each change, while keeping inputs, outputs and side effects identical.

Prompt, 2 blanks to fill
Refactor this {{LANGUAGE}} code to make it easier to read and change.

```
{{CODE}}
```

Hard rule: behaviour must stay identical. Same inputs give same outputs, same errors and same side effects.

Do this:
1. List the readability problems you see, most important first.
2. Propose a sequence of small refactoring steps.
3. Show the final refactored code.
4. List every place where behaviour could have changed by accident, so I can test it.

Prefer clear names, short functions and early returns. Do not add new dependencies or change public function signatures.

Example output

Step 1: extract `calculateDiscount()` from the 60-line handler. Step 2: replace nested ifs with early returns. Behaviour check: same return values for all 4 code paths.

How to use

Commit before you paste the result so you can diff it. If the change is large, ask for one step at a time and run your tests after each.

Why it works

The behaviour rule is stated first and in plain terms because that is the one thing a refactor must not break. The list of risky spots tells you exactly where to point your tests.

Find a prompt for the task in front of you.

96 prompts in 8 categories. Copy one, fill in the blanks, paste it into any model.

Browse prompts

Promptlexity is a free library of 96 AI prompts and 8 downloadable AI skills across 8 categories, plus a glossary of 64 AI and prompting terms. Each prompt shows the exact text to paste, the variables to fill in, an example of what comes back, and why it works. Each glossary term has a plain-English definition, an example and a common mistake. 6 step-by-step guides cover the basics. No signup, works with ChatGPT, Claude, Gemini and other models.

Press Esc to close