Refactor a function

Refactoring prompts fail by scope creep. This one draws the boundary explicitly, which is the only thing that stops it.

When to use it

A targeted change to code that is working and that people are nervous about touching.

What it prevents

Scope creep. Left unbounded, a model will add error handling for impossible cases, extract helpers nobody asked for, and rename things on the way past. The “change nothing that is not needed” line is the whole prompt.

The prompt

Copy this, then adapt it
Refactor the function below to reduce its nesting depth.

### Context
This runs in a hot path called on every request. The team is wary of rewrites because the last one caused an outage.

### Requirements
- Keep the signature and the behaviour identical, including for the empty-input case.
- Do not add dependencies, helper files, or error handling for cases that cannot occur.
- Output format: A single code block with the new function, then two sentences on what changed.
- Change nothing that is not needed to reduce nesting.

### Function
"""
function total(items) { if (items) { if (items.length) { return items.reduce((a, b) => a + b.price, 0) } } return 0 }
"""

Instant checks

9 key structural practices evaluated locally

100% Coverage7/7
  • States a concrete task

    Passed
  • Avoids vague language

    Passed
  • Specifies an output format

    Passed
  • Gives context or audience

    Passed
  • Avoids dangling references

    Passed
  • Avoids over-aggressive phrasing

    Passed
  • Defines what a good answer looks like

    Passed
Not a claim — the real checker, run on this prompt, in your browser.

Why each part is there

Every section of that prompt exists because a specific failure happens without it. These are the checks it passes, and what each one is protecting you from:

How to adapt this one

Swap the function and state the specific outcome you want — reduce nesting, remove a dependency, split one responsibility out. Keep “change nothing that is not needed”; without that boundary a model treats a refactor as an invitation to rewrite. And keep the note about the empty-input case: behaviour-preserving refactors break on the edge case nobody restated.

The mistake this prompt avoids

Leaving the goal implicit. “Clean this up” licenses the model to do anything — rename variables, add error handling for impossible inputs, extract helpers you now have to review. Name the one property you want changed and forbid everything else. A refactor with an unbounded scope is a rewrite you did not agree to.

A variation

For a larger, riskier change, ask for the diff in steps — “give me the smallest first change that is safe to ship on its own” — rather than one final version. Reviewing a rewrite as a single block is where regressions hide; reviewing it as a sequence of behaviour-preserving steps is how you keep the outage from happening twice.

Check your version

Once you have adapted it, run it through the checker. The same nine checks that graded this page will grade yours, free and in your browser — paste it in, or build one from scratch by answering a few questions.

More code prompts