Nested ternary formatting - add indents back
See original GitHub issueEdit: 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:
- Created 5 years ago
- Reactions:769
- Comments:202 (38 by maintainers)
Top 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 >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
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
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.
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:
to this:
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 theelse
for?