All courses

Happy-path code: the edge cases it skipped

Probe agent code for the structural inputs it typically ignores: empty collections, None/null/nil and missing keys, zero and negative values, and off-by-one boundaries on ranges, slices and pagination. Predict which of these a given function will mishandle, and write the one test that would show it.

Builds on: Why agents fill gaps with plausible guesses

Read it card by card Start the whole course

Works on the sample, breaks at the edges

You asked the agent for two helpers for a product page: the average star rating, and paging through reviews. It ran a quick check on five sample reviews, the output looked right, and the summary says done. Nothing it tried had zero reviews, a review missing its rating, or the first page (page 1) when pages are numbered from 1. Those inputs are exactly where this kind of code fails.

Why agent code only covers the happy path

The agent writes the most typical continuation. Typical public code handles the typical input: a non-empty list, every field present, numbers in a normal range. Its own checks reuse that same kind of input. So a green run is a **proxy signal**: it shows the common case works and says nothing about the edges. **Later cost:** a crash or a silently wrong result the first time real data hits an edge, often far from this code.

Four probes for every input

For each input, try: - **Empty**: `[]`, `""`, no rows. - **Absent**: None/null/nil, a missing key. - **Zero/negative**: sizes, counts, divisors. - **Boundaries** (off-by-one): first page, last partial page, exact multiple, one past the end. Find the line each probe hits (a division, an index, a lookup, a `start`/`end`). Then ask how the language reacts. Python and Java mostly throw. JS returns `NaN`/`undefined` and carries on. Go silently returns the zero value for a missing map key (even in a nil map) and panics on a bad index. Rust panics on a bad index.

Predict before you run it

These are the agent's two helpers. Before you read on, predict which call crashes and which one silently returns the wrong data. Read the docstring and the `start` line together.
// Python
def avg_stars(reviews):
    total = sum(r["stars"] for r in reviews)
    return round(total / len(reviews), 1)

def page(reviews, page_no, size=20):
    """Pages start at 1."""
    start = page_no * size
    return reviews[start:start + size]

Walking the probes through it

**Empty:** `avg_stars([])` computes 0 / 0 and raises `ZeroDivisionError`, so a new product's page errors. **Absent:** a review with no `"stars"` key raises `KeyError`. **Boundary:** `page(r, 1)` starts at index 20, so the first 20 reviews never show. Nothing errors, because Python slices clamp, and even a page past the end just returns `[]`. One test shows it: `assert page(list(range(25)), 1) == list(range(20))` is red. What empty should return is your call, so tell the agent.

Remember

Agent code is written and checked against the typical input. Probe every input with empty, absent, zero/negative and boundary values. Predict the line that breaks, then pin it with one test that is red today.

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.