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.

Where to place operators in a multi-line ternary?

See original GitHub issue

My team uses airbnb’s styleguide as our default for coding style, although we do deviate occasionally.

Does airbnb have an opinion on where the operators should go in a multi-line ternary?

It’s not specifically talked about anywhere, but there is the example:

// better
const foo = maybe1 > maybe2
  ? 'bar'
  : maybeNull;

one of our engs prefers this, with the justification that it’s easier to read. If you read the first line by itself, then having the ? at the end lets you know that this is a ternary and is continuing. Otherwise, if you’re skimming the code very fast, you might think that you’re assigning foo a boolean value.

const foo = maybe1 > maybe2 ?
  'bar' :
  maybeNull;

And again, we’re using multi-line ternaries because we have some long expressions and values to be assigned.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:12

github_iconTop GitHub Comments

51reactions
ljharbcommented, Jan 30, 2017

Currently https://github.com/airbnb/javascript#comparison--nested-ternaries does permit multiline ternaries, although it discourages them. I’d strongly encourage you to use let and if/else rather than multiline ternaries.

That said, the former is the only style I would expect to see one:

const foo = condition
  ? a
  : b;
6reactions
rsolomoncommented, Apr 4, 2017

I’d strongly encourage you to use let and if/else rather than multiline ternaries.

Can you elaborate on why you feel that to be the correct approach? Type safety is much easier when you disallow mutations and discourage nullable objects. Ternaries meet both criteria, whereas let often necessitates a host of additional checks.

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 >
Can i use multiple line in ternary operator - Stack Overflow
The ternary conditional operator is an expression. It resolves to a value, which can be used elsewhere. For example: let x = someCondition...
Read more >
Multiple lines in a ternary operator's clauses? - SitePoint
It's very important where you place parenthesis in nested ternary operators. Basically if you want a nest a ternary operator in an existing ......
Read more >
Python Ternary Multiple Lines - Finxter
Ternary Operator : The most basic ternary operator x if c else y consists of three operands x , c , and y...
Read more >
How to use the ternary operator in C++ - Educative.io
In C++, the ternary operator is used to replace a multiline if-else code with a single line. It's a shorthand if-else statement as...
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