Most examples show a comfortable middle case. Bugs prefer the edges: no items, one item, repeated values, and the largest allowed input.

Build a small test set

  • Empty input, if the rules allow it.
  • The smallest valid input.
  • All values equal.
  • Already ordered and reverse ordered data.
  • Very large or negative values when relevant.

For every test, decide the expected answer before running the program. Otherwise a wrong result can look convincing.

Boundaries cause index mistakes

Ask whether an endpoint is included, whether a loop should use less than or less than or equal, and what happens when two pointers meet.

Make the empty case explicitPython
def average(numbers):
    if not numbers:
        return None

    total = 0
    for number in numbers:
        total += number

    return total / len(numbers)

print(average([]))       # None
print(average([4, 8]))   # 6.0

Edge cases are not tricks added after the real solution. They are part of the problem definition and should influence the design from the start.

Your tiny challenge

Choose the palindrome checker from Episode 15. List five tests, including an empty word, one letter, and a word that almost matches.