njzxhq
Last Updated: February 25, 2016
·
1.613K
· 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
});

5 Responses
Add your response

2509

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 ·
2515

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 ·
14634

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

over 1 year ago ·
15292
over 1 year ago ·
15297

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 ·