Topic 4 of 5 · 5 min read
Edge-case inputs AI code forgets
Goal: List the edge-case inputs an AI-written function fails on and the checks or tests that cover them.
AI-written functions are tuned for the example in the prompt; your job is to feed them the inputs the prompt never showed. The failures cluster in a short, predictable list.
Check yourself · 1 of 3
An AI function averages a list by dividing the sum by the length. Which input breaks it?
Why it matters
A model writes the common case well because it has seen it many times. It often has no signal that your data includes an empty cart, a name with an apostrophe or a customer in another time zone. Real systems meet those inputs on day one, and tests written by the same assistant tend to cover only the inputs it already thought of.
A checklist to apply
For every function that takes input, ask what happens with:
- Nothing. Empty list, empty string,
null,undefined, missing key, zero. Averaging an empty list divides by zero.items[0]on an empty array is an error orundefined. - One. A list of one item breaks code that assumes pairs or compares neighbours.
- Too much. Very large numbers, huge files, ten thousand rows loaded into memory, deep nesting.
- Odd text. Unicode, emoji, accents, right-to-left text, leading and trailing spaces, very long strings. Cutting a string at a byte or character index can split a character. Comparing names case-insensitively is not the same in every language.
- Time. Time zones, daylight saving changes, month ends, leap days. Storing local time without a zone is a classic.
- Money and numbers. Floating point for currency, rounding, negative values, NaN.
- Partial data. A record that has some fields missing, or an API response that omits a key the code indexes directly.
Before and after
function averageRating(reviews: { stars: number }[]): number {
return reviews.reduce((sum, r) => sum + r.stars, 0) / reviews.length;
}
For a product with no reviews this returns NaN, which then renders on the page or is saved in the database. The reviewed version decides what "no data" means:
function averageRating(reviews: { stars: number }[]): number | null {
if (reviews.length === 0) return null;
return reviews.reduce((sum, r) => sum + r.stars, 0) / reviews.length;
}
Then the caller shows "No reviews yet". Add tests for 0, 1 and many reviews.
What goes wrong
- The agent's tests mirror its code, so edge cases are untested and the suite still looks complete.
- Validation only at the front. The UI checks the input but the API accepts anything.
- A silent coercion. A parser turns bad input into
0or""instead of rejecting it. - Truncating at the wrong unit, so an emoji is cut in half and shows as a broken symbol.
Examples
The first item
def latest_order(orders):
return sorted(orders, key=lambda o: o.created)[-1]
A new customer has no orders and gets an IndexError. Return None for an empty list and let the caller handle it.
Midnight in another zone
const today = new Date().toISOString().slice(0, 10);
For a user in New Zealand the UTC date is yesterday for part of the day, so their "today's tasks" are wrong. Store UTC instants, and compute the day in the user's time zone.
Key points
- Check every function against the same list: nothing, one, too many, odd text, time, numbers, partial data.
- Decide explicitly what an empty or missing input means, and make the return type say so.
- Dates and money need a stated time zone and a decimal or integer-cents type, never a guess.
- Tests written by the same agent usually repeat its blind spots; add the edge cases yourself.
- Validate at the API boundary, not only in the user interface.
Common mistakes
- Testing only with the example from the prompt.
- Using floating-point numbers for prices.
- Assuming names are ASCII and fit in 20 characters.
- Treating a missing field and a zero value as the same thing.
Check yourself · 2 of 3
Which type is safest for storing a price in code you review?
Check yourself · 3 of 3
An AI wrote tests for its own function, and all pass. What should you still add?
Practice items, not counted towards your mastery.
Start here
- Making retries safe with idempotent APIs (Amazon Builders' Library) (opens in a new tab)ArticleFree
How idempotency tokens let a client retry a request without doing the work twice.
- Timeouts, retries and backoff with jitter (AWS Builders' Library) (opens in a new tab)ArticleFree
Why every remote call needs a timeout and how capped backoff with jitter avoids retry storms.
- Error Handling Cheat Sheet (OWASP) (opens in a new tab)DocsFree
Handle errors centrally, fail safely and log enough detail without leaking it to users.