News for hackers. Stuff that matters.
Re:Rust for JavaScript Developers – Functions and Control Flow by bobbylarrybobby on Monday July 06, 2020 @12:45AM (#23744771)
I find ternary operators perfectly readable if you split the parts across lines — they map exactly to the conditions and statements in an if-else group.

    Cond1 // if this
    ? Result1 // return this
    : cond2 // else if this
    ? Result2 // return this
    : cond3 // else if this
    ? Result3 // return this
    : ResultElse // else return this
Re:Rust for JavaScript Developers – Functions and Control Flow by mekkkkkk on Monday July 06, 2020 @06:13AM (#23746297)
I've come to hesistently accept nested ternaries, but I try to have all lines lead with a colon. This kinda mimics a switch statement with returns.

  firstPossible ? thenThis
    : secondPossible ? thenThisOtherThing
    : thirdPossible ? thenThisThirdThing
    : defaultThing
Always fun to see how people treat ternary nesting. They still feel a bit naughty to me. The terseness is sort of unbeatable though.
Re:Rust for JavaScript Developers – Functions and Control Flow by Kkoala on Monday July 06, 2020 @08:48AM (#23747296)
It's funny how people have so many different preferences for ternary formatting. I can't think of any other operator that would have tens of different format prefs.
Re:Rust for JavaScript Developers – Functions and Control Flow by pests on Monday July 06, 2020 @02:11AM (#23745165)
Which is not true in PHP up until the most recent version (iirc) due to it being left associative instead of right associative you are assuming.
Re:Rust for JavaScript Developers – Functions and Control Flow by amenod on Monday July 06, 2020 @02:11AM (#23745167)
True, or even:

    Cond1 // if this
      ? Result1 // return this
      : cond2 // else if this
        ? Result2 // return this
        : cond3 // else if this
          ? Result3 // return this
          : ResultElse // else return this
Re:Rust for JavaScript Developers – Functions and Control Flow by giantDinosaur on Monday July 06, 2020 @05:02AM (#23745964)
Goodness me that has potential to become something truly hideous.
Re:Rust for JavaScript Developers – Functions and Control Flow by amenod on Monday July 06, 2020 @01:11PM (#23749998)
It's actually not that bad in practice... But I guess the pragmatic formatting is "whatever `prettier` does by default" anyway. :)
Re:Rust for JavaScript Developers – Functions and Control Flow by Kkoala on Monday July 06, 2020 @08:46AM (#23747285)
It's funny how people have so many different preferences for ternary formatting. I can't think of any other operator that would have tens of different format prefs.