outmoded / hapi-contrib

Discussion forum for project contributors
Other
78 stars 25 forks source link

Style rule: Arrow function with concise body #72

Closed leesei closed 5 years ago

leesei commented 8 years ago

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:

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 commented 8 years ago

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

prashaantt commented 8 years ago

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 commented 8 years ago

That's what I was responding to, yes.

AdriVanHoudt commented 8 years ago

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 commented 8 years ago

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  }`
AdriVanHoudt commented 8 years ago

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 commented 8 years ago

found it https://github.com/hapijs/contrib/issues/58

prashaantt commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

The conclusion I see in #58 seems to allow for one-liners with implicit returns: https://github.com/hapijs/contrib/issues/58#issuecomment-151287951

prashaantt commented 8 years ago

@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 commented 8 years ago

This is the revised guide for arrow function reflecting https://github.com/hapijs/contrib/issues/58#issuecomment-151287951

#### 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 commented 8 years ago

In https://github.com/hapijs/contrib/issues/58#issuecomment-151287951, "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"?