25-Comp-B11 Advanced Software Design · May 2015
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer) · Last updated
Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.
Both aim to keep an operation's behaviour correct in the presence of possibly-invalid input, but they place the RESPONSIBILITY for ensuring validity on opposite parties.
Design by Contract (DbC) makes the CALLER responsible for satisfying an operation's precondition (Question 10). The supplier is entitled to assume the precondition holds and need not re-check it defensively; a violated precondition is treated as a bug in the caller, not a normal input the supplier must gracefully handle. Contracts are typically verified only via debug-mode assertions (which can be compiled out in production), since a violation signals a programming error to be fixed during development, not a runtime condition to recover from.
Defensive programming makes the SUPPLIER responsible: it does not trust callers to honour any implicit obligation, so it validates and sanitizes every input itself, at every call, regardless of what the caller was "supposed" to guarantee, and handles invalid input gracefully (returning an error code, throwing a well-defined exception, substituting a safe default) rather than assuming correctness.
Example (Stack.pop()). Under DbC: the precondition is !isEmpty(); the implementation does not explicitly re-check emptiness in the production build (only via an optional debug assertion) — calling pop() on an empty stack is the caller's bug. Under defensive programming: pop() explicitly checks isEmpty() first and, if true, throws a documented EmptyStackException (or returns an Optional) every single time, in every build, regardless of whether the caller was expected to have checked already.
When each is appropriate. Defensive programming is essential at a system's TRUST BOUNDARY — public APIs, user input, data from external systems — where the caller cannot be assumed to honour any contract at all. DbC is more appropriate for internal, tightly-coupled code within one team/module, where redundant defensive checks on every internal call both waste runtime and duplicate validation that a contract already documents as the caller's job. Good practice combines both: defend rigorously at the boundary, and rely on documented, assertion-checked contracts internally.