• entwine@programming.dev
    link
    fedilink
    arrow-up
    7
    arrow-down
    12
    ·
    2 days ago

    Microsoft hasn’t been known for good engineering for… a long time, but this seems like the type of idea an undergrad with zero real world experience might come up with (or I guess AI).

    This is why I avoid corporate languages like this. Swift and Go are also on my “hell no” list.

    • Victor@lemmy.world
      link
      fedilink
      arrow-up
      34
      ·
      2 days ago

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

      • andyburke@fedia.io
        link
        fedilink
        arrow-up
        22
        arrow-down
        1
        ·
        2 days 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
          22
          ·
          edit-2
          1 day 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.

          • Victor@lemmy.world
            link
            fedilink
            arrow-up
            4
            ·
            21 hours 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. 🤷‍♂️

          • LeFrog@discuss.tchncs.de
            link
            fedilink
            arrow-up
            4
            ·
            edit-2
            1 day 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
              1
              ·
              21 hours 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?

              • LeFrog@discuss.tchncs.de
                link
                fedilink
                arrow-up
                1
                ·
                21 hours ago

                This is basically one of the core ideas of Ruby: that you can read it like a story. Once you are used to reading it as “do X unless Y”, you will miss it in other languages. Note that I wrote Y, not Y is true. Because often Y is a statement that has meaning in itself.

                Example:

                # imagine that given_name is some text from user input
                
                # this is of course valid:
                user.name = given_name if !user.is_locked
                
                # but this reads more fluently:
                user.name = given_name unless user.is_locked
                

                Ruby also allows using ? as last character in method names, which is a convention for methods that return either true or false.

                Same goes for ! (the bang operator), that is commonly used to tell developers that there exists a non-bang version of the same method as well. The bang method is often the more strict version (e.g. raises an error instead of returning nil; or having side effects compared to the non-bang version).

                So the above example may be more commonly written like this:

                user.name = given_name unless user.locked?
                

                and the question mark makes you automatically adding is or has while reading: Set the user’s name to the given_name unless the user is locked

                Of course this is all by convention and you may also do it different. But that’s how most Ruby code is written.

                To stay consistent, lots of projects use RuboCop, which warns you on inconsistency based on your project’s settings.

                • Victor@lemmy.world
                  link
                  fedilink
                  arrow-up
                  1
                  ·
                  19 hours ago

                  you will miss it in other languages

                  I honestly don’t like the “unless X” paradigm, because it adds the condition at the end. The ??= operator shows us what will happen much sooner.

                  I guess it’s a matter of personal taste.

      • rtxn@lemmy.world
        link
        fedilink
        arrow-up
        7
        arrow-down
        1
        ·
        edit-2
        2 days 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
            1
            arrow-down
            7
            ·
            1 day 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.

            • Victor@lemmy.world
              link
              fedilink
              arrow-up
              3
              ·
              21 hours ago

              you forgot to return the result.

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

            • masterspace@lemmy.ca
              link
              fedilink
              English
              arrow-up
              19
              ·
              1 day 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
                4
                ·
                21 hours ago

                Exactly. Not adding efficient things because “they will be new” is just silly. We adapt, as programmers. We learned the language a first time, so we know what it means to learn things. Just like in English when you come upon a word you don’t understand, either you understand it by its context, or you just look it up. Simple as that.

            • Lemminary@lemmy.world
              link
              fedilink
              arrow-up
              1
              ·
              21 hours ago

              And that improves readability, how?

              By being used to it. As many have said, if you’re familiar with the syntax you have no problem parsing this.