Skip to content
This repository has been archived by the owner on Apr 20, 2019. It is now read-only.

Style rule: Arrow function with concise body #72

Closed
leesei opened this issue Jan 21, 2016 · 13 comments
Closed

Style rule: Arrow function with concise body #72

leesei opened this issue Jan 21, 2016 · 13 comments

Comments

@leesei
Copy link
Contributor

leesei commented Jan 21, 2016

Arrow function will return concise body (single expression without block) by default.
This feature makes inline mapper function really elegant.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Function_body

However these two rule forbids usage of concise body:

  • "Arrow function bodies must be enclosed in curly braces"
  • "Empty line after {"
const array = [1, 2, 3, 4, 5];

// Right

array.map((x) => {

  return x*x;
});

// Wrong

array.map(x => x*x);

EDIT: use term 'concise body', revised Right code

@skeggse
Copy link

skeggse commented Jan 21, 2016

Actually, I revise my position. I'm ambivalent towards this.

@prashaantt
Copy link

I personally dislike non-void implicit returns

@skeggse I was talking not of any personal dislike but of a feature introduced in ES6 to promote brevity and better FP-style code in JS. The "mandatory curly braces in arrow function body" rule is a judgment that that feature shouldn't have been allowed.

@skeggse
Copy link

skeggse commented Jan 21, 2016

That's what I was responding to, yes.

@AdriVanHoudt
Copy link
Contributor

first rule doesn't really matter writing array.map((x) => x*x); isn't the worst
2 and 3 can be solved by allowing one liners but not sure if the maintainers are pro for this

@leesei
Copy link
Contributor Author

leesei commented Jan 21, 2016

That said, the current style also avoided the pitfall of returning undefined upon missing parenthesis around object.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Returning_object_literals

var func = () => {  foo: 1  };               // func() returns `undefined`
var func = () => ({  foo: 1  });             // func() returns `{  foo: 1  }`

@leesei leesei changed the title Style rule: Arrow function declaration Style rule: Arrow function with concise body Jan 21, 2016
@AdriVanHoudt
Copy link
Contributor

That is probably one of the reasons for it, there was an issue relating the es6 styleguide discussion, let me see if I can find it

@AdriVanHoudt
Copy link
Contributor

found it #58

@prashaantt
Copy link

the current style also avoided the pitfall of returning undefined upon missing parenthesis

@leesei I would argue that's the programmer's burden to ensure.

@AdriVanHoudt Thanks for the issue link. It doesn't seem to make it very clear if there was a real consensus on this rule. I would love to have a renewed discussion around it.

@AdriVanHoudt
Copy link
Contributor

@prashaantt we are having it right now :P the other issue was to provide context on what was already being said about the topic

@devinivy
Copy link
Contributor

The conclusion I see in #58 seems to allow for one-liners with implicit returns: #58 (comment)

@prashaantt
Copy link

@AdriVanHoudt Well then, what @devinivy said :)

I'll just repeat myself that disallowing single line implicit returns for violation of an arbitrary style rule diminishes the language. We should revoke that rule.

@leesei
Copy link
Contributor Author

leesei commented Jan 22, 2016

This is the revised guide for arrow function reflecting #58 (comment)

#### Function declaration

  - Declare functions via assignment
  - Arrow function arguments must be enclosed in parentheses
  - Arrow function bodies must be enclosed in curly braces (except for single line [concise body](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Function_body))
  - No function creation in concise body
  - Object returned in concise body must be enclosed in parentheses

"Object returned in concise body must be enclosed in parentheses" is a syntax requirement. I'm not sure if it should be included in style guide.

#71 is updated accordingly.

@leesei
Copy link
Contributor Author

leesei commented Jan 22, 2016

In #58 (comment), "parentheses around object creation allowed ((foo) => ({ bar: foo })) including line breaks in the {}" means that the concise body does not need to be in single line, and the style for object creation should be respected.

Any comment on "single-line-ness"?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants