Defensive Development Failure

In the past, I have argued that devensive development is a useful tool to ensure unexpected exceptions are not introduced into a piece of software as well as ensuring that the error conditions are handled in an appropriate manner. Unfortunately, if defensive development is implemented poorly, it achieves none of its goals and can cause errors and exceptions to occur. One example that I found while reviewing some code recently is below:

var func = function(objParam1) {
	var isValid = true,
    	status;
    if(objParam1 === null) {
    	isValid = false;
        status = "failure";
    }
    if(objParam1.value1 === null) {
    	isValid = false;
        status = "failure 2";
    }
    if(isValid === false) {
    	return status;
    }
    /* Do Stuff Here */
};

So, do you see what the problem is here? The issue is that while we are properly detecting that objParam1 is null, we do not short-circuit the execution of the code, instead allowing it to fall into the check on objParam1.value1 and whether it is null. However, null does not have a member named value1. In JavaScript, this would trigger an unhandled exception and prevent the execution of other JavaScript on the page.

The correct way to write the above code in a Defensive Development method would be as follows:

var func = function(objParam1) {
    if(objParam1 === null) {
    	return "failure";
    }
    if(objParam1.value1 === null) {
    	return "failure 2";
    }
    /* Do Stuff Here */
};

As you can see, this code is much more concise, and fails fast, ensuring the least amount of code is executed as is possible.

Related Posts

Apr 30, 2014
2 minutes

Always Namespace Variable Names in JavaScript

After running into a few issues with variable naming collisions over the past few days, it drives home how much we all should be namespacing our variable names in JavaScript. When writing JavaScript code that is only in use on your own website, you should still always namespace your variables. If you are writting a JavaScript library that will be in use on any website a user puts it on, namespacing your variable names is a minimum requirement.

Mar 31, 2014
2 minutes

JavaScript Templating

Many times it becomes useful to be able to make an AJAX request for some data, insert it into some HTML that is already on the client, and then display it to the user. There are a few ways to implement this, each approach has its benefits and drawbacks.

String Concatenation

Possibly the simplest way to accomplish the templating in JavaScript is to use simple string concatenation with ‘+’. This is the approach that I see many newcomers to JavaScript use in their code, as it is the simplest to implement. However, it does have a major drawback in that this method has the worst performance of all, especially in older versions of Internet Explorer. This could be implemented as below:

May 1, 2014
One minute

Why Use AddThis.com Sharing Buttons

AddThis.com produces a bit of JavaScript that enables website owners to quickly and easily add social sharing buttons/links to their website. In addition to allowing users to easily share your content socially, they also provide analytics information about what content is shared via which method, giving a greater insight into visitor behavior.

However, just because it provides these benefits to websites does not necessarily mean that it is a good tool to implement on your website. If you are concerned about the performance and the total payload size for your website, then you may have some reservations about the AddThis sharing buttons. On one site I was looking at, when all of the AddThis JavaScript, CSS and Images were combined, it took up 250kB. When you consider that this is a responsive website, and many mobile users only get 300MB of data before they get to overage fees, you hope that there are other ways to implement the sharing to the major social sites.