Computer Science I / Defensive Programming and Debugging
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
  1. A function receives a negative length and returns 0 instead of complaining. Where does the problem show up?
  2. 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.

Read the lesson: Defensive Programming and Debugging →

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