This is a series of posts that explains some of the fundamental rules for developing complex JavaScript applications.
How often have we seen function grow over time with just one more parameter added for that supposed last little piece of functionality we have to add. Far too often I would imagine. Then add in the new requirement that some of the parameters on the function need to be optional and the next thing you know you are in a world of hurt. There is a basic maxim in any software development, the only constant is change. As a result as a software writer I strive to ensure my code recognizes this maxim along with a couple of others.
Alas for JavaScript, the poor Yorick of languages we all know so well, it does not give us a lot of flexibility in the function object. Unlike other languages, even SQL, functions/methods provide much greater flexibility in there definition, including default values, optional parameters, name parameter calling, e.t.c. It would certainly be nice if we could provide a similar model to JS function calls and definitions. Many a moon a go I started developing in ExtJS and was introduced to a very simple method called Ext.apply and Ext.applyIf. In essence these methods take two objects as parameters and merge them into one. So if you call Ext.apply({a:1,b:2},{c:1}) you would get back {a:1,b:2,c:1}. Sounds simple enough right? The applyIf works in the same fashion but makes a test in the receiving object to see if the property already exists and if so does not over write it. This is a very simple piece of code that we can look at below:
function applyIf(objectTo,objectFrom){
if(objectTo){
for(var property in objectFrom){
if(typeof objectTo[property] == 'undefined' ){
objectTo[propety] = objectFrom[property];
}
}
}
return objecTo;
}
So in essence, enumerate through the properties in objectFrom and if the property does not exist in the objectTo then add the property and its value from the objectFrom. So what does this have to do with functions you are probably asking? Let’s have a look at a mythical function as an example
function doSomethingCool(
// width of element
width,
// height of element
height,
// text of element
text,
// optional x and y, element type and style string
,x,y,elType,style){…do some stuff}
Looks simple enough but what’s with the four of optional parameters at the end of the function. As you well know from in your function code you end up with a bunch of if statements along the lines of if(x===undefined){x=0}. This of course gets to be a pain in the code rear. Now let’s change our function to accept to a single parameters as an object.
function doSomethingCool(config){
config=applyIf(config,{
width : null,
height : null,
text : ’’,
x : 0,
y : 0,
elType : ‘div’,
style : ‘position:relative’
})
… do some stuff
}
So now in our doSomethingCool function we can easily default each parameter in our function and reduce the amount of if conditions to test for them. By defaulting each of our values we also reduce the risk of errors being introduced by bad callers.
This may seem like a simple idea but it has a couple of extremely important advantages. How many times have you had to go and look up a function you wish to call to check the order or the parameters? I would guess far too many times. Using a single parameters object order of the values in the object to not matter. But there is a far more important reason to use this method and that is in regards to the caller. In the standard function design you would see some code calling the method like thus:
doSomethingCool(10,10,’my text to display’,undefined,undefined,’span’)
Really does not really tell you much when you look at it does it. When another developer has to go through your code the above line tells him/her absolutely nothing and forces them to go and look at the doSomethingCool function to determine what 10 and 10 is and why are there two undefined. Now lets make the same call to the function using the config object parameters:
doSomethingCool({
width : 10,
elType : ‘span’,
height : 10,
text : ‘my text to desolay’
})
Looks a lot better doesn’t it! Not only is it very clear on what we are passing, we also did not need to add in a bunch of undefined definitions to get to the elType parameter. Notice also that in the above the order of object properties do not matter either.
A couple of noteworthy things about implementing your functions this way. If you need to add another parameter to your doSomethingCool function you can simply default it the default config object that is applied without any concern of impacting existing callers. Another great benefit is code readability. How often have you had to take someone else’s code over? Now imagine reading through that code and seeing all these clearly understandable calls. Not only does it benefit you when you need to revisit something but it will benefits the next developer who has to hit your code to make a modification.
In the next part of the series I will be exploring Crockford’s singleton pattern vs the prototypal pattern and how to merge, extend them and organize code.