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
- Validation checks assumptions about the INPUTS before they are used.
- 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.
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
- 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.
- Defensive programming favours failing loudly at the point of the error over continuing with a plausible…