Computer Science I / Defensive Programming and Debugging
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
  1. Ask where the bad value came from in each case: your own code, or the user?
  2. 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.

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