Demystifying Defensive Programming: A Modern Approach to Bulletproof Code

Is it a myth? A sophisticated way of programming? Or an obsessive concern for errors?

Gerardo Fernández
8 min readMay 2, 2023
Photo by Charlein Gracia on Unsplash

Lately, I’ve come across the term “defensive programming” in various places, and it seems to be gaining traction in the programming world. This article aims to discuss this intriguing concept, provide insight into how it can change the way you write your code, and introduce you to some essential principles that make up defensive programming. Chances are, you might already be familiar with some of these concepts; defensive programming leverages them to create code that can withstand future failures.

So, without further ado, let’s delve into this fascinating concept of defensive programming.

Defensive Programming: The Concept

Defensive programming is a coding approach where the developer anticipates potential problems that might arise in the code. This foresight, enabled by the defensive programming methodology, allows for the prevention of future errors and, more importantly, ensures that if errors do occur, the program won’t crash catastrophically.

In essence, defensive programming doesn’t revolve around the utopia of writing infallible code. Instead, it…

--

--

Gerardo Fernández

Entre paseo y paseo con Simba desarrollo en Symfony y React