I'm starting to use ES6 arrow functions more, but haven't found a coding style that I like, especially when chaining them together. e.g., Eric Elliott gives this code:

mix = (...fns) => x => fns.reduce((acc, fn) => fn(acc), x)

And touts how ES6 lets you write it all on "one line of code", but that's partly due to formatting standards, which would require that the function(x) {...} alternative version to have the { and } on separate lines, etc...

Is there a "coding style standard" (or more likely several) for how that line should be formatted, particularly with newlines?

In my limited research, the Google JavaScript Style Guide and Mozilla Guide are silent.

  • 1
    IMHO such "one liners" should be avoided since they are not particularly readable ...I'm probably one of the many biased. I just hope I'll never have to maintain his code. – dagnelies Jan 4 '18 at 19:49
  • @dagnelies - largely agree with you, that code is an extreme example. But I wonder if better formatting would make it more readable / maintainable. – user949300 Jan 4 '18 at 19:59
  • 2
    Something is wrong when it makes me wish it was written in lisp. – candied_orange Jan 4 '18 at 21:05
  • 1
  • 1
    My main problem with that as a "one liner" is the => inside the reduce is not associated with either of the other two =>s. Spliting that out into const call = (arg, fn) => fn(arg); const pipe = (...fns) => x => fns.reduce(call, x) is much more palatable. – Caleth Jan 5 '18 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.