6

Consider the following code:

function foo(handlers) {
  callSomeFunction(handlers.onSuccess, handlers.onFailure);
}

The caller may do:

foo({onSuccess: doSomething, onFailure: doSomethingElse });

or simply just

foo()

if s/he has nothing special to do.

The issue with the above code is that if 'handlers' are undefined, like in the simple foo() call above, then a run-time exception will be thrown during the execution of callSomeFunction(handlers.onSuccess, handlers.onFailure).

For handling such situations one may re-write the foo function as:

function foo(handlers) {
  var _handlers = handlers || {};
  callSomeFunction(_handlers.onSuccess, _handlers.onFailure);
}

or more structured

function safe(o) {
  return o === Object(o) ? o : {};
}

function foo(handlers) {
  callSomeFunction(safe(handlers).onSuccess, safe(handlers).onFailure);
}

I tried to find something like the safe() function in libraries such as sugarjs and underscore, but found nothing. Did I miss something? Is there any other utility library that has a similar function?

Just trying not to reinvent the wheel... ;)

BR, Adrian.

P.S. I didn't test the code above, so it may have errors.

Adrian Herscu
  • 738
  • 1
  • 6
  • 19
  • 1
    `var _handlers = handlers || {};` is sufficient for defaulting parameters. no need for libraries. – jbabey Oct 22 '12 at 14:41
  • 1
    I cant quite decide if this is a valid question... but either way, why would you want a library for something you have demonstrated you can achieve with a single line of code? – musefan Oct 22 '12 at 14:42
  • 2
    `if(!handlers)handlers={}` or `handlers=handlers||{}` is just too common and equally short as using a library function `handlers=safe(handlers)` - no need for that – Bergi Oct 22 '12 at 14:42
  • Check [this](http://stackoverflow.com/questions/411352/how-best-to-determine-if-an-argument-is-not-sent-to-the-javascript-function). The answer gives a couple of ways to do it and also some best practices. – bhb Oct 22 '12 at 15:47
  • @jbabey that is an additional line of code to be added to each function like foo() – Adrian Herscu Oct 22 '12 at 16:04
  • @musefan look at underscore's code, each function there is just couple of lines; I could easily re-write what I need, but that is reinventing the wheel – Adrian Herscu Oct 22 '12 at 16:06
  • So when you have to add two numbers like `var c = a + b;` do you also look for a library in fear of "re-inventing the wheel"? My point is, there are lots of things that you will do repeatedly and that just has to be done. If you are concerned about ensuring you use exactly the same method each time, then what comfort do you have to ensure that you always call the same function each time? it's just a different version of the same concern. – musefan Oct 22 '12 at 16:27

1 Answers1

2

There is something pretty similar in jQuery, often used when dealing with plugin development wich is jQuery.extend:

function foo(options){
    var defaults = {onSuccess: someFunction, onFailure: someOtherStuff}
    var options = $.extend({}, defaults, options); 
}

In this case it make sense, because user can provide an arbitrary subset of options, and the function with add the defaults for those not provided in the function call.

ArtoAle
  • 2,939
  • 1
  • 25
  • 48