Practice question · Multiple choice
Assertions are usually compiled out of release builds, and input validation never is. Why treat them differently when both check conditions?
Hints
- Ask where the bad value came from in each case: your own code, or the user?
- One condition should be impossible. The other is merely unwelcome. Which is which?
Show the answer
A. Because an assertion checks the code's own assumption, not outside input
Why
An assertion says 'this cannot happen' and firing means a bug; validation says 'this might happen' and firing is normal operation. Compiling out assertions is defensible because a shipped program should not contain the impossible, compiling out validation would mean trusting the outside world, which is how security holes are made.
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…
- Order the steps by which an assertion turns a distant, mysterious failure into a local, obvious one.
- Defensive programming favours failing loudly at the point of the error over continuing with a plausible…