What Is Defensive Programming?
As the name suggests, defensive programming is careful, guarded programming. To construct reliable software, we design every component in the system so that it protects itself as much as possible. We smash unwritten assumptions by explicitly checking for them in the code. This is an attempt to prevent, or at least observe, when our code is called in a way that will exhibit incorrect behavior.
Defensive programming enables us to detect minor problems early on, rather than get bitten by them later when they’ve escalated into major disasters. All too often, you’ll see “professional” developers rush out code without thinking. The story goes something like this:
They are continually tripped up by the incorrect assumptions ...
Get Code Craft now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.