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

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

At a glanceWhat the refactor deliversFrom a 28-line Python 3.11 order-total function
Edits explained in the change log
7 changes
Constants, helper, sum(), dead code removed
Edge cases to test
3 checks
Rounding, None input, bulk threshold
Possible bugs flagged, not fixed
2 bugs
Negative quantities, banker's rounding

Example for: a 28-line Python 3.11 function that totals order prices with bulk and member discounts; no tests yet.

1. Refactored code
BULK_THRESHOLD = 10
BULK_DISCOUNT = 0.9
MEMBER_DISCOUNT = 0.95


def calc_total(lst, m):
    items, is_member = lst, m
    if not items:
        return 0

    total = sum(_line_total(price, qty) for price, qty in items)
    if is_member:
        total *= MEMBER_DISCOUNT
    return round(total, 2)


def _line_total(price, qty):
    subtotal = price * qty
    if qty >= BULK_THRESHOLD:
        return subtotal * BULK_DISCOUNT
    return subtotal
2. Change log
#LocationChangeReasonBehavior note
1Top of fileReplaced 10, 0.9, 0.95 with named constantsExplains what the numbers meanSame values, same results
2calc_totalKept parameter names lst, m; added local aliasesReadable body without breaking keyword callersSignature unchanged
3calc_totallen(lst) == 0 check became early return on not itemsShorter, standard idiomSame for lists; VERIFY if None is ever passed
4Loop bodyExtracted _line_total helperSeparates per-line pricing from order rulesSame math per line
5LoopManual accumulator replaced by sum()Removes 4 lines of bookkeepingSame iteration order
6Member discountApplied once to total instead of to each lineRule now reads as one stepVERIFY: float rounding may differ in last cent
7BottomRemoved unused variable tmp_countDead codeNever read, no effect
3. VERIFY list
  1. Input: calc_total([(19.99, 3), (4.35, 12)], True). Expected: 106.59 in both versions. Confirms change 6.
  2. Input: calc_total(None, False). Original raised TypeError; new version returns 0. If any caller relies on the error, revert change 3 to the len() check.
  3. Input: calc_total([(5.0, 10)], False). Expected: 45.0. Confirms the threshold uses >= as before.
4. Possible bugs noticed (not fixed)
  • Negative quantities are accepted and reduce the total.
  • round() uses banker's rounding, so 2.675 can become 2.67; consider Decimal for money.

Assumptions: items are (price, qty) tuples and nothing imports tmp_count; I could not run the code, so items 1 and 2 above need a quick test.

Written by AI from this prompt. Your results will vary.

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 in Coding