All courses

Refactors that quietly change behavior

Spot 'pure cleanup' that changes behaviour: reordered side effects, a different error type, lost short-circuit evaluation, changed iteration order or null handling. Explain why callers who depend on that old behaviour break.

Builds on: What the agent cannot see

Read it card by card Start the whole course

Tidy diff, green tests, a broken caller

You asked the agent to tidy `billing.py`: shorter functions, modern idioms. The diff reads well, tests are green, and the summary says "pure refactor, no behaviour change". You approve it. A week later, a payment path in another service starts failing, and nobody touched that service. The cleanup changed something a caller relied on. How could you have seen it in the diff before merging?

Looks the same is not behaves the same

The agent judges equivalence by reading. Two versions that *look* alike feel alike. But it opened the function, not its callers. It never saw production data with missing cards, or the reason one check came first. Tests usually cover the main path, so green is a proxy signal here: same results on easy inputs. **Later cost:** callers that relied on an error type, an order or a skipped call break far from the diff.

Five questions for every 'cleanup' hunk

1. **Side effects:** do writes, logs, sends or counters now run before a check, or in a different order? 2. **Errors:** is it the same exception type, raised at the same point, for the same input? 3. **Short-circuit:** is a call that used to sit behind `and`/`or`/`&&`/`||` now always evaluated? 4. **Order:** did a list become a set, or does the code now range over a Go map? 5. **Null/empty:** watch `if x` vs `is not None`, `||` vs `??`, and `0` or `""`.

A readable rewrite that lost short-circuiting

The new version reads more clearly, but a list literal evaluates every element while the list is built, before `all()` sees any of them. `all()` stops at the first falsy item, but by then every call has already run. A user with no card now hits `user.card.is_valid()`, which raises `AttributeError` instead of returning `False`. Users with an invalid card now also reach `fraud.check`. **Later cost:** crashes on card-less users, plus extra fraud calls that may be billed or logged. BETTER: keep the `and` chain.
// Python
--- a/billing.py
+++ b/billing.py
 def can_charge(user):
-    return (user.card is not None
-            and user.card.is_valid()
-            and fraud.check(user))
+    checks = [
+        user.card is not None,
+        user.card.is_valid(),
+        fraud.check(user),
+    ]
+    return all(checks)

A new error type that callers never catch

The agent "unified error handling". Each `throw new IllegalArgumentException(...)` became `throw new InvalidOrderException(...)`, a new class extending `RuntimeException`. The tell: an exception type changed, but no catch sites appear in the diff. A controller the agent never opened catches `IllegalArgumentException` and returns 400. **Later cost:** the new type slips past that catch, so clients get 500. BETTER: extend `IllegalArgumentException`, or update every catch site and list each one.

Pure means every caller sees the same thing

A refactor is pure only if callers see the same side effects in the same order, the same errors, the same calls skipped, the same result order, and the same null and empty handling. If a hunk touches any of these, find out who depends on it before you believe "no behaviour change".

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.