CODE REVIEW
Review Code Boundaries by Finding a Cache Expiry Counterexample
Use equality at an expiry boundary to turn a cache requirement into a concrete counterexample and focused regression check.
A solution can be readable, short, and almost correct. Reviewing it means connecting each operation to the requirement, including the point where the requirement stops being true.
Consider a cache whose entries are valid strictly before their expiry time:
def get_cached(key, now):
entry = cache.get(key)
if entry and now <= entry.expires_at:
return entry.value
return MISS
Assume entries are objects with ordinary truthiness and now and expires_at use the same clock and units. We are inspecting the expiry rule, not designing the whole cache.
Translate the boundary
“Strictly before” excludes equality. Pick the smallest example that distinguishes the requirement from the implementation: now = 100, expires_at = 100.
The entry has expired. The condition still passes. We have a counterexample that explains the defect without relying on how convincing the implementation looks.
Changing <= to < repairs this particular boundary. It does not prove anything about concurrent updates, missing entries, clock choice, or eviction. Good review keeps the scope of its conclusion explicit.
Build a three-case check
For an expiry time of 100, inspect times 99, 100, and 101. The expected results are the stored value, MISS, and MISS respectively. Add a missing-key case separately.
These cases come from the contract. Generating expected results by running the condition we are reviewing would only reproduce its mistake.
Explain the repair
A useful review comment names three things: the requirement, the violating input, and the behavior after the change.
Entries must be invalid at expiry. When both times are 100, the current comparison returns a value. A strict comparison returns MISS at equality and still accepts earlier times.
Try this on your next plausible solution, whether it came from you, a colleague, or an AI tool. Before deciding that it works, find an input that could change your mind.
For another stateful boundary, try the expiring key-value store exercise. The guide to practicing code production describes how to make an independent attempt before reviewing a solution.