• Victor@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    1 month ago

    What’s wrong with this? I don’t get it. Perfectly understandable code to me. Can someone explain?

    • rtxn@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      1 month ago

      Looks a lot like more syntax sugar to me, to hide boilerplate code. It’s not necessarily a bad thing, but it can obfuscate the actual meaning of the code for the sake of brevity. What does A ??= B do at a glance, for example?

      It’s not exclusive to C# or “corporate” languages either. Rust has a fuckton of syntax sugar that makes it difficult to read.

        • rtxn@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          1 month ago

          And that improves readability, how? Don’t get me wrong, I’m a big fan of the Elvis operator, but chaining multiple null coalescing assignments into a one-line expression is a chore to decipher.

          By the way, you forgot to return the result.

          • masterspace@lemmy.ca
            link
            fedilink
            English
            arrow-up
            0
            ·
            1 month ago

            And that improves readability, how?

            Because null checks are an extremely common operation to have to do, and this let’s your code read as just the business logic without these constant null checks breaking things up by multiple lines.

            It’s only not readable to you because you’re not used to them. That’s the case for literally every bit of new programming syntax that comes along.

          • Victor@lemmy.world
            link
            fedilink
            arrow-up
            0
            ·
            1 month ago

            you forgot to return the result.

            What result? The result is A being assigned a value. That’s the result.

    • andyburke@fedia.io
      link
      fedilink
      arrow-up
      0
      ·
      1 month ago

      Old programmers shouting at clouds.

      (Old programmer here, I just shout at differently shaped clouds than this one.)

      Edit: I am not sure if the respondents to this comment think I have a horse in this race. I said I don’t and that I shout at different clouds. I am just here answering a question.

      • floofloof@lemmy.ca
        link
        fedilink
        arrow-up
        0
        ·
        edit-2
        1 month ago

        Another old programmer here, and I don’t see the issue. C# gets better with every release and the null coalescing assignment operator is very handy. It also exists in JavaScript.

        • LeFrog@discuss.tchncs.de
          link
          fedilink
          arrow-up
          0
          ·
          edit-2
          1 month ago

          Ruby has it as well:

          a ||= b
          
          # which means
          a = a || b
          # wich is the same as
          a = b if !a
          # which rubyists like to write as
          a = b unless a
          # or as ternary
          a = a ? a : b
          
          • Victor@lemmy.world
            link
            fedilink
            arrow-up
            0
            ·
            1 month ago

            That’s way too many ways of doing the same thing, yuck.

            But you’re saying the idiomatic way is to use unless, rather than the actual operator for this?

        • Victor@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          1 month ago

          I feel like it boils down to understanding that operator. I’m a TypeScript developer by trade so I had no issue understanding this. 🤷‍♂️