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.
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.