All courses

Catch plausible-but-wrong code

Find bugs in code that compiles, reads well and passes its tests: skipped edge cases, text, time and money traps, scale problems, unawaited async work, leaked resources, races, unsafe retries, missed call sites, behaviour-changing refactors and invented APIs. Choose the check that would expose each one.

Builds on: Happy-path code: the edge cases it skipped, Text, time and money: classic data traps, Fine on test data, broken at scale, Unawaited async work and floating promises, Leaked resources: files, connections, goroutines, Races introduced by plausible concurrent code, Retrying operations that aren't idempotent, Half-finished changes: the call sites it missed, Refactors that quietly change behavior, Invented APIs, flags, config keys and packages

Read it card by card Start the whole course

Green, clean and still wrong

The agent finishes a 'batch refunds' task. The diff compiles, the names are clear, 9 new tests are green, and the summary says *'handles all cases'*. None of that rules out the traps you have studied. Each of them compiles, reads well and passes small tests. So the useful question is not 'does it look right?' It is: **which input, load or timing would break it, and which check would show that?**

Why plausible code hides these bugs

The agent writes the most likely code for its context, and likely code fits the common case. That means small inputs, ASCII text, one caller, one thread, replies that always arrive, and the API version it remembers. Its tests come from the same guesses, so they share the same blind spots. Green only proves the cases it imagined. **Later cost:** the bug ships and shows up under real data, load or timing, far from the change.

Seven questions, seven checks

For each changed function, ask: 1. **Inputs:** empty, null, zero, boundary? → one targeted test 2. **Data:** non-ASCII, time zones, money? → a test with a real value 3. **Volume:** a query or full read in a loop? → query log, large fixture 4. **Async/resources:** all awaited, all closed? → linter, many-item run 5. **Shared state:** concurrent writes? → race detector, many-item run 6. **Retries:** safe if the first try succeeded but its reply was lost? → a test of exactly that, counting effects 7. **Reach:** all callers, same behaviour, real APIs? → repo-wide search, a test of the old edge case, docs for the installed version

Two traps behind one green summary

Summary: *'Renamed `getUser` to `fetchUser` everywhere and simplified `syncAll`; build and tests green.'* Trap 1: `syncAll` now calls `users.forEach(async u => await push(u))`. It returns before any push finishes, and the test never waits for the pushes. Later cost: callers think the sync is done while pushes are still running, and a failed push is never reported. Trap 2: 'everywhere' means the files the agent read. `rg getUser` finds the old name as a string in a job config. Later cost: the job breaks the next time it runs. Checks: the `no-misused-promises` lint and a repo-wide search. Fix: `for...of` with `await`, or `await Promise.all(...)`.

Four traps, four checks

Summary: *'Refunds retried on timeout, tested with 3 orders.'* - `Order.objects.get` in the loop runs one query per order. Read the query log with 5,000 orders. Later cost: big batches crawl and time out. - `* 0.9` brings in a float. A float `total` drifts by cents. A `Decimal` one raises `TypeError`. Test with a real amount and use `Decimal('0.9')`. - A retry after a timeout refunds twice if the first call went through. Test that case, then send an idempotency key. Later cost: double payouts. - After 3 timeouts the amount is still appended to `refunded`. Test with every call timing out. Later cost: a failed refund is reported as done.
// Python
def refund_all(order_ids):
    refunded = []
    for oid in order_ids:
        order = Order.objects.get(id=oid)
        amt = order.total * 0.9
        for _ in range(3):
            try:
                gateway.refund(oid, amt)
                break
            except TimeoutError:
                continue
        refunded.append(amt)
    return refunded

Remember

Plausible code fails on the inputs, data, volume, timing and reach the agent never saw. Read each change against the trap list. For every suspect, name the one check that would expose it: a targeted test, the query log, a linter, the race detector, a search or the compiler.

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.