liveBook · Manning

Defensive programming

Also called defensive coding

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.

How 3 Manning books explain it

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

What to know first

Related concepts

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.