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
});
Written by Ivo Limmen
Related protips
6 Responses
The problem with this is that your method definition says nothing about what parameters it takes.
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.
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.
This seems like a good idea that, like other good ideas, should never be applied thoughtlessly.
What about Currying ?
http://ejohn.org/blog/partial-functions-in-javascript/
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).