Last Updated: February 25, 2016
·
1.777K
· ivolimmen

Maintainable JavaScript: one argument

When writing a large application in JavaScript it is best to write functions with one argument. The argument, being a configuration object, will make the call to the method self-explainatory and self-documenting.
Example:

// function:
function doSomething(config) {
   var x = config.x || 0; // not defined: will have default
   var y = config.y || 0; // not defined: will have default
}

// call
doSomething({
   x: 15
});

6 Responses
Add your response

The problem with this is that your method definition says nothing about what parameters it takes.

over 1 year ago ·

It still requires the developer to write documentation: I never said it could go without documentation. Point I am trying to make is the self-documentation on function-calls; not the function-definition.

over 1 year ago ·

yeah, I like this, but the good point of having a set of parameters in order is that you're forcing the caller to stick to that and debug is easier.

over 1 year ago ·

This seems like a good idea that, like other good ideas, should never be applied thoughtlessly.

over 1 year ago ·
over 1 year ago ·

The curry John Resig writes about is nice if you write your own (complex) library in Javascript. If you write a very large application in Javascript I would not advise it. I do pass on callback function as parameters. I try to keep my code simple and clean as I do not know what my successor knows (and currying is for the advanced; IMHO).

over 1 year ago ·