• bluGill@fedia.io
        link
        fedilink
        arrow-up
        0
        ·
        2 months ago

        With no indication of why we should think such a thing should be. Singletons are sometimes useful, but they are the wrong answer to your problems more often than the right one. Even where they are a good answer to your problem a class should rarely enforce its own singletonness.

        • mozingo@lemmy.world
          link
          fedilink
          English
          arrow-up
          1
          ·
          edit-2
          2 months ago

          Idk. Depends entirely on what kind of code you’re writing. Singletons are extremely useful patterns for video games, for example. There should never be multiple instances of classes that handle things like game state, achievement unlocking, display/resolution managing, etc.

          If they didn’t enforce their own singletonness then you’d have to always launch the game using the exact same startup routines, to ensure that only one of each ever gets instantiated. But game engines typically divide the game into something like “scenes”, which logically divide code and assets to only what’s relevant for that area of the game. When testing a specific level/area/scene, It’s extremely handy to have a singleton class exist in every scene, that acts as that startup routine, so you can start the game from any scene, and when you load in another scene through gameplay, the singleton class itself makes sure no duplicates exist, instead of having some kind of manager that keeps track of all that (which itself would also have to be a singleton, so you’d just be kicking the can down the road).

  • entwine@programming.dev
    link
    fedilink
    arrow-up
    0
    ·
    2 months 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
      0
      ·
      2 months 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
        2 months 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
            ·
            2 months 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
              ·
              2 months 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
                1
                ·
                2 months 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.