5 questions to test your understanding
A function calculate_average(numbers) divides the sum by the length of the list. A colleague calls it with an empty list, causing a division-by-zero crash. Whose responsibility is this failure under a contract-based design approach?
A developer finds that her function is hard to write a contract for — the postconditions have many special cases and the preconditions are tangled and interconnected. What does this signal?
A well-designed function should be able to handle any input it receives, even invalid ones, by returning a sensible default or raising a graceful error.
Writing a function's contract before implementing it — specifying preconditions and postconditions first — helps clarify what the function should do and guides how to test it systematically.
How does thinking in terms of preconditions and postconditions change the approach to testing a function, compared to testing without a formal contract?