• four@lemmy.zip
    link
    fedilink
    English
    arrow-up
    0
    ·
    5 months ago
    x = if y > 5 { "foo" } else { "bar" }
    

    This is just superior to anything else

    • thebestaquaman@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      5 months ago

      I honestly can’t see how this is more readable than

      x = (y > 5) ? "foo" : "bar"

      I get that it’s a syntax that needs to be learned, but it’s just so clean and concise!

      • four@lemmy.zip
        link
        fedilink
        English
        arrow-up
        0
        ·
        5 months ago

        What I like about using if and else for that is that you’re already using those keywords for branching in other parts of the code.

        Though my least favorite is probably Python’s:

        x = "foo" if y > 5 else "bar"
        

        It just seems backwards to me

        • thebestaquaman@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          5 months ago

          While Python’s version does feel a bit backwards, it’s at least consistent with how list comprehensions are set up. They can also feel a bit “backwards” imo, especially when they include conditionals.

          • four@lemmy.zip
            link
            fedilink
            English
            arrow-up
            0
            ·
            5 months ago

            List comprehension is another thing I don’t like about Python :)

            There’s more of those, but one thing I do like about Python is that I get paid for writing it, so I try not to complain too much

  • groctel@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    5 months ago

    At my previous workplace we had a C macro that was something like

    #define CheckWhatever(x__, true__, false__) \
        whatever(x) ? (true__) : (false__)
    

    I don’t remember this shit, so I’m just paraphrasing cursed C. The question one would ask is… why? Well, because you also want to do

    #define CheckWhatever2(x__, true__, false__) \
        CheckWhatever((x__ ##1), (true__), (false__)) \
        CheckWhatever((x__ ##2), (true__), (false__))
    

    And, of course

    #define CheckWhatever3(x__, true__, false__) \
        CheckWhatever2((x__ ##1), (true__), (false__)) \
        CheckWhatever2((x__ ##2), (true__), (false__))
    

    Long story short, someone wanted to CheckWhatever6 inside another macro. While debugging code old enough to vote, my editor suggested expanding the macro, which expanded to ~1400 lines for a single ternary operator chain. Fun times!

  • PeriodicallyPedantic@lemmy.ca
    link
    fedilink
    arrow-up
    0
    ·
    5 months ago

    Bah

    Ternary is just a compressed if-elseif-else chain with a guaranteed assignment.
    If you format it like a sane person, or like you would an if/else chain, then it’s way easier to read than if/else chains.

  • 9point6@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    5 months ago

    Control structure conditional:

    • verbose
    • boring
    • may result to nothing

    Ternary expression:

    • terse
    • all action
    • always leads to a result
  • BeigeAgenda@lemmy.ca
    link
    fedilink
    arrow-up
    0
    ·
    5 months ago

    Don’t you just love the readability

     a =  a > b ? (b > c ? (a < d ? c : a) : d) : (b < c ? a : d )
    
    • kryptonianCodeMonkey@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      5 months ago

      Weird example. 3 nested conditionals is not the typical use case for a ternary, and 2 of the 5 branches result in a pointless a=a assignment. I agree this is bad code, but it’s just as bad and hard to parss in a normal if-else structure too:

      if (a>b) {
          if (b>c) {
              if (a<d) {
                  a=c;
              }
              else {
                  a=a;
              }
          }
          else {
              a=d;
          }
      }
      else {
          if (b<c) {
              a=a;
          }
          else {
              a=d;
          }
      }
      

      In another situation, though, it’s perfectly readable to have a much more typical ternary use case like:

      a = c > d ? c : d

      And a pair of parentheses never hurt readability either:

      a = (c > d) ? c : d