Example result: Make confusing code easy to read
AI-generated example · Readability
Updated

Make confusing code easy to read

This prompt rewrites hard-to-follow code so a new teammate can understand it quickly. It keeps the same inputs, outputs and side effects, and lists every change with a short reason.

text-code-code-readability-rewrite — claude — 73 words

Act as a senior developer who writes very readable code.

Code:
[PASTE]

Rewrite it so a new team member can understand it quickly, without changing what it does. Use clear names, smaller functions, early returns instead of deep nesting, and remove clever tricks. Keep the same inputs, outputs and side effects. Show the new code, then list each change with a short reason, and point out anything whose behaviour you were unsure about.

Run your tests before and after. If there are no tests, ask for a few first so you know the rewrite did not change behaviour.

Get a better result

What you will get

Your code rewritten for a newcomer, with clearer names, smaller functions and early returns, the same inputs, outputs and side effects, a list of each change with a reason, and notes on anything the AI was unsure about.

Why it works

The prompt names the reader, a new team member, which sets the bar better than “clean code”. Keeping inputs, outputs and side effects fixed is the guard rail that turns a rewrite into a safe refactor. The list of changes and the “unsure” section make the result reviewable, so you can accept parts of it rather than all or nothing.

Common mistake

The rewrite quietly changes behaviour.

Fix: Run your tests before and after, as the tip says, and read the “unsure” list first. That is where a changed edge case hides.

Also watch for
  • Small helpers are split out until the code is hard to follow. Ask it to extract a function only when it gets a name that explains something, and to keep short steps inline.
  • It renames things that other files depend on. Say which names are public or used elsewhere, and ask it to keep those and only rename locals.
Try a variation: match the team style

Paste a file your team thinks is well written and ask for the rewrite to follow its naming, comment style and error handling.

Still not right? Reply with this

Split this rewrite into small steps, one change per commit, each with a commit message and a note on how to check it.

More chat & writing prompts

See all 465 →