3
\$\begingroup\$

I was using foo(bar) as it's adopted for functional programming.

console.log(
    join(
        map(function(row){ return row.join(" "); },
            tablify(
                map(function(text){return align(text},
                    view),
                20))),
    "\n");

Now, with dot operator:

view.map(function(text){return align(text)})
    .tablify(20)
    .map(function(row){return row.join(" ");})
    .join("\n")
    .log();

I guess everyone will agree this reads too much better, and the only cost is that you have to modify the native types prototype. So, which?

\$\endgroup\$
11
  • \$\begingroup\$ This will help you find the right thing... just go to jsperf.com and paste your code there and compare the speed... \$\endgroup\$
    – mikrowelt
    Commented Mar 30, 2013 at 6:50
  • \$\begingroup\$ Who says the OOP approach has to modify native prototypes? jQuery, for instance, doesn't modify native prototypes, but still gives you array-like objects with chainable methods. As for the functional approach, it requires (the way it's written now) a lot of functions to be in the current scope - perhaps even in the global scope. That seems more suspect to me. \$\endgroup\$
    – Flambino
    Commented Mar 30, 2013 at 16:33
  • \$\begingroup\$ @Flambino how else would you do it? If you had it all inside an object your whole code would gain a substantial noise (if you're using them too much, which you should). \$\endgroup\$
    – MaiaVictor
    Commented Mar 30, 2013 at 18:08
  • \$\begingroup\$ @Dokkat Sorry, not sure which approach you're referring to there \$\endgroup\$
    – Flambino
    Commented Mar 30, 2013 at 20:18
  • 1
    \$\begingroup\$ @Dokkat Nothing, if you can control it. But in JS, that's sort of impossible. There's no telling what other libraries might define, what certain browsers might define, what users' own extensions and little helper scripts might define. Maybe someone missed a var keyword in function, and made an implicit global that overwrites your stuff - or vice-versa. Maybe a new browser version will overwrite something with new native functions. The global scope is already crowded; namespacing your stuff limits your exposure to those issues. Which is why it's considered best practice to do so. \$\endgroup\$
    – Flambino
    Commented Mar 31, 2013 at 15:16

1 Answer 1

2
\$\begingroup\$

I think that the first variant can look nice, too, if you change the way you format the code and you always use the function as the last argument:

log(map(tablify(map(view, function(text) {
  return align(text)
}), 20), function(row) {
  return row.join(" ")
}).join("\n"))

Edit: Or if you really like indent:

log(map(tablify(map(view,
                    function(text) { return align(text) }
                ),
                20
        ),
        function(row) { return row.join(" ") }
    ).join("\n")
)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.