Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if statements: force brace or one-liner #16

Closed
dcousens opened this issue Jan 30, 2015 · 16 comments

Comments

@dcousens
Copy link
Member

commented Jan 30, 2015

I find I have to double take when I look at this, to make sure I'm not mis-understanding what is actually part of the expressions that get executed.

The example is the following:

if (condition)
  expression
else
  more expressions
otherstuff

I think that is confusing and unnecessary, and IMHO it should only either be:

if (condition) expression
else more expressions

or

if (condition) {
  expression
} else {
  more expressions
}

edit: Infact, I only ever use the shorthand one-liner for early-exit return statements, but YMMV.

@feross thoughts?

@mafintosh

This comment has been minimized.

Copy link
Contributor

commented Jan 30, 2015

👍

@feross

This comment has been minimized.

Copy link
Member

commented Jan 31, 2015

I'm also 👍 but theres no ESLint or JSCS rule that captures this. The closest I could find is: http://eslint.org/docs/rules/curly.html but it's not quite what we want. Take a look for yourself.

@kytwb

This comment has been minimized.

Copy link

commented Feb 23, 2015

👍 for braces everywhere:

if (condition) {
  expression
} else {
  more expressions
}
@feross

This comment has been minimized.

Copy link
Member

commented Feb 26, 2015

There is now an eslint rule that would enforce @dcousens's original request. curly: [2, "multi-line"], see: http://eslint.org/docs/rules/curly.html

If we add this rule to standard, the following patterns will be considered errors:

if (foo)
  doSomething()
else
  doSomethingElse()

while (foo
  && bar) baz()

if (foo) foo(
  bar,
  baz)

It will not error for these patterns:

if (foo) return; else doSomething()

if (foo) return
else if (bar) baz()
else doSomething()

do something()
while (foo)

if (foo) {
  return
}

if (foo) { return }

while (true) {
  doSomething()
  doSomethingElse()
}

This is definitely a breaking change (it causes many of the modules in test.js to fail), so we'd need to bump the major version and release this as 3.0.0.

@feross

This comment has been minimized.

Copy link
Member

commented Feb 26, 2015

Is this worth doing? cc @mafintosh @othiym23

@dcousens

This comment has been minimized.

Copy link
Member Author

commented Feb 26, 2015

I'm happy with the result of that rule. ACK from me.
On 27 Feb 2015 1:43 am, "Feross Aboukhadijeh" notifications@github.com
wrote:

There is now an eslint rule that would enforce @dcousens
https://github.com/dcousens's original request. curly: [2, "multi-line"],
see: http://eslint.org/docs/rules/curly.html

If we add this rule to standard, the following patterns will be
considered errors:

if (foo)
doSomething()else
doSomethingElse()
while (foo
&& bar) baz()
if (foo) foo(
bar,
baz)

It will not error for these patterns:

if (foo) return; else doSomething()
if (foo) returnelse if (bar) baz()else doSomething()
do something()while (foo)
if (foo) {
return
}
if (foo) { return }
while (true) {
doSomething()
doSomethingElse()
}

This is definitely a breaking change (it causes many of the modules in
test.js to fail), so we'd need to bump the major version and release this
as 3.0.0.


Reply to this email directly or view it on GitHub
#16 (comment).

@othiym23

This comment has been minimized.

Copy link

commented Feb 26, 2015

@feross I'm in favor, but (to set error bars) I feel like I'm unusually particular about this rule.

@yoshuawuyts

This comment has been minimized.

Copy link
Contributor

commented Feb 26, 2015

fwiw, I'm also in favor of this.

@mafintosh

This comment has been minimized.

Copy link
Contributor

commented Feb 26, 2015

👍 from me

@kytwb

This comment has been minimized.

Copy link

commented Feb 27, 2015

Isn't it too permissive?

@dcousens

This comment has been minimized.

Copy link
Member Author

commented Feb 27, 2015

@kytwb what would you see changed?

@kytwb

This comment has been minimized.

Copy link

commented Feb 27, 2015

I'd reject those as well

if (foo) return; else doSomething()

if (foo) return
else if (bar) baz()
else doSomething()

do something()
while (foo)

if (foo) { return }

And force explicit blocks and braces while accepting same line chaining of else/else if/while with previous closing brace:

if (foo) {
  return; 
} else {
  doSomething()
}

if (foo) {
  return
} else if (bar) {
  baz()
} else {
  doSomething()
}

do {
something()
} while (foo)

if (foo) { 
  return 
}
@dcousens

This comment has been minimized.

Copy link
Member Author

commented Feb 27, 2015

@kytwb I personally use if (condition) return as an early exit everywhere in my code, so removing that would be a huge deal breaker for me.

@feross

This comment has been minimized.

Copy link
Member

commented Feb 27, 2015

@kytwb Most users of standard use if (condition) return liberally. It's not practical to forbid it – we have to be pragmatic. I think the succinctness is worth the small risk of errors in this case, since handling an err argument is so common in JavaScript/node.js code: if (err) return cb(err).

@kytwb

This comment has been minimized.

Copy link

commented Mar 2, 2015

Fair enough.

feross added a commit that referenced this issue Mar 7, 2015

new rule: force braces or one-liner
If statements should be:

if (condition) expression
else more expressions

or

if (condition) {
  expression
} else {
  more expressions
}

Fixes #16.
@feross

This comment has been minimized.

Copy link
Member

commented Mar 7, 2015

This rule is added in 3.0.0-alpha. Works great! Please give it a try and update your modules to always use statement braces, except for single-liners.

npm install standard@3.0.0-alpha -g

@feross feross added the v3 label Mar 7, 2015

feross added a commit that referenced this issue Mar 9, 2015

new rule: force braces or one-liner
If statements should be:

if (condition) expression
else more expressions

or

if (condition) {
  expression
} else {
  more expressions
}

Fixes #16.

feross added a commit that referenced this issue Mar 12, 2015

new rule: force braces or one-liner
If statements should be:

if (condition) expression
else more expressions

or

if (condition) {
  expression
} else {
  more expressions
}

Fixes #16.

@feross feross closed this Mar 12, 2015

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
6 participants
You can’t perform that action at this time.