• groctel@lemmy.world
    link
    fedilink
    arrow-up
    7
    ·
    edit-2
    16 hours 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
      ·
      16 hours 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
        1 hour 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!”

  • infinitesunrise@slrpnk.net
    link
    fedilink
    English
    arrow-up
    27
    ·
    21 hours 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.

    • lime!@feddit.nu
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      17 hours ago

      luas operators are all text in order to be readable. more symbols makes code less readable.

      if you want a one line operation that gives a default result, use or: a = b or c is equivalent to if b then a = b else a = c end.

      • KubeRoot@discuss.tchncs.de
        link
        fedilink
        English
        arrow-up
        1
        ·
        11 minutes ago

        The issue with Lua’s and/or in this context is that they don’t work if false or nil are valid values. In a and b or c, if b = false, the result is always c.

        I also love null-related operators like ?? and ?. for this, since they explicitly check for null, letting you handle any non-null values for optional/default values. The syntax can get a bit cursed, like maybeNull?.maybeMethod?.(args) in JS, but I still prefer that to writing out multiple field accesses in an if condition… And arguably the code is only less readable if you aren’t acclimated to it.

        All that said I do really appreciate Lua’s simplicity, as a language that provides tooling to create the features you want instead of building them into the language, though I wish it had some conventional regex instead of its own patterns.

      • infinitesunrise@slrpnk.net
        link
        fedilink
        English
        arrow-up
        2
        ·
        edit-2
        16 hours ago

        OK true, technically speaking it is indeed more readable, I guess I really meant that it takes far longer to read. I do admire Lua’s barebones simplicity. Thank you for the “or” tip, I’ve used it a few times before but I often forget about it.

        • lime!@feddit.nu
          link
          fedilink
          English
          arrow-up
          1
          ·
          8 hours ago

          always remember that code is read more than it is written. complex lines need to be deciphered, simple lines don’t. especially fun with symbols that have nonlocal effects like rusts ?.

    • nimpnin@sopuli.xyz
      link
      fedilink
      arrow-up
      1
      ·
      17 hours ago

      I’ve survived 11 years of programming without ternary operators and prefer to keep it that way

  • BeigeAgenda@lemmy.ca
    link
    fedilink
    arrow-up
    21
    ·
    edit-2
    20 hours 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
      8
      ·
      16 hours 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);
      
  • 9point6@lemmy.world
    link
    fedilink
    arrow-up
    13
    ·
    edit-2
    19 hours ago

    Control structure conditional:

    • verbose
    • boring
    • may result to nothing

    Ternary expression:

    • terse
    • all action
    • always leads to a result
  • PeriodicallyPedantic@lemmy.ca
    link
    fedilink
    arrow-up
    17
    arrow-down
    1
    ·
    20 hours 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
        ·
        17 hours 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
          2
          ·
          17 hours 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
            2
            ·
            17 hours 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.

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

    This is just superior to anything else

    • thebestaquaman@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      6 hours 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
        1
        ·
        3 hours 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
          1
          ·
          edit-2
          1 hour 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
            1
            ·
            5 minutes 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