Giter Site home page Giter Site logo

Comments (11)

nordfjord avatar nordfjord commented on June 11, 2024 14

You've left out a 4th option.

I'm commenting here because this was the top result when googling "Javascript ternary formatting"

parts = typeof value === "string" 
  ? value.split(" ")
  : [ value ];

from contribute.jquery.org.

mgol avatar mgol commented on June 11, 2024 1

IMHO it should be either the first or the second one (both can co-exist depending on expression length) but I'd avoid the third one.

from contribute.jquery.org.

timmywil avatar timmywil commented on June 11, 2024

@mzgol 👍

from contribute.jquery.org.

ryanneufeld avatar ryanneufeld commented on June 11, 2024

The first two are my preference as well.

Though I wonder about the second. I you need to break an inline ternary like that, perhaps an 'if' statement is a better choice.

from contribute.jquery.org.

timmywil avatar timmywil commented on June 11, 2024

I'd prefer a ternary with line breaks over a large if statement.

from contribute.jquery.org.

gibson042 avatar gibson042 commented on June 11, 2024

@mzgol @timmywil 👍

from contribute.jquery.org.

jzaefferer avatar jzaefferer commented on June 11, 2024

I'll put together a PR for this once the existing ones are merged.

from contribute.jquery.org.

dmethvin avatar dmethvin commented on June 11, 2024

Agreed with above; the rule about when you must break can be governed by the line length enforced by jshint.

from contribute.jquery.org.

scottgonzalez avatar scottgonzalez commented on June 11, 2024

Agreed on first and second, but not the third. Who's managing that project anyway? :-P

from contribute.jquery.org.

NotDaze avatar NotDaze commented on June 11, 2024

I usually prefer the first, but the second is more clear for comments if you're documenting all of it.

from contribute.jquery.org.

Made-of-Clay avatar Made-of-Clay commented on June 11, 2024

Following up a bit late in the conversation, I had a discussion with a co-worker on why to choose @nordfjord's 4th option vs @jzaefferer's 2nd option. It came down to how the author interprets the meaning of ? and : in the expression.

4th Option
My preferred style, the ? and : are viewed as the beginnings of the if and else statements respectively.

parts = typeof value === "string" 
  ? value.split(" ")
  : [ value ];

2nd Option
My co-workers style, the ? and : are viewed as the endings of the condition and if statement respectively, while the ; marks both the end of the else statement and the end of the ternary itself.

parts = typeof value === "string" ?
  value.split(" ") :
  [ value ];

Understanding this now, I am more accepting of either style, despite preferring the 4th option; I like the 4th because the symbols basically introduce the beginning of the differences. That is particularly helpful when the if/else statements become complex or long.

And that's my 2.5-years-after-the-fact note. Happy dev-ing!

from contribute.jquery.org.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.