• infinitesunrise@slrpnk.net
    link
    fedilink
    English
    arrow-up
    23
    ·
    10 days ago

    For real though I actually find them incredibly useful for creating clean and readable code. I wish Lua 5.1 had a ternary syntax.

  • 9point6@lemmy.world
    link
    fedilink
    arrow-up
    20
    ·
    edit-2
    10 days 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
    20
    ·
    edit-2
    10 days ago

    Don’t you just love the readability

     a =  a > b ? (b > c ? (a < d ? c : a) : d) : (b < c ? a : d )
    
    • subignition@fedia.io
      link
      fedilink
      arrow-up
      6
      ·
      10 days ago

      this is way more nested ternary operators than I would ever use (which I understand is for the sake of example) but if you rearrange them so that the simplest statements are in the true branches, and use indentation, you can make it at least a little more readable

      a = a <= b ? 
          (b < c ? a : d)
          : b <= c ?
              d
              : (a < d ? c : a);
      
    • kryptonianCodeMonkey@lemmy.world
      link
      fedilink
      arrow-up
      6
      ·
      9 days 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

      • BeigeAgenda@lemmy.ca
        link
        fedilink
        arrow-up
        1
        ·
        7 days ago

        Good point, I have only seen 2. nested ternary operators in the wild, and I am pretty sure the second level was added as a bugfix.

  • PeriodicallyPedantic@lemmy.ca
    link
    fedilink
    arrow-up
    17
    ·
    10 days 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.

      • PeriodicallyPedantic@lemmy.ca
        link
        fedilink
        arrow-up
        6
        ·
        10 days ago

        Hey, when you gotta pick a value from a bunch of options, it’s either if/elseif/else, ternary, switch/case, or a map/dict.

        Ternary generally has the easiest to read format of the options, unless you put it all on one line like a crazy person.

        • guber@lemmy.blahaj.zoneOP
          link
          fedilink
          arrow-up
          3
          ·
          10 days ago

          me personally, i prefer switch case statements for many-value selection, but if ternary works for you, go ham (as long as you don’t happen to be the guy who’s code I keep having to scrub lol)

          • thebestaquaman@lemmy.world
            link
            fedilink
            arrow-up
            3
            ·
            10 days ago

            If there’s more than two branches in the decision tree I’ll default to a if/else or switch/case except if I want to initialise a const to a conditional value, which is one of the places I praise the lord for ternaries.

          • PeriodicallyPedantic@lemmy.ca
            link
            fedilink
            arrow-up
            1
            ·
            edit-2
            9 days ago

            Switch is good if you only need to compare equals when selecting a value.
            Although some languages make it way more powerful, like python match.
            but I generally dislike python despite of this, and I generally dislike switch because the syntax and formatting is just too unlike the rest of the languages.

            Generally I prefer the clear brevity of:

            var foo=
                x>100 ? bar :
                x>50 ? baz :
                x>10 ? qux :
                quux;
            

            Over

            var foo;
            if(x>100) {
                foo=bar;
            } else if(x>50) {
                foo=baz;
            } else if(x>10) {
                foo=qux;
            } else {
                foo=quux;
            }
            

            Which doesn’t really get any better if you remove the optional (but recommended) braces.
            Heck, I even prefer ternary over some variations of switch for equals conditionals, like the one in Java:

            var foo;
            switch(x) {
            case 100:
                foo=bar;
                break;
            case 50:
                foo=baz;
                break;
            case 10:
                foo=qux;
                break;
            default:
                foo=quux;
            }
            

            But some languages do switch better than others (like python as previously mentioned), so there are certainly cases where that’d probably be preferable even to me.

  • groctel@lemmy.world
    link
    fedilink
    arrow-up
    6
    ·
    edit-2
    10 days 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!

    • guber@lemmy.blahaj.zoneOP
      link
      fedilink
      arrow-up
      3
      ·
      10 days ago

      yeah… yikes. c is a beautiful language but thing like these are why macros may be it’s largest blemish. hope that codebase doesn’t keep planes flying!

      • thebestaquaman@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        9 days ago

        For all its faults, I think what makes C beautiful is that it gives you complete freedom do be an absolute idiot.

        Whenever I decide to hack something together with an arcane macro, I feel like an animal being released back into the wild, with the (pre-)compiler yelling “Be free! Explore the mysteries of our incomprehensible world!”