Code Review
Get a prioritized code review that covers correctness, security, readability and tests.
Improve the structure and naming of code without changing what it does, with a list of changes.
Refactor the {language} code below for readability. Do not change its behavior.
<code>
{code}
</code>
<rules>
- Conventions: {conventions}
- Must not change: {must_not_change}
- Do not add features, new dependencies or new abstractions that are not needed.
- Prefer small, clear steps over a full rewrite.
</rules>
Give me:
1. A short list of what makes this code hard to read, ordered by impact.
2. The refactored code in one block.
3. A list of the changes, each with the reason (for example: "split the validation into its own function so the main flow reads top to bottom").
4. Behavior check: explain why the refactored code behaves the same as the original, and name any case where you are not sure.
5. Tests I should run, or write, to confirm nothing changed.
If you notice a bug in the original, do not silently fix it. Mention it separately under "Possible bugs", and keep the original behavior in the refactored version unless I tell you to fix it.| Variable | What to put in | Example |
|---|---|---|
| {language} | The programming language. | JavaScript |
| {code} | The code to refactor. | Paste the function or module here. |
| {conventions} | Style rules and conventions. | Use const, arrow functions and descriptive names; max 30 lines per function. |
| {must_not_change} | Behavior or interfaces that must stay the same. | The function signature and the returned object shape. |
Hard to read: one 70-line function mixes validation, calculation and formatting; names like d and tmp. Change: extracted validateOrder, calculateTotal and formatReceipt. Behavior check: the same operations run in the same order. Not sure: the original mutates the input object; the new version keeps that behavior. Possible bug: the discount is applied twice for orders above 100.
Claude tends to keep behavior intact when told what must not change. Be explicit about interfaces used elsewhere.
Works as-is. For larger files, ask for one function at a time to keep changes reviewable.
If you can attach the file and its tests, name both so the refactor can respect the existing tests.
Keep the instruction not to add features. A conversational assistant may offer extras you did not ask for.
Show the refactor as a diff instead of a full block.Keep the original structure but only improve the names.Which part of this refactor is the riskiest?Get a prioritized code review that covers correctness, security, readability and tests.
Generate unit tests that cover normal cases, edge cases and failures, with the reasoning for each.
Write clear docstrings and a README section from your code, without inventing behavior.