New readability rule to format ternary expression
See original GitHub issueHi,
I’d like to propose new readability rule for ternary expressions. Valid options should be:
-
Whole expression in one line:
int x = booleanExpression ? firstIntValueExpression : secondIntValueExpression;
-
Each subexpression on its own line, with
?
and:
as starting characters:int x = booleanExpression ? firstIntValueExpression : secondIntValueExpression;
Rationale for placement expressions in separate lines:
- consistency with parameters and statement rules - parameters should be in one line or each in separate lines, statements should be each in its own line.
- each expression in its own line allows to easy grasp where it starts and ends.
Rationale for ?
and :
placement:
- when these operators are placed at the beginning of line, one can easy see that rest of the line is part of ternary expression.
- when these operators are placed at the end of line and expressions are long,
?
and:
tokens can go out of view. This makes harder to see that ternary expression is used.
Here is corresponding original SC issue, together with follow up discussion: https://stylecop.codeplex.com/discussions/253622
Issue Analytics
- State:
- Created 8 years ago
- Reactions:17
- Comments:16 (14 by maintainers)
Top Results From Across the Web
multiline-ternary - ESLint - Pluggable JavaScript Linter
This rule enforces or disallows newlines between operands of a ternary expression. Note: The location of the operators is not enforced by this...
Read more >Which coding style you use for ternary operator? [closed]
To the ternary detractors - readability is the point. If you don't think it makes for more readable code, don't use it. But...
Read more >Rethinking the JavaScript ternary operator - James Sinclair
The only reason to use a ternary is to be concise or clever; and; An if-statement would serve just as well in the...
Read more >What do you think about the ternary operator?
The ternary operator has a reputation of reducing readability. However, in the right hands, it may result in less duplication.
Read more >Ternary operators are the worst..unless you indent them right!
Nested ternaries are a coding antipattern that should be avoided, not normalized. It decreases the readability of code in order to compact ...
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
Folks, was this abandoned? It sounded like a fair proposal, but for some odd reason discussions abruptly stopped 3 years ago?
Should be allowed, since each line is a logical unit.