Learn
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?
fetch_parcelneeds onlyparcel_id.fetch_eventsneeds onlyparcel_id. It never readsparcel.fetch_depotneedsparcel.depot_id, so it cannot start until the parcel has arrived.
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:
- Failure. Before, if the parcel call failed, the events call never ran. Now both are in flight. With the default
gather, the first exception reaches the caller at once, and the other call keeps running until it finishes. For two reads that is usually harmless. For a call that writes or charges, it is not. - Load. Each page view now holds two connections at the same moment instead of one. If the client has a small pool, or the events service has a rate limit, that matters.
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:
- Where does it run? On a page a person is waiting for, a third of the wait is worth a change request. In a nightly job, it is a note at most.
- What do the calls cost? Ask for a trace or a timing rather than guessing. If one call takes 5 ms and the other 400 ms, running them together saves almost nothing.
- Are they all reads? Two reads can overlap safely. A read next to a write needs an order, and the order is now gone.
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
- Find a function you wrote that makes two or more network or database calls in a row.
- Under each call, write the variables it reads. Draw an arrow from the call that produces each one.
- Any two calls with no arrow between them could run together. Write that version, in
asyncio, Java'sCompletableFuture, or whatever your stack uses. - 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.
- 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].