Join WhatsApp
Sample output Ā· AI-generated

1. Refactored code
```js
const FREE_SHIPPING_MINIMUM = 50;
const US_SHIPPING = 5.99;
const INTERNATIONAL_SHIPPING = 14.99;

function getShippingCost(order) {
if (!order) throw new Error("Order is required");
if (order.country !== "US") return INTERNATIONAL_SHIPPING;
return order.total >= FREE_SHIPPING_MINIMUM ? 0 : US_SHIPPING;
}
```

2. Changes
1. Replaced three levels of nested if-else with early returns, so each rule reads on one line.
2. Moved 50, 5.99 and 14.99 into named constants, so the pricing rules are clear and easy to update.
3. Kept the same error message and return values, so callers see no difference.

3. Possible bug (not fixed)
The country check is case-sensitive. An order with country "us" is charged international shipping. Confirm whether country codes are always uppercase.

4. Tests to run
- No order: throws "Order is required".
- US order with total 50: returns 0.
- US order with total 49.99: returns 5.99.
- Order from Canada: returns 14.99.

Text prompt Ā· ChatGPT Ā· Claude Ā· Gemini

Refactor code prompt for cleaner, more readable functions

Clean up a messy function while keeping its behaviour the same, with a short list of every change and the reason for it.

You are a senior [language] developer. Refactor the code below to make it easier to read and maintain.

Code:
```
[paste code]
```
Context: [what it does and where it is used]
Team style rules: [e.g. PEP 8, ESLint config, max function length]
Must not change: [public function names, return format, database calls]

Rules:
- Keep the behaviour exactly the same, including edge cases and errors.
- Prefer clear names, early returns, smaller functions and named constants instead of magic numbers.
- Do not add new libraries.
- Do not change formatting only for taste.

Give me:
1. The refactored code.
2. A numbered list of changes with one reason each.
3. Any behaviour you noticed that might be a bug, without fixing it.
4. Tests I should run to confirm nothing changed.
Type
Text
Fill in
5 blanks
Copies
Be the first

Updated Ā· By

Tip: Commit the refactor separately from new features so reviewers can check it in one focused pass.

1. CopyTap "Copy prompt" above.
2. Fill the blanksReplace each [bracket] with your own details.
3. Paste + refinePaste it into the chat, then ask for changes.

Why this prompt works

This refactor code prompt makes "keep the behaviour exactly the same" the first rule, because the biggest risk in any cleanup is a quiet change in what the code does. Listing what must not change, such as public names and return formats, protects the code that calls this function.

Asking for a numbered list of changes with reasons makes the result easy to review in a pull request. Separating possible bugs from the refactor keeps two kinds of change apart, so a cleanup never slips in a behaviour change nobody approved.

How to use this prompt

  1. Tap "Copy prompt".
  2. Replace every part in [square brackets] with your own details.
  3. Paste it into ChatGPT or Claude and send.
  4. Ask a follow-up like "make it shorter" or "give me 3 more options" to fine-tune it.

Variations to try

Split a long function

ChatGPT Claude Gemini
This [language] function is [number] lines long: [paste code]. Split it into smaller functions with clear names, each doing one job. Keep the original function as the entry point with the same signature. Explain the new structure in 3 bullet points.
Type
Text
Fill in
3 blanks
Copies
Be the first

Modernise old syntax

ChatGPT Claude Gemini
Update this [language] code from [old version] style to [new version] style, for example [callbacks to async/await, var to const and let]. Keep behaviour the same and list every change. Code: [paste code].
Type
Text
Fill in
5 blanks
Copies
Be the first

Remove duplication

ChatGPT Claude Gemini
These [number] functions repeat similar logic: [paste functions]. Suggest how to remove the duplication with a shared helper, show the new code, and explain when it would be better to leave some duplication in place.
Type
Text
Fill in
2 blanks
Copies
Be the first

Make it your own

  • Add your linter rules or style guide so the output passes CI on the first try.
  • Ask for type hints or TypeScript types as part of the refactor.
  • Add "Explain each change for a junior developer" if the refactor is for teaching.

Common problems and fixes

  • The refactor changes behaviour. Write or ask for tests first, then refactor, and run them after.
  • The AI adds clever but hard-to-read code. Add "prefer simple code a junior developer can read over shorter code".

FAQ

Should I refactor and fix bugs at the same time?

No. Refactor first with tests passing, then fix bugs in a separate change. It makes both easier to review.

How big a piece of code can I paste?

One function or class at a time works best. For whole files, ask for a refactoring plan first.

More Coding prompts

See all
ChatGPT Claude Text prompt

Debug code prompt that finds the root cause before fixing

Paste your code and the error message to get the real cause explained first, then a small fix and a test for it.

4 prompts
ChatGPT Claude Text prompt

Explain code prompt for beginners, line by line

Paste any snippet and get a plain-English summary, a line-by-line walkthrough and the concepts you need to understand it.

4 prompts
ChatGPT Claude Text prompt

Unit test prompt for edge cases in pytest or Jest

Get a list of test cases for a function, then ready-to-run tests for normal input, edge cases and errors in your framework.

3 prompts
ChatGPT Claude Text prompt

SQL query prompt: describe your tables, get the query

Describe your tables and the question you want answered, and get a correct query for your database with a short explanation.

4 prompts
ChatGPT Claude Text prompt

Regex prompt that writes and explains a pattern with tests

Describe what you want to match and get a working pattern for your language, explained piece by piece, with examples that should and should not match.

4 prompts
ChatGPT Claude Text prompt

Code review prompt for bugs, security and readability

A structured review of a pull request or snippet, with issues sorted by severity, each tied to a line and a suggested fix.

3 prompts
ChatGPT Claude Text prompt

API design prompt for clean REST endpoints and errors

Plan a REST API for a feature: endpoints, example requests and responses, status codes, error format and the decisions behind them.

4 prompts
ChatGPT Claude Text prompt

Vibe coding prompt: write an app spec before you build

Turn an app idea into a clear spec with features, screens, data and build steps that an AI coding assistant can follow one step at a time.

4 prompts