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.

Nested ternary formatting - add indents back

See original GitHub issue

Edit: I will no longer be actively participating in this thread - my reasons are explained in comments below.

Hello,

there are many - including me thinking that change to nested ternary is not best decision.

Eg threads https://github.com/prettier/prettier/issues/5476 https://github.com/prettier/prettier/issues/3018

I know there is some reasoning why nested ternary indent was removed, but it’s much harder to read code now.

At least would be good to have option to remain nice formatting (from previous prettier version) - formatting of ternaries with indent and not under each other like:

condition
  ? a
  : b
  ? c
  : d

instead much easier to understand and read is:

condition
  ? a
  : b
    ? c
    : d

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:769
  • Comments:202 (38 by maintainers)

github_iconTop GitHub Comments

148reactions
gaearoncommented, Mar 1, 2019

The thing that concerns me is that I don’t think it’s a purely stylistic preference. It erases the logical structure where one is implied. Reconstructing it from a flat list of punctuation marks where every “question” goes after : (but every ? means a truthy case) is pretty challenging unless you already know what the code is doing. Ternary is already very terse and frankly I was relying on its nesting structure a lot when reading. So it seems to me like it needs to be more strongly motivated than that it looks cleaner in some cases. I’m all for zero config btw. I just think in this case the cleanliness made comprehension significantly harder, and that makes the cleanliness not worth it to me. YMMV.

I do agree this shouldn’t be configurable. I would personally prefer it be reverted to the old behavior.

104reactions
mjacksoncommented, Mar 1, 2019

Thanks for the link, @suchipi. Looks like there was a fork with a flattenTernaries option. Was that considered before making flattening the default behavior?

FWIW, here’s the largest nested ternary that I’ve ever written, in the React Router codebase. With this change it goes from:

screen shot 2019-03-01 at 12 05 09 pm

to this:

screen shot 2019-03-01 at 12 04 49 pm

The code was already difficult to read, but this change makes it much more so.

Edit: e.g. consider the last trailing null. Which condition is that the else for?

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the accepted formatting for nested ternary-operators?
I strongly believe that the following usage of ternary is the best manifestation of a nested conditional. return foo ? 1 : bar...
Read more >
How to write clean nested ternaries | by Rajesh Naroth - Medium
This format maintains the conditions and return values in the same relative position with a low noise to signal ratio. It takes a...
Read more >
Prettier 1.12: Fixes, Features, and Formatting, Oh My!
When ternaries are nested, depending on the print width and indentation level, sometimes the outer ternary could get broken across multiple ...
Read more >
clang-format: support aligned nested conditionals formatting
Ternary operator do indeed get aligned, thus : the "tests" (on first and third lines) get aligned; the "results" do not fit on...
Read more >
Nested Ternaries </> TL;DR: JavaScript Codecasts
By formatting our ternaries like this, we can easily add and rearrange cases without changing other lines or fretting about nested parentheses!
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