Simplify and clean up a complicated function

Make tangled code easier to read and change without altering behavior, with every edit explained.

LLazyPrompt(@lazyprompt)CodingRewriteWorks with any model
Prompt template
Simplify the {{language}} code below so it is easier to read, test and change. Behavior must stay exactly the same.

What the code is for: {{purpose}}
Priorities: {{priority}}
Style constraints (linters, team conventions, language version): {{style}}

Code:
{{code}}

Process:
1. First, in a few bullets, state what the code does, so I can confirm you understood it. List its inputs, outputs, side effects and error behavior.
2. Identify the specific sources of complexity: deep nesting, long functions, duplicated logic, unclear names, mixed responsibilities, boolean flags, hidden state, magic values, and error handling that obscures the main path.
3. Show the simplified version in full. Prefer early returns, small well-named functions, clear data structures, and removing dead code. Do not introduce new abstractions, patterns or dependencies that are heavier than the problem.
4. Give a change log: each edit, its reason, and a risk note if any edit could alter behavior in an edge case (such as null handling, ordering, or short-circuit evaluation).
5. List the tests I should run or write to prove nothing changed, naming the exact inputs that exercise the riskiest edits.

If you spot a real bug, do not silently fix it. Keep the behavior, point out the bug separately and show the one-line fix.
Example output
**Understanding:** `shippingCost(order, user)` returns a number. Inputs: weight in kg, country, membership. It returns 0 for members on orders over 50, charges per kg with a surcharge for non-domestic orders, and caps cost at 80. No side effects; throws on negative weight. **Sources of complexity:** four nested ifs; the member check repeated twice; magic numbers 50, 80, 4.5; variable `x` reused for two meanings. **Simplified** ```js const FREE_SHIPPING_MIN = 50; const MAX_COST = 80; const RATE_PER_KG = 4.5; const INTERNATIONAL_SURCHARGE = 12; function shippingCost(order, user) { if (order.weightKg < 0) throw new Error("negative weight"); if (user.isMember && order.total > FREE_SHIPPING_MIN) return 0; const base = order.weightKg * RATE_PER_KG; const surcharge = order.country === "US" ? 0 : INTERNATIONAL_SURCHARGE; return Math.min(base + surcharge, MAX_COST); } ``` **Change log** - Flattened nesting with early returns (no behavior change). - Named constants (none). - Risk: original checked `order.total >= 50` in one branch and `> 50` in another; I kept the `>` version. Confirm which is right. **Possible bug:** weight 0 charges only the surcharge; kept as is. **Tests:** total exactly 50 as member; weight 0 international; weight 30 domestic (cap); negative weight.

Why it works

Restating the behavior first lets you catch misunderstandings before any code changes. Reporting bugs separately keeps a cleanup from quietly altering behavior.

Licensed CC0 (public domain) Version 1 · history