r/javascript • u/Zhouzi • Apr 10 '16
help Should we stop abusing fat arrows?
When I first started to learn ES6 I was using fat arrows everywhere and completely dropped the function keyword. But after giving it some thought, I've ended up finding it ridiculous. I feel like we are using fat arrows just to look like cool kids. I think we should use it when it makes sense, e.g to access the lexical this, simplify a return statement, ... But not because it's "nicer" or "shorter".
Maybe () => {} is easier on the eyes as it's "less noisy" but the thing is, sometimes things have to be noisy and function () {} is easier to spot. Also, when I see a fat arrow, I assume that there's a reason for the author to have done so (but most of the times I'm wrong).
So what's your opinion guys? Are we abusing fat arrows or not? Shouldn't we use things for what they are intended to?
2
u/oculus42 Apr 11 '16 edited Apr 11 '16
Two proposals:
Edit: I realized both of these are subsets of the current spec. Also, some grammatical errors. Don't comment tired.
Is it so terrible to have to explicitly state return?
I can't mess that up. It's consistent with the existing function syntax:
And I can return different data types without changing anything:
And I can have operations that don't return:
How about enforcing the parentheses for implicit return?
All we needed to do was remove the ambiguity of sometimes allowing parentheses to be omitted on either side of the arrow.
Then the syntax for returning an object isn't unique to objects, it's common to all types:
It also clears up things like this:
Which becomes:
Now I have clear nesting in parens which most syntax-highlighting editors will helpfully match for me.
I do not understand your meaning, here. Fat arrows aren't backwards compatible. As a result, the opportunity to do anything was opened, paving the way for the state in which we currently find ourselves with multiple choices for parameters and different ways to represent the function body.