Defensive programming
Defensive programming is an approach that anticipates invalid or unexpected inputs, states, and failures and uses checks or safeguards to handle them or limit their effects.
Data-Oriented Programming in Java
Defensive programming is a programming technique that anticipates invalid conditions and adds checks or safeguards to prevent their harmful effects.
- How it is used
- Uses checks to contain invalid states when a data representation permits conflicting or missing attributes.
- How deep it goes
- Works through constructor guards and use-site hazards, but stops short of replacing the flawed representation.
- Examples
- Demonstrates nullable completion fields, constructor validation, and growing conditionals for additional statuses.
- What you can do after
- Diagnose defensive code as evidence that a data model may permit illegal states or obscure domain meaning.
Read how Data-Oriented Programming in Java explains defensive programming →
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 →
Also covered in
- Build Financial Software with Generative AI (From Scratch)
- The Art of Code
- Functional Programming in C#, Second Edition
- Build Applications with Local AI Models on a Mac
- Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform
What to know first
- attributeAn attribute is a named property or piece of information associated with an entity, object, or structured data item.
- don't repeat yourself principleThe Don’t Repeat Yourself principle is a software design guideline that gives each piece of knowledge or behavior a single authoritative representation to avoid unnecessary duplication.
- enumAn enum is a type whose values are selected from a finite set of named alternatives.
- classA class is a programming-language construct that defines a type’s structure and behavior and provides a basis for creating or representing objects.
Related concepts
- try-catch blockuses · A try-catch block is a control-flow construct that executes code under exception handling and transfers control to an associated handler when a matching exception occurs.
- validationhas part · Validation is the process of checking data, behavior, or results against defined criteria to determine their correctness, acceptability, or suitability for a purpose.
- NullPointerExceptionaddresses · NullPointerException is a Java runtime exception raised when code attempts to use a null reference where an object is required.
- data modelingcontrasts with · Data modeling is the process of identifying domain information, defining its meaning, and designing its representation for storage and access.
- illegal stateaddresses · An illegal state is a program or system state that violates constraints required by its data model, domain rules, or specification.
- logginguses · Logging is the practice of recording events and diagnostic information produced during software or system activity for later inspection, analysis, or troubleshooting.
Questions about defensive programming
What is defensive programming?
Defensive programming is an approach that anticipates invalid or unexpected inputs, states, and failures and uses checks or safeguards to handle them or limit their effects.
Which Manning books explain defensive programming?
Data-Oriented Programming in Java, Object-Oriented Software Design in C++ and Software Design for Python Programmers.
What should I know before learning defensive programming?
Manning books that teach defensive programming usually introduce attribute, don't repeat yourself principle, enum and class first.
What is the difference between defensive programming and data modeling?
Defensive programming is an approach that anticipates invalid or unexpected inputs, states, and failures and uses checks or safeguards to handle them or limit their effects. Data modeling is the process of identifying domain information, defining its meaning, and designing its representation for storage and access.
What are the parts of defensive programming?
Defensive programming includes validation.
What is defensive programming also called?
Defensive programming is also called defensive coding.
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.


