I’m writing a JavaScript plugin that receives a set of settings, some required, some not. It’s actually a plugin for Raphael.js but this question extends to jQuery plugins too.
My code looks something like the following:
jQuery.fn.myplugin = function (settings) {
settings = settings || {};
var reqParam1= settings.requiredParameter1 || return; //Or throw? //Or ... ?
/* More code */
}
If I determine that the plugin didn’t receive all the required settings, what course of action should I take? The obvious and most conservative choice is to throw but what about in the case where the plugin functioning isn’t ‘essential’? By not ‘essential’ I mean purely an aesthetic enhancement or less-than-critical to the rest of the page. In this case could it be correct to die silently?
You should throw on mandatory parameters, if it means your plugin cannot function (but try to keep mandatory parameters to a minimum), and use defaults for everything else.