Practice question · Multiple choice
Defensive programming favours failing loudly at the point of the error over continuing with a plausible substitute. Why is crashing sometimes better than carrying on?
Hints
- A function receives a negative length and returns 0 instead of complaining. Where does the problem show up?
- Ask how far a bad value can travel before anything notices.
Show the answer
D. Because a fault that surfaces immediately is localised and diagnosable.
Why
A function handed a negative length that quietly returns 0 has moved the failure elsewhere and removed the evidence, the wrong value flows on and surfaces in innocent code. It is not a rule about production behaviour: a web server should return an error, not exit. The distinction is between expected conditions, handled deliberately, and bugs, where an assumption is false.
Practise Defensive Programming and Debugging
The app has 7 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.
More questions on Defensive Programming and Debugging
- A function takes a list and an index and returns the element at that index. Select every check that belongs…
- Assertions are usually compiled out of release builds, and input validation never is. Why treat them…
- Order the steps by which an assertion turns a distant, mysterious failure into a local, obvious one.