Open Menu
AllLocalCommunitiesAbout
lotide
AllLocalCommunitiesAbout
Login

proportional reaction

⁨159⁩ ⁨likes⁩

Submitted ⁨⁨2⁩ ⁨days⁩ ago⁩ by ⁨guber@lemmy.blahaj.zone⁩ to ⁨[deleted]⁩

https://lemmy.blahaj.zone/pictrs/image/4162679e-4107-4971-9740-1c5b5cd9adaf.webp

source

Comments

Sort:hotnewtop
  • infinitesunrise@slrpnk.net ⁨2⁩ ⁨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.

    source
    • p_consti@lemmy.world ⁨2⁩ ⁨days⁩ ago

      Ternary, and inline switch (match expressions), as found in functional languages

      source
      • infinitesunrise@slrpnk.net ⁨1⁩ ⁨day⁩ ago

        Oh god yea, replicating switch functionality with a huge column of elifs is so gross.

        source
    • nimpnin@sopuli.xyz ⁨1⁩ ⁨day⁩ ago

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

      source
    • lime@feddit.nu ⁨1⁩ ⁨day⁩ 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: local a = b or c is equivalent to if b then a = b else a = c end.

      source
      • kuberoot@discuss.tchncs.de ⁨1⁩ ⁨day⁩ 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.

        source
        • -> View More Comments
      • infinitesunrise@slrpnk.net ⁨1⁩ ⁨day⁩ 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.

        source
        • -> View More Comments
  • 9point6@lemmy.world ⁨1⁩ ⁨day⁩ ago

    Control structure conditional:

    • verbose
    • boring
    • may result to nothing

    Ternary expression:

    • terse
    • all action
    • always leads to a result
    source
  • BeigeAgenda@lemmy.ca ⁨1⁩ ⁨day⁩ ago

    Don’t you just love the readability

    a = a > b ? (b > c ? (a < d ? c : a) : d) : (b < c ? a : d )

    source
    • kryptonianCodeMonkey@lemmy.world ⁨1⁩ ⁨day⁩ 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

      source
    • subignition@fedia.io ⁨1⁩ ⁨day⁩ 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);
      
      source
  • PeriodicallyPedantic@lemmy.ca ⁨1⁩ ⁨day⁩ 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.

    source
    • guber@lemmy.blahaj.zone ⁨1⁩ ⁨day⁩ ago

      if else chain? believe of or not, straight to jail.

      source
      • PeriodicallyPedantic@lemmy.ca ⁨1⁩ ⁨day⁩ 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.

        source
        • -> View More Comments
  • four@lemmy.zip ⁨1⁩ ⁨day⁩ ago
    x = if y > 5 { "foo" } else { "bar" }
    

    This is just superior to anything else

    source
    • kryptonianCodeMonkey@lemmy.world ⁨1⁩ ⁨day⁩ ago

      In what language is that valid syntax?

      source
      • four@lemmy.zip ⁨19⁩ ⁨hours⁩ ago

        This is Rust syntax, but there’s similar syntax in Haskell

        source
      • calcopiritus@lemmy.world ⁨1⁩ ⁨day⁩ ago

        This is valid rust. I don’t know if there are more languages with this feature

        source
    • thebestaquaman@lemmy.world ⁨1⁩ ⁨day⁩ 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!

      source
      • calcopiritus@lemmy.world ⁨1⁩ ⁨day⁩ ago

        Because it can be done for multiple lines too. And you can do else-if too. Also, “if” and “else” is more recognizable than “?” and “:”

        x = if y > 5 {
            println!("Y was over 5");
            z + 5
        } else if y < 0 {
            handle_negative_y(y);
            z - y
        } else {
            println!("<WARN> unexpected value for y"}
            0
        }
        
        source
      • four@lemmy.zip ⁨1⁩ ⁨day⁩ 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

        source
        • -> View More Comments
    • groctel@lemmy.world ⁨1⁩ ⁨day⁩ ago

      Never forget your roots

      (setq x (if (> y 5) :foo :bar))
      
      source
    • humanspiral@lemmy.ca ⁨1⁩ ⁨day⁩ ago

      (‘bar’,:‘foo’)&({~ 5&< )

      source
  • benni@lemmy.world ⁨1⁩ ⁨day⁩ ago

    I love ternary for assigning to constants.

    source
  • groctel@lemmy.world ⁨1⁩ ⁨day⁩ 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__))
    
    source
    • guber@lemmy.blahaj.zone ⁨1⁩ ⁨day⁩ 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!

      source
      • thebestaquaman@lemmy.world ⁨1⁩ ⁨day⁩ ago

        For it’s 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 compiler yelling “Be free! Explore the mysteries of our incomprehensible world!”

        source
      • groctel@lemmy.world ⁨1⁩ ⁨day⁩ ago

        I have bad news for you

        source
        • -> View More Comments
  • UnfortunateShort@lemmy.world ⁨18⁩ ⁨hours⁩ ago

    Easily solved by using Rust and have literally anything evaluate your expression and return whatever

    source
  • Amir@lemmy.ml ⁨1⁩ ⁨day⁩ ago

    All my homies love ternary

    source
  • bluespin@lemmy.world ⁨1⁩ ⁨day⁩ ago

    you would love jsx/tsx with react

    source
  • QuantumTickle@lemmy.zip ⁨1⁩ ⁨day⁩ ago

    “Brought to you by the Go gang”

    source
  • black_flag@lemmy.dbzer0.com ⁨1⁩ ⁨day⁩ ago

    A lot of languages have more intuitive ternary syntax than C

    source