Skip to content

Instantly share code, notes, and snippets.

@getify
Created June 29, 2017 19:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getify/ec553873f79dd9ae8a5882aebcf2f4d5 to your computer and use it in GitHub Desktop.
Save getify/ec553873f79dd9ae8a5882aebcf2f4d5 to your computer and use it in GitHub Desktop.
arrow functions readability fun
var f = (x,y) => z => x + y + z
// POP QUIZ: how do you use `f(..)` here?
// (x,y) is the parameter inputs to `f(..)`. But what does `f(..)` return?
// To my eyes, on a first left-to-right read, calling `f(..)` returns a `z` value.
// But that's not the case, because `z` isn't a value to return, it's the parameter to another function!
// So calling `f(..)` returns this whole function `z => x + y + z`.
// You can't figure that out from looking only at `z` though. You have to look-ahead to see that
// `z` is `z => ..`, which is another function.
// ANSWER:
f(1,2)(3); // 6
@marianban
Copy link

marianban commented Jun 29, 2017

is it more readable this way?

var f = (x,y) => (z => x + y + z)

@getify
Copy link
Author

getify commented Jun 29, 2017

@marianban Good question. Yes, in general. But then, the general appeal of using arrow functions is to remove all that "unnecessary" punctuation and be as terse as possible.

Let me ask this, which is more readable to you?

a = b * c + d;

a = (b * c) + d;

They both do the exact same thing, so the ( .. ) in the latter one is "unnecessary". But it clarifies a detail not everyone fully gets, which is operator precedence. I think unquestionably the second is more readable, but many (who also like terse arrow functions) would I imagine argue that the former one is more "readable" since it has less to read.


EDIT: Also, I think something which could harm readability in your suggestion, a tiny bit anyway, is that when an arrow-function devotee sees a => followed immediately by a (, I think they're probably trained by now to think "A parameter list, I better go find the closing ). In this case, their eyes might scan rightward to the ...z) part, and think at first that it's a parameter list without a body, before realizing the whole (..) thing was the function. Shrugs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment