Chapter 12 · AI Architect / Lead

Silent edge-case failure

AI code is reliably correct on the examples it was shown and reliably wrong at the boundaries nobody demoed. A percentage helper computes passed / len(items) * 100 — flawless on [1, 2, 3, 4], and a ZeroDivisionError (0 / 0) the instant it meets the empty list it was never tested against.

This is the *silent* failure mode: green on every example, lethal at the edge. The edges that catch AI code are a short, predictable list — empty input, zero, None, division by zero, the first/last element, off-by-one at the end, float precision. When reviewing, walk that list deliberately; the model almost never does.

The fix is usually a single guard clause at the top that handles the boundary before the risky operation runs. if not items: return 0.0 removes the 0 / 0 before any division happens. Boundary handling is rarely clever — it's just remembering the boundary exists.

While you're there, check the *quality* of the output too: the AI version returns 33.33333333333333 because it never rounds. Correct-but-ugly is still a revise.

Syntax

# AI wrote this — correct on [1,2,3,4], crashes on []:
def pass_rate(items, predicate):
    passed = sum(1 for x in items if predicate(x))
    return passed / len(items) * 100   # 0 / 0 -> ZeroDivisionError

# Fixed — guard the boundary first, then round:
def pass_rate(items, predicate):
    if not items:
        return 0.0                      # the edge the demo skipped
    passed = sum(1 for x in items if predicate(x))
    return round(passed / len(items) * 100, 1)

# Boundary checklist: empty · zero · None · first/last · off-by-one · precision

Worked examples

The boundary that crashes the AI version

def pass_rate(items, predicate):
    passed = sum(1 for x in items if predicate(x))
    return passed / len(items) * 100

try:
    pass_rate([], lambda x: True)   # the input nobody demoed
except ZeroDivisionError as e:
    print(type(e).__name__)
# Output:
# ZeroDivisionError

The fix — one guard clause, plus rounding

def pass_rate(items, predicate):
    if not items:
        return 0.0
    passed = sum(1 for x in items if predicate(x))
    return round(passed / len(items) * 100, 1)

print(pass_rate([], lambda x: True))                 # 0.0   (no crash)
print(pass_rate([1, 2, 3, 4], lambda x: x % 2 == 0)) # 50.0
print(pass_rate([1, 2, 3], lambda x: x == 1))        # 33.3  (rounded)

Test the boundaries the demo skipped

f = pass_rate
assert f([], lambda x: True) == 0.0          # empty
assert f([2, 4, 6], lambda x: x % 2 == 0) == 100.0  # all pass
assert f([1, 3, 5], lambda x: x % 2 == 0) == 0.0    # none pass
assert f([1, 2, 3], lambda x: x == 1) == 33.3       # rounding
print('boundaries covered')
# Output:
# boundaries covered

Common mistakes & gotchas

Forgetting that empty is an input

Empty list, empty string, empty dict, zero count — the 'nothing' case is the single most-skipped input in AI code, and the one most likely to divide by zero or index out of range. Add if not x: thinking to every review.

Off-by-one at the boundary

AI loops and slices are frequently right in the middle and wrong at the ends — the last element, range stopping one short, < vs <=. Trace the very first and very last iteration by hand; that's where the boundary bug hides.

Accepting correct-but-sloppy output

33.33333333333333 is technically 'the right number' and still wrong for a percentage display. Edge review covers output quality too — precision, formatting, type. A boundary fix and a rounding fix often ship together.

Why it matters

Silent edge-case failures are the bugs that pass review, pass the demo, and then take down production on the first empty list. AI produces them constantly because it tests the middle, not the edges — so the reviewer who runs the boundary checklist every time is the one standing between green code and a 3am page.