CoreMesh

Refactor Without Changing Behavior

Ask an AI to 'clean up' code and it may quietly change what it does. This prompt keeps behaviour fixed and asks for small, reviewable steps.

Works with Claude, ChatGPT and Gemini3 fields

prompt.md · 521 charslive
Refactor this JavaScript (Node.js 20) code. Goal: readability; keep it fast.

```
function total(items){let t=0;for(let i=0;i<items.length;i++){if(items[i].qty>0){t=t+items[i].qty*items[i].price}}return t}
```

Rules:
- Behaviour must stay exactly the same, including edge cases and error handling.
- Work in small steps; after each step show the code and one line on what changed.
- Do not add features, rename public functions or change the signature.
Finish with a list of cases I should test to prove nothing changed.

Everything you type stays in your browser. Remove passwords, keys and personal data before pasting anything into an AI tool.

Why this prompt is written this way

  • Behaviour fixed. Stating that behaviour must not change is the main guard against silent bugs.
  • Small steps. Each step is easy to review and easy to undo.
  • Test list. The final list tells you exactly what to check before you merge.

Example answer

Illustrative and shortened, to show the shape of a good answer. Real answers vary by model.

Step 1: rename t to sum, use for...of.
Step 2: replace the loop with items.filter(i => i.qty > 0).reduce(...).
Test: empty array, qty 0, negative qty, decimal prices.

Tips

  • Run your tests between steps if you have them.
  • Say what must not change (public API, performance) as part of the goal.

How to use it

  1. Replace the example values with your own. The prompt on the right updates as you type.
  2. Press Copy prompt and paste it into any AI chat, or open it straight in Claude or ChatGPT.
  3. Save it as prompt.md if you want to reuse it.
Claude ↗

New notes, no spam.

One post at a time, only when there's something worth reading. Follow in any feed reader.

Subscribe via RSS