The handler waits for two calls in turn, and neither needs the other's answer

The interviewer shows you ten lines and asks how you would improve them. The code runs, the tests pass, and nothing looks wrong. "It's fine" is a tempting answer, and it is the one that costs the most, because the question is really asking what you checked before deciding it is fine.

The situation

Here is an original example. A parcel-tracking page loads the parcel, its scan history and the depot that holds it, then renders all three:

async def tracking_page(parcel_id):
    parcel = await fetch_parcel(parcel_id)
    events = await fetch_events(parcel_id)
    depot = await fetch_depot(parcel.depot_id)
    return render(parcel, events, depot)

Every line does what it says. Each await waits for one network call to finish before the next one starts.

What goes wrong, and the question that exposes it

Read each call and ask one thing: what does it need that it does not already have?

So the first two calls are independent, and the code still waits for one before starting the other. If each call takes about 200 ms, the page waits about 600 ms. Started together, the first two finish in about 200 ms, and the depot call adds 200 more:

import asyncio

async def tracking_page(parcel_id):
    parcel, events = await asyncio.gather(
        fetch_parcel(parcel_id),
        fetch_events(parcel_id),
    )
    depot = await fetch_depot(parcel.depot_id)
    return render(parcel, events, depot)

gather returns the results in the order you passed the calls, so the unpacking is safe. With three stand-in calls that each sleep for 200 ms, the old version takes 0.6 seconds and this one 0.4.

The trap on the other side is to put all three into one gather. That line evaluates parcel.depot_id before parcel exists, and Python raises UnboundLocalError. A reviewer who says "run everything in parallel" without reading the arguments has made the opposite mistake.

Two more things change, and a careful review names them:

How to argue the severity

The diff is correct, so this is not a blocking defect. Whether it is worth a comment depends on facts you can name:

That turns "this could be faster" into "this page waits 600 ms where 400 would do, because two reads that share no data are started one after the other".

What to say out loud

"This works, so I'd look at what each call depends on. The events call only needs the parcel id, not the parcel, so it can run at the same time as the parcel call. The depot call needs depot_id from the parcel, so it has to wait. If each call is about 200 ms, that takes the page from roughly 600 to roughly 400. Before I change it, I'd check that both are reads, that the client allows two requests in flight, and what the page should show if the events call fails."

That answer gives the improvement, the call that cannot move, the number, and the conditions under which you would not do it.

The exercise

  1. Find a function you wrote that makes two or more network or database calls in a row.
  2. Under each call, write the variables it reads. Draw an arrow from the call that produces each one.
  3. Any two calls with no arrow between them could run together. Write that version, in asyncio, Java's CompletableFuture, or whatever your stack uses.
  4. For each pair you would run together, write one line on what happens if one of them fails and one on whether either writes anything.
  5. Say the out-loud answer above for your own function in under forty seconds, including one reason you might leave it as it is.

Where this fits in Gadfly

Gadfly has four rooms: algorithms, system design, behavioral and code review. The code review room takes a class you wrote yourself, a connected GitHub repository, or a project from its catalog, and reviews it together with the transcript of how the code was produced. It does not execute code, so timing a handler and tracing what each call depends on is reasoning you explain aloud rather than measure in the room. Code review is part of a session pack. You code in Java or Python, and the tutor speaks English.

Published 19 November 2026 · Code review: find it, then defend it · all notes · feed

This note was drafted by Gadfly's scout — an automated writing agent run by Dmytro Hryshchenko — and checked by rules against the product's claims registry, not read by a person before it went live. The exercise is original. Spotted a mistake? [email protected].