Refactor Pasted Code for Readability Without Changing Its Behavior
Returns a cleaner version of your code plus a change log that explains each edit, why behavior is preserved, and which edge cases you should test.
You getRefactored code in one complete code block, change log table and VERIFY list
Prompt
Act as a senior software engineer doing a careful readability refactor. I will paste code right after this prompt. If I have not said the language, runtime version, or whether tests exist, ask me up to 3 short numbered questions in one message and wait for my answers.
Your job: make the code easier to read and maintain while keeping observable behavior identical: same outputs, return values, side effects, raised errors and public names.
Rules:
1. Do not add features, fix bugs or change public function signatures. If you spot a bug, list it separately and leave it in place.
2. Every change log entry names the function or line, what changed, why it reads better, and why behavior is unchanged.
3. Mark any change that could alter results in edge cases (ordering, floating point, empty or null input, exceptions, mutation, performance) as VERIFY, with a concrete test input.
4. Prefer small, standard moves: clearer local names, extracted helpers, early returns, named constants, removing dead code.
5. Keep comments only where the reason is not obvious from the code.
Output format:
1. Refactored code in one complete code block
2. Change log table: # | Location | Change | Reason | Behavior note
3. VERIFY list: test input and expected result for each
4. Possible bugs noticed (not fixed)
End with one line stating your assumptions and what you could not confirm without running the code.Example output
Tips and common edits
Why this works
- Banning bug fixes and signature changes keeps the refactor safe to merge without surprises for callers.
- A behavior note per change forces the model to justify equivalence instead of just claiming it.
- VERIFY items with concrete inputs turn hidden edge-case risks into tests you can run in a minute.
Use before a code review or when inheriting messy code. Paste the full function or file into ChatGPT, Claude or Gemini, and mention the language version and whether tests exist.
Common edits
- Ask it to also write unit tests for each VERIFY item in your test framework.
- Allow renaming public functions if the code is internal and you control all callers.
Prompt details
- By
- Prompts-GPT editors
- Published
Did this prompt work?
Keep exploring
More like this
Resolve a Git Merge Conflict and Keep What Both Branches Intended
Paste your conflict markers and get a plain explanation of each side's change, a merged version that keeps both intents, and a test checklist.
Explain Any Regex Token by Token with Matching and Failing Examples
Get a token-by-token table explaining your regex, plus strings that match, nearly match and fail, and a clear catastrophic backtracking risk check with a safer rewrite.
Review a Beginner's Code with Kind, Prioritized Feedback on the Top 3 Fixes
Paste your code and get a mentor-style review with one specific compliment, the three highest-impact fixes ranked with corrected snippets, and one next step.
Explain a Complex SQL Query in Plain English, Step by Step
Get a plain-English walkthrough of any SQL query in execution order, with tiny sample tables, row counts after each clause, and the hidden gotchas.
Add Docstrings and Why-Focused Comments to Your Pasted Code
Returns your code with standard-style docstrings covering parameters, return values and raised errors, plus comments that explain intent rather than restate the code.
Explain Pasted Code Line by Line with a Worked Example for Beginners
Get a block-by-block walkthrough of any code you paste, with one example input traced through every step and three questions to check that you understood it.