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.

alternate ternary formatting

See original GitHub issue

Here is how Prettier currently reformats nested ternaries:

const value = condition1
  ? value1
  : condition2
      ? value2
      : condition3
          ? value3
          : value4;

This isn’t so bad when there are only two ternaries, but I sometimes have many more because I essentially use them as an alternative to a switch statement when each case only gets a single value.

Here is how I prefer to format this case:

const value =
  condition1 ? value1 :
  condition2 ? value2 :
  condition3 ? value3 :
  value4;

I like this because it clearly associates each condition with its corresponding value and the indentation doesn’t increase regardless of the number of ternaries.

Perhaps some like the current formatting because it discourages use of nested ternaries, but I think my example shows that you can have any number without making the code hard to read.

Would you consider adding an option to support this alternate formatting? Maybe something like this? --ternary=condval

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:51
  • Comments:40 (18 by maintainers)

github_iconTop GitHub Comments

13reactions
Jamesernatorcommented, Jul 20, 2017

Ternary style is always controversial, personally I write them more like if-elseif-else chains:

const message =
    i % 3 === 0 && i % 5 === 0 ?
        'fizzbuzz'
    : i % 3 === 0 ?
        'fizz'
    : i % 5 === 0 ?
        'buzz'
    :
        String(i)
11reactions
vjeuxcommented, Apr 9, 2017

This keeps confusing people both inside and outside of Facebook. Here’s a proposal:

If there is a strict chain of ternaries, then write it the following way:

a
  ? b
  : c
  ? d
  : e
  ? f
  : g

We need to make sure that the fully nested ones are expanded, otherwise it looks really weird.

This is not as good looking as

a ? b :
c ? d :
e ? f :
g

but at least it’s compatible with the current way we print ternaries and it doesn’t have issues when both don’t fit in a single line.

What do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

multiline-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 >
Which coding style you use for ternary operator? [closed]
I compared my if-block to its alternative as a ternary statement, and when the ternary had some formatting (parens, spacing, etc), it was...
Read more >
Gwydion Style Guide for Java - Carnegie Mellon University
Nesting the ternary operator should be done sparingly. For array access, there should be no spaces between the array and the open bracket....
Read more >
Conditional (ternary) operator - JavaScript - MDN Web Docs
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?)
Read more >
How to write clean nested ternaries | by Rajesh Naroth - Medium
IMO, nested ternary expressions are compact and a better alternative to using ... if you have Prettier installed, it will automatically format it...
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 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