Guard clauses before brittle arithmetic
Beginner
Defensive APIs: validation, sanitization & exceptions
Created by Pavel
· 29.04.2026 at 19:10 UTC
Division, logarithms, and normalisations explode on zero or negative values in the wrong domain. Guard clauses at the top of a function (if denom == 0: raise ValueError) document preconditions and avoid try/except around expected business violations.
In notebooks, silent inf and nan from bad divides propagate into metrics—guarding early keeps plots trustworthy.
This pairs with numpy conventions: np.where masks, divide with where=, or explicit checks before log.
Floating-point exceptions in NumPy: [1].
Sources
University approvals: 0
Tasks
Card Info
- Topic: Defensive APIs: validation, sanitization & exceptions
- Difficulty: Beginner
- Completed: 0 users
Creator
Pavel