About no-nested-ternary
See original GitHub issueI sometimes use the following syntax that I find elegant and useful. What do you think about it? And What do you suggest to replace it?
var foo =
condition1 ? a :
condition2 ? b :
condition3 ? c :
d;
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:12
Top Results From Across the Web
no-nested-ternary - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >eslint-plugin-unicorn/no-nested-ternary.md at main - GitHub
Improved version of the no-nested-ternary ESLint rule, which allows cases where the nested ternary is only one level and wrapped in parens.
Read more >Alternative to nested ternary operator in JS - Stack Overflow
I'm trying to find out alternatives to this approach. I really don't want to turn it into a huge if / else statement,...
Read more >disallow nested ternary expressions (no-nested-ternary) - ESLint
Nesting ternary expressions can make code more difficult to understand. ... The no-nested-ternary rule disallows nested ternary expressions.
Read more >Stop using nested ternary operators. Here's why. | by Ben Lmsc
Except in very simple cases, you should discourage the use of nested ternary operators. It makes the code harder to read because, indirectly, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ggomesfe In your example above:
we need to have useless mutation and cannot use
const
that we could use in:which have semantic consequences that are beyond mere style - i.e. the variable is no longer protected from being reassigned - an otherwise promoted property in the spirit of
prefer-const
rule described in https://github.com/airbnb/javascript#variablesTo use your example to assign to a const we would need to either use a closure:
or use a temporary variable:
Both of those workarounds seem to be much less clear than the use of the ternary operator for what it was designed for.
@testerez Your last example is what I like the most. In fact, multiline ternary is what I find much more readable than the if/elses - it’s short, readable, to the point, always evaluates to a single expression, has a very functional programming feel.
In fact, looking at the two examples in your post I find it surprising that the Airbnb style guide which is otherwise very functional-programming-oriented forces me to change the second one to the first one. If anything I would expect it to work the other way around - to recommend the ternary operator if all you have in the if/else blocks are returns.
I would understand if it didn’t allow the ternary operator at all but saying that this is readable:
and this is not:
seems strange to me, especially when compared to the recommended style:
I’d like the linter to tell me that I can simplify the last example with a ternary operator, not that I shouldn’t use the ternary operator at all (unless it’s for a single comparison - in which case it’s fine). Unfortunately the linter doesn’t share my view on this.
@ljharb I agree that clarity is far more important than brevity - I just don’t think that function
c()
above is more clear that functionb()
. This is the only issue that I have with those linter rules so far after using them extensively in production for large Node projects for over a year so don’t get me wrong. I think you’ve done an amazing job, many thanks for that. Now, if you just stopped hating my favorite operator it would be perfect. 😉