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.

Ternary formatting is a bit unexpected

See original GitHub issue

I’m sure this is intentional, but wanted to point it out/ask. My source:

let foo = (bar === 0) ?
               baz :
               qux;

Which gets reformatted to:

let foo = (bar === 0)
                ? baz
                : qux;

This is a little strange to me, as it seems like the first stanza is the “question” and should keep the ? token. The second and third stanzas are “truthy” answer and “falsy” answer.

let foo = question ?
                truthyAnswer :
                falsyAnswer;

I can see where the current formatting is “prettier” in that the ? and : stack neatly, just wondering if it throws the implication of the syntax a bit.

Thx!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

15reactions
kimjoarcommented, Feb 15, 2017

In my experience having ? and : on the new line improves the readability (as it’s easier to immediately see them while scanning code). This becomes more obvious when you have something more than a single word, e.g. an example with a require:

const { configureStore } = process.env.NODE_ENV === `production` ?
  require(`./configureProdStore`) : // eslint-disable-line import/no-commonjs
  require(`./configureDevStore`); // eslint-disable-line import/no-commonjs

// compared to

const { configureStore } = process.env.NODE_ENV === `production`
  ? require(`./configureProdStore`) // eslint-disable-line import/no-commonjs
  : require(`./configureDevStore`); // eslint-disable-line import/no-commonjs

(In the former it kinda looks like it’s requiring both until I read the code more closely, while in the latter it’s immediately obvious while scanning the code that it’s a ternary)

Or with jsx:

<button
  onClick={e => this.toggleAdvanced(e)}
  className="advancedOptions-button"
>
  {this.state.showAdvanced ?
    <FormattedMessage
      id="allocator-vacate-index.hide-advanced-options"
      defaultMessage="Hide advanced options"
    /> :
    <FormattedMessage
      id="allocator-vacate-index.show-advanced-options"
      defaultMessage="Show advanced options"
    />}
</button>

// compared to

<button
  onClick={e => this.toggleAdvanced(e)}
  className="advancedOptions-button"
>
  {this.state.showAdvanced
    ? <FormattedMessage
        id="allocator-vacate-index.hide-advanced-options"
        defaultMessage="Hide advanced options"
      />
    : <FormattedMessage
        id="allocator-vacate-index.show-advanced-options"
        defaultMessage="Show advanced options"
      />}
</button>

I feel the latter is more obvious when quickly scanning through the code (though, I’m biased as that’s the way I’ve written them for a long time, so might just be because of experience that I scan them quicker).

5reactions
mvolkmanncommented, Feb 18, 2017

Here’s an example of a ternary in my code:

      const prop =
        field === 'DurationOfLast1_2Signal' ? 'durationLast12' :
        field === 'DurationOfLast1_4Signal' ? 'durationLast14' :
        field === 'LeakFault' ? 'leakFault' :
        null;

And here is how Prettier reformats it:

      const prop = field === 'DurationOfLast1_2Signal'
        ? 'durationLast12'
        : field === 'DurationOfLast1_4Signal'
            ? 'durationLast14'
            : field === 'LeakFault'
                ? 'leakFault'
                : null;

I know it’s subjective, but I think my code is more readable. In my real code there are 9 ternaries, so the Prettier indentation gets pretty extreme.

The default indentation is 2 spaces, so why does it switch to 4-space indentation after the first ternary?

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Unexpected type resulting from the ternary operator
The ternary operator requires both result values be the same type, ... to solve the problem with integers encoded in floating point format....
Read more >
Prettier 1.12: Fixes, Features, and Formatting, Oh My!
A bug that has been open for a long time deals with the behavior around printing comments before else . Comments before the...
Read more >
Rethinking the JavaScript ternary operator - James Sinclair
Lots of people treat the ternary operator with suspicion. At first glance, ternaries appear unnecessary. Nothing more than a tool for the ...
Read more >
Ternary operator: break after or before? - Google Groups
The Chromium style guide and clang-format disagree about how to linebreak ... in a CL I reviewed my first impression was pleasant surprise...
Read more >
I shall never give up on forcing ternary statements down my ...
Best piece of advice I had ever heard about coding is, always code for the next person ... But again, formatting probably matters...
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