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

New rule: no-nested-ternary #647

Closed
qzb opened this issue Oct 3, 2016 · 5 comments

Comments

@qzb
Copy link

commented Oct 3, 2016

Disallow nested ternary expressions.

http://eslint.org/docs/rules/no-nested-ternary

Nested ternary expressions makes code really difficult to understand.

@qzb qzb changed the title New issue: no-nested-ternary New rule: no-nested-ternary Oct 3, 2016

@yoshuawuyts

This comment has been minimized.

Copy link
Contributor

commented Oct 3, 2016

I'm 👎 on this; with proper indentation nested ternaries are fine imo - I don't use them often, but given JS doesn't have macro support sometimes they're the cleanest and most efficient solution for a problem. I'd rather have the option to use them when I need to

@feross

This comment has been minimized.

Copy link
Member

commented Oct 3, 2016

Yeah, I use them too. Haven't run the numbers, but I suspect that this would break a lot of modules and the rule is not automatically fixable.

@feross feross closed this Oct 3, 2016

@dcousens

This comment has been minimized.

Copy link
Member

commented Oct 3, 2016

An example of where I have used nested ternary expressions would be in https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/src/bufferutils.js#L129-L134.

function varIntSize (i) {
  return i < 253 ? 1
  : i < 0x10000 ? 3
  : i < 0x100000000 ? 5
  : 9
}

I can't imagine how the use of a verbose set of if statements in this scenario would increase readability in any way.

@rstacruz

This comment has been minimized.

Copy link
Member

commented Oct 4, 2016

On a devil's advocate note, Standard asks you to do this:

a
? b
: c
  ? d
  : e

so it's a bit more readable:

user.isAdmin()   // if
? 'Manage'  // then
: user.isEditor()  // elseif
  ? 'Edit'   // then
  : 'Unauthorized'   // else
@dcousens

This comment has been minimized.

Copy link
Member

commented Oct 4, 2016

@rstacruz interesting, I find that less readable since in my head, chained ternary expressions are instead more like:

if (a) return b
else if (c) return d
else return e

You wouldn't do

if (a) return b
  else if (c) return d
    else return e

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

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