Evolving UX Design: Reduced Cognitive Load with an Anti-Dropdown Anti-Modal Form
gist.github.com2 pointsby L8D1 comments
# print all lines given
pall = (lines...) -> console.log line for line in lines
Someone might write the function like that so that it would fit on one line and be easier to call. But as it turns out, there is a lot more than meets the eye. This is the generated code: var pall,
__slice = [].slice;
pall = function() {
var line, lines, _i, _len, _results;
lines = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
_results = [];
for (_i = 0, _len = lines.length; _i < _len; _i++) {
line = lines[_i];
_results.push(console.log(line));
}
return _results;
};
Which in that case, you might as well skip CoffeeScript's for loop and do an ES5 `Array.protocal.map` call, which will do the same thing, and can be optimized a lot further by the engine. # print all
pall = (lines...) -> lines.map (line) -> console.log(line)
Which, in V8, can be shortened down to: # print all
pall = (lines...) -> lines.map console.log
That skips the whole array generation part and is much more concise and up to ES5 standards. But there are still a few more problems with the code. Look at what it generates: var pall,
__slice = [].slice;
pall = function() {
var lines;
lines = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return lines.map(console.log);
};
See a problem? We could skip the entire first two lines by calling `Array.prototype.map` directly on the arguments. But also, we've forgotten that we never needed to 'map' over the array in the first place, because we know that `console.log` will return `undefined`. I just stuck with using map over `.forEach` so it would match CoffeeScript's 'always return something' rule, and it would be ugly to have an empty handing `return` at the end. So instead, we should probably write the function like so: # print all
pall = -> Array::forEach.call arguments, console.log
This is all in all the best of all the other code, because it generates the optimal representation in JS. If you wrote code like what CS generates, then you could see and make out a ton of flaws before hand, but who dare questions the power of CS and inspects the generated code? But none the less, if you were to write your CS like that, there would be no point in CS. Still, if you wanted to cleanest possible code to be generated, you'd need to put one of those annoying empty returns at the end of the function. pall = -> Array::forEach.call arguments, console.log
return