Skip to content

Where to place operators in a multi-line ternary? #1281

@pconerly

Description

@pconerly

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions