Defensive programming
Defensive programming is a technique that anticipates invalid inputs, failures, misuse, and unsafe states and explicitly guards against their harmful effects.
Object-Oriented Software Design in C++
A programming technique that validates inputs and guards against invalid states or programming errors.
- How it is used
- Guards object construction and search operations against programming errors in a book-catalogue design.
- How deep it goes
- Explains runtime parameter checks through a variant-backed map and constructor assertions, while deferring broader checking in short examples.
- Examples
- Uses typed book attributes, constructor assertions, catalogue searches, and partial queries such as genre and author.
- What you can do after
- Apply runtime validation to construct only proper objects from parameter values.
Read how Object-Oriented Software Design in C++ explains defensive programming →
Software Design for Python Programmers
Defensive programming is a technique that validates inputs and guards against programming errors to preserve valid program states.
- How it is used
- Uses runtime checks to prevent invalid objects and guard application state against programming errors.
- How deep it goes
- Shows constructor assertions and matching behavior, while noting that broader examples generally omit parameter checks.
- Examples
- Uses typed book attributes, invalid-value prevention, case-insensitive author matching, and partial catalogue searches.
- What you can do after
- Build valid Python domain objects and diagnose invalid parameter values before they enter the application.
Read how Software Design for Python Programmers explains defensive programming →
Related concepts
- try-catch statementuses · A try-catch statement is a control-flow construct that executes code under exception handling and transfers control to a matching handler when an exception is raised.
- validationhas part · The process of checking data, behavior, systems, models, or results against defined criteria to assess their correctness, suitability, or acceptability.
- NullPointerExceptionaddresses · NullPointerException is a Java runtime exception raised when code improperly dereferences or otherwise uses a null reference where an object is required.
- illegal stateaddresses · An illegal state is a program or system state that violates constraints required by its data model, domain rules, or specification.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

