Sub topic 6
Hunting for edge cases and logic errors
This chapter assumes you can read a Git diff, write basic unit tests, and have used an AI coding assistant. By now you have checked the change against the requirement and read the diff. This chapter covers the next step: finding inputs and states where plausible code gives wrong results.
Why plausible code hides logic errors
An AI assistant writes the most likely code for the request you gave it. That is usually the happy path, where inputs are well formed and typical. The requirement often says nothing about the unusual cases, so the code says nothing about them either.
The result is code that reads cleanly and passes a quick manual try. It fails only when an input falls outside the common case.
Here is a typical example:
def average(values):
return sum(values) / len(values)It looks correct. But average([]) raises ZeroDivisionError, because len([]) is 0. The requirement never said what an empty list should do, so the assistant never decided.
A checklist of edge cases
For each function in the diff, ask what happens with these inputs.
| Category | Cases to try |
|---|---|
| Empty | `[]`, `""`, `{}`, `None` |
| Boundaries | 0, 1, -1, the maximum size, one past the limit |
| Off by one | First and last item, `<` versus `<=`, slice ends |
| Types | Wrong type, mixed types, floats where ints are expected |
| Duplicates and order | Repeated items, unsorted input |
| Text | Unicode, leading or trailing whitespace, very long strings |
| State and time | Time zones, repeated calls, concurrent calls |
| Failure | Network error, missing file, partial result |
You do not need every row for every function. Pick the rows that match what the function does.
A repeatable method
- Write down the inputs the function accepts, including ones the requirement did not mention.
- For each input class, trace the code by hand and note the result.
- Check every branch. Ask what is missing: an
ifwithout anelse, or a loop that never runs. - Compare each result with what a user would expect.
- Where the expected behavior is unclear, decide it yourself or ask the requirement's owner. Do not let the code decide by accident.
Turn the finding into a decision and a test
Once you find the empty-list problem, choose a behavior. Here the choice is to raise a clear error. Then pin that choice with tests, so later changes cannot undo it silently.
import pytest
def average(values):
if not values:
raise ValueError("values must not be empty")
return sum(values) / len(values)
def test_average_of_numbers():
assert average([2, 4, 6]) == 4
def test_average_of_empty_list_raises():
with pytest.raises(ValueError):
average([])Run it with pytest. Both tests should pass. If you return 0 or None instead of raising, that is also valid, but it is a deliberate choice you should record in a test.
Common logic errors in AI output
- Off-by-one errors. Loops and slices that skip the last item or read one past the end.
- Wrong operator.
andwhereoris needed, or>where>=is needed. - Swallowed errors. A broad
except Exception: passthat hides failures and returns a wrong value. - Mutated inputs. A function that changes the list or dict its caller passed in.
- Unstated assumptions. Code that assumes sorted input, unique keys, or non-empty data.
Exercise
A generated function returns the largest value in a list with return sorted(values)[-1]. Name two edge cases you would test and what you expect for each.
Show answer
First, an empty list. Indexing [-1] on an empty list raises IndexError, so decide whether to raise a clearer ValueError or return a default. Second, a list containing mixed types, such as ints and strings. Sorting raises TypeError, so decide whether to reject such input explicitly. Also worth noting: the function sorts the whole list, so max(values) is simpler and faster, and it fails on an empty list in the same way.