Defensive Development - Fail Fast or Go Home

Defensive Development is a programming practice that is frequently misunderstood, but is nevertheless a critical practice to follow when working in many environments. I have seen articles written that argue that defensive development simply causes nonsensical null checks to be written, and as a result of seeing people writing bad code defensively, argues that no one should practice defensive development. There are other articles that, like many things in software development, argue that you should always use defensive development for everything.

While I am a propronent of defensive development, like most things, somewhere in the middle of the spectrum is where I would suggest developers fall. What this means for languages like PHP and JavaScript is that when you are writing a function that accepts arguments, you should always check to make sure they are in an acceptible state before acting upon them. For example, if you need to access a member of an array or object, first, make sure that the variable is of the proper type, and that the member of the object exists before trying to access the value of the member. Failure to do so may cause errors that prevent users from interacting with your site, or in the case of PHP errors in a framework like Magento, they may be left staring at a blank white screen.

Once you determine that the parameter passed to your function is not of the correct type, you should immediately signal a failure condition for your site/application to handle the failure in a user-friendly manner. The idea is to figure out that a failue condition exists as quickly as possible and then ensure that you can properly respond to it.

In addition to checking that parameters to functions are valid, you should also check the response values from outside functions that are called to ensure that you get sane return values.

While checking all of these variables and others have sane values before using them may seem like overkill, frequently in PHP and JavaScript errors that can be found with these techniques would present a bad user experience. Properly handling these situations makes the application to better serve the user experience and inform the user of what is happening so that it can be corrected and an ideal resolution can be reached.

Related Posts

Apr 24, 2014
2 minutes

Optimize Magento Time To First Byte

When looking at the performance of your site, waterfall charts are one of the first places you should investigate. The first thing that is represented on these charts is that the HTML for the website is the first resource that is downloaded every time.

As a result of being the first resource downloaded every time, this is the logical first place to look to improve the performance of your Magento website. There are a few ways to make sure that the page downloads more quickly, and they all involve making sure that the file size is as small as possible.

Apr 29, 2014
2 minutes

Avoid jQuery.bind()

When chasing down performance issues, you never know what kind of problems you will find. I was looking for something that would cause jitter when scrolling on the page. After looking at the custom code that runs on every scroll event, I still had not found a reason for the jitter. Looking at the JavaScript CPU profile when scrolling in Chrome showed that there was an overwhelming majority of the time spent in a function in the Prototype JS library.

Apr 4, 2014
2 minutes

Creating a Best-Sellers Category with Magento

Magento allows you to organize products in categories, and a single product can be a member of quite a few separate categories. As a result, you can create a category that is specifically for your top selling products. You could manually keep track of which products sell the best, either by number of sales completed, or by the actual quantity of each product that were sold. If you want to spend all your time managing this category, then this is the way to go. However, there is a much easier way to manage the products in the category.