When I first heard the term defensive coding, the first thought that popped to my mind, was that it meant the art of protecting your software against outside intruders, like those trying to gain access to functionality they are not entitled to. Later on after much research, I discovered that the term was not related to just hackers and the sort, but to protect that piece of software from anything unexpected that might disrupt the flow of that software you worked so hard on.
A short story:
Let us go through one quick scenario where defensive coding would have protected the day. There was one system a group of colleagues and myself developed some years ago for a shop. To keep it simple, there is an application to allow placing purchases made by clients in the shop, one piece of information of all the information that was recorded was the date the product should be delivered on. Now here comes the fun part, one order was actually placed with a shipping date in the past! The front-end development team, forgot to place validations to prevent such a date, and me non-the-wiser, took the order at the back-end side, and placed in a limbo state. Now the users working on the system checking for orders never saw the order, as I magically archived it somewhere seeing the shipping date is already past. Suffice to say the client didn’t get the order, at least not until the complaint came. Now what is wrong with this story, if the front-end team kept the validations in place, or I rejected the order in the back-end, in other words if we coded defensively and kept proper checks on the data flowing, this issue wouldn’t have happened.
Now looking at the story, someone will come and say this is a bug, which is true, that is why if you take a look from a different angle at the term defensive coding, it also means reducing the number of bugs in your all-powerful-software.
Defensive coding principles:
Now that story time is over, let us look at how we can code defensively, let me begin by asking a question, what is spaghetti code? If you don’t know the term, then please go ahead and Bing it. Now that we all know what that term means, we need to avoid making spaghetti and focus more on nice clean friendly code. Basically to make a clean code, you need to put these points in mind while coding:
1- Easy: Nobody wants to read a Latin code, make the code easy to read even by a child!
2- Clear intentions: Make your code self-explaining, don’t make people call of the services of Sherlock Holmes to crack what the hell is that function going to do.
3- Simplicity: 1 + 1 = 2, don’t make it 1 + 1 – 1 + 2 – 1 = 2.
4- Minimalistic: Each function or part of your code, does what it is supposed to do, in other words, you don’t make your car mechanic do your plumbing right?
5- Structure: Make your code structured, and well organized, again in practical life, you keep your white undies separate from your shirts, don’t you.
Defensive coding benefits:
What are the overall benefits I am going to gain if I build my code using defensive coding principles, in hind sight, you will gain these points among much others:
1- Easily scalable software.
2- Reusable pieces of the software.
3- Maintenance friendly.
4- Higher quality.
5- Less bugs.
7- Consistent behavior.
If you want to start learning defensive coding, you might want to look up a couple of articles on some coding architectures, my personal best is multitier architecture, and you might want to look up some more articles on coding conventions such as naming conventions.