question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New readability rule to format ternary expression

See original GitHub issue

Hi,

I’d like to propose new readability rule for ternary expressions. Valid options should be:

  1. Whole expression in one line:

    int x = booleanExpression ? firstIntValueExpression : secondIntValueExpression;
    
  2. 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:open
  • Created 8 years ago
  • Reactions:17
  • Comments:16 (14 by maintainers)

github_iconTop GitHub Comments

7reactions
julealgoncommented, Jun 29, 2018

Folks, was this abandoned? It sounded like a fair proposal, but for some odd reason discussions abruptly stopped 3 years ago?

3reactions
otac0ncommented, Aug 4, 2015
p == "Cat" ? "Animals" :
p == "Bat" ? "Equipment" :
p == "Fat" ? "Nutrition" : "None"

Should be allowed, since each line is a logical unit.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found