Computer Science I / Defensive Programming and Debugging
Practice question · Select all that apply

A function takes a list and an index and returns the element at that index. Select every check that belongs in it as input validation.

Hints
  1. Validation checks assumptions about the INPUTS before they are used.
  2. Two options describe things the function neither knows nor has any business requiring.
Show the answer
  • B. That the index is less than the number of elements
  • C. That the list is not empty
  • D. That the index is at least 0
Why

Emptiness and the two index bounds are all assumptions about the inputs that can be checked at the boundary. What type the stored element has is not the function's business, and what the caller does afterwards is unknowable. Validating things you cannot see is as much a design fault as validating nothing.

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