• xep@discuss.online
    link
    fedilink
    arrow-up
    12
    ·
    19 hours ago

    The problem with Gary is thinking that Singletons are ok to write in the first place.

    • Jankatarch@lemmy.world
      link
      fedilink
      arrow-up
      3
      ·
      16 hours ago

      I seen people use singletons in C++ for a single function. There was no justifying context either they just didn’t think of putting the function in some namespace.

  • UnderpantsWeevil@lemmy.world
    link
    fedilink
    English
    arrow-up
    110
    arrow-down
    3
    ·
    2 days ago

    When you’re an old-head who recognizes the old style it is easy to read the old style.

    When you’re a new-head who recognizes the new style it is easy to read the new style.

    When you’ve never seen C# before, they’re both gibberish.

    When you’ve got experience with both, it can get a little confusing but you’ll catch on without too much difficulty.

    But its fucking wild to think the left side is more readable than the right side, simply because it is more verbose.

    • expr@programming.dev
      link
      fedilink
      arrow-up
      6
      arrow-down
      2
      ·
      17 hours ago

      It’s all pretty gibberish. It’s just unabated OOP nonsense that doesn’t serve any real purpose and is incredibly difficult to maintain (unrestricted global mutation is never a good idea).

          • Venator@lemmy.nz
            link
            fedilink
            arrow-up
            6
            ·
            21 hours ago

            this syntax is common in modem JavaScript

            I think you might have just explained the hate 😅

            • Lemminary@lemmy.world
              link
              fedilink
              arrow-up
              4
              ·
              20 hours ago

              Sure, but those are people who hate JavaScript for the sake of it. The rational ones hate it for its inconsistencies because the one thing JS gets right is its syntax.

    • dejected_warp_core@lemmy.world
      link
      fedilink
      arrow-up
      14
      arrow-down
      1
      ·
      2 days ago

      Eh, I haven’t touched C# since 2001. I agree that the more verbose style is more explicit, and so more readable. That said, I can figure most of the new style out from context.

      • => is clearly a closure declaration operator, similar to JavaScript.
      • x ??= y is shorthand for “assign y to x if x is not set, and return x” which is kind of nice.

      There must also be some shorthand going on for getter properties being the same as methods w/o an arglist (or even a ()).

      The only part that has me stumped is the unary question-mark operator: private static Singleton? _instance = null; I can think of a half-dozen things that could be, but I cannot decide what it’s doing that the original question-mark-free version isn’t.

      • sidelove@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        19 hours ago

        The only thing that’s not obvious to me is that ??= doesn’t seem to invoke new Singleton() if it’s already defined, essentially short-circuiting. Otherwise I would have to look up the semantics of it if I were worried about that constructor having side effects or doing something heavy.

        • fibojoly@sh.itjust.works
          link
          fedilink
          arrow-up
          8
          ·
          1 day ago

          My favourite operator to go with the question mark is the exclamation mark. I remember it as the “I swear to God I’m not null!” operator.

      • KindaABigDyl@programming.dev
        link
        fedilink
        arrow-up
        14
        ·
        2 days ago

        As others said, it means nullable, but to put it in more intuitive, less-jargony way - it’s a question mark bc you don’t know if the value is actually there or not. It could be a Singleton, but it isn’t until you check if there is a value. Whereas if you have, idk, int a no question mark, then you’re saying you actually have data.

        Essentially with C# 8, they “removed” null and reused the idea of null references in creating what is essentially an Option like in other languages. You either have some data of some type, or none (a null reference, in this case). By default, everything has to be there. Then when you need null, e.g. you may not have something initialized or an operation could fail, you explicitly grab for it. Thus it reduces null pointer bugs. If you don’t need nullability, you can ensure that you don’t accidentally write in an issue. It safety checks statements and parameters.

      • UnderpantsWeevil@lemmy.world
        link
        fedilink
        English
        arrow-up
        5
        ·
        2 days ago

        I agree that the more verbose style is more explicit, and so more readable.

        On its face, its readable. But when you’re dealing with 10,000 lines of code in a file, it becomes this ugly morass of extra nothingness to scroll through.

        The only part that has me stumped is the unary question-mark operator: private static Singleton? _instance = null;

        It transforms a strict variable into a nullable variable. I most commonly see it with primitive types.

        So, for instance

        int myInt = null

        is an illegal assignment, but

        int? myInt = null

        works fine.

        Why does a public class instantiation need this? Idfk. That might be extraneous. But I wouldn’t throw away the whole code block rewrite over that one character.

      • Ephera@lemmy.ml
        link
        fedilink
        English
        arrow-up
        4
        ·
        1 day ago

        I can figure most of the new style out from context.

        • => is clearly a closure declaration operator, similar to JavaScript.
        • x ??= y is shorthand for “assign y to x if x is not set, and return x” which is kind of nice.

        Man, I’ve successfully stayed away from C# for a few years now, but that’s wild to me that the x ??= y operator would be intuitive to you.
        This could’ve easily been two or three operations, without being much more verbose, but actually being somewhat intuitively readable for most devs…

        • dejected_warp_core@lemmy.world
          link
          fedilink
          arrow-up
          4
          ·
          edit-2
          1 day ago

          Well, I did have the older version on the left as a kind of rosetta stone for this. Plus, this kind of “init and/or return” pattern shows up a bunch of places, so it makes sense someone would want a quick version that’s harder to screw up or has fewer side-effects.

          I’ve also spent years investigating better ways to do things through various versions of C++, D, Rust, Go, and TypeScript. After a while, the big-picture patterns start to emerge and you see different camps start to converge on the same kinds of things. Stuff like these weird features start to feel like learning a new slang term for something you’ve felt, but could never say so succinctly.

          In the case of ??= it’s a more formalized Python x = x or y or x = x || y in JavaScript. The catch is that not all languages treat assignments like expressions that can be returned, so you get a clunky return as a separate statement; return (x = x or y) doesn’t always fly. That friction is all over the place, and it’s natural to want a shorthand for this very thing.

          Sure enough, after searching a bit, ??= shows up in JS, PHP, and even Ruby has a version.

          Edit: more context.

      • masterspace@lemmy.ca
        link
        fedilink
        English
        arrow-up
        17
        ·
        2 days ago

        C# 8.0 came out 6 years ago at this point, and it’s syntax is aping that of Javascript’s ES6 update which is 10 years old, which was in turn aping that of earlier functional languages. There are a lot of engineers who have learned how to code using predominantly modern syntax, so the one on the right is “textbook” to them.

        And being textbook isn’t a reason to keep doing something forever. The syntax on the left is overly verbose and leaves more room for unexpected behaviour-changing lines of code. The syntax on the right is concise and scannable in a way that doesn’t require jumping back and forth between lines to follow.

      • UnderpantsWeevil@lemmy.world
        link
        fedilink
        English
        arrow-up
        7
        ·
        edit-2
        2 days ago

        Boiling down multi-line expressions into single line statements has been a trend in Comp-Sci for a while.

        That

        X = IsY ? Y : Z

        format has been around for decades.

        I generally prefer it to clunky if-statements

        • Ephera@lemmy.ml
          link
          fedilink
          English
          arrow-up
          5
          ·
          1 day ago

          I prefer if-expressions where possible. For example, this is valid Rust:

          let x = if is_y {
              y
          } else {
              z
          };
          

          (Can also be on a single line.)

          This is the same syntax as the normal if-statement, except the compiler forces you to add an else-branch, if you want to ‘return’ a value from it.

          Don’t tell anyone, but the ternary operator is when the C designers realized that being purely procedural is cumbersome AF. 🙃
          Unfortunately, they decided that expressions need to look like math, so now JS devs get to write random question marks and colons across many, deeply nested lines of code.

  • CrackedLinuxISO@lemmy.dbzer0.com
    link
    fedilink
    English
    arrow-up
    26
    ·
    edit-2
    2 days ago

    Gary should learn about Lazy<T> and stop reinventing the wheel.

    I put blame on any of his senior coworkers who didn’t use this as a teaching opportunity during PR.

    • dax@feddit.org
      link
      fedilink
      arrow-up
      5
      ·
      16 hours ago

      Or just services.AddSingleton<X>. There’s plenty to criticize in C#, or any language, but this ain’t it. Most of the new language features just make the code shorter without loosing clarity. Really liking newer C# versions.

  • mozingo@lemmy.world
    link
    fedilink
    English
    arrow-up
    27
    ·
    2 days ago

    I don’t get it. I literally write code that looks like both of these all the time. Null coalescing is cool, and very handy sometimes. In this particular case, I’d probably write it more like the left, since it’s more standard and my coworkers would be more familiar with it, but the right obviously does the same thing and I have no issues reading it.

  • WanderingThoughts@europe.pub
    link
    fedilink
    arrow-up
    15
    ·
    1 day ago

    You’ll write whatever is in the guidelines at the place that pays your wage. But I’ll take issue with people trying to be smart and trying to cram a complete function into one overly complicated line.

    • KoalaUnknown@lemmy.worldOP
      link
      fedilink
      arrow-up
      10
      arrow-down
      1
      ·
      edit-2
      2 days ago

      Yes, it’s cut down for the meme.

      In reality, it would probably look more like this:

      
      public class Singleton
      {
        private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton());
      
        private Singleton(){}
      
        public static Singleton Instance
        {
            get
            {
               return lazy.Value;
            }
         }
      }
      
      

      or this:

      
      public class Singleton
      {
         private static readonly Singleton instance = new Singleton();
      
         private Singleton(){}
      
         public static Singleton Instance => instance;
      }
      
      
      • MonkderVierte@lemmy.zip
        link
        fedilink
        arrow-up
        1
        ·
        19 hours ago

        Second is more readable.

        Btw lazy; why can “second” be “zweite” or “Sekunde” depending on context??

        • optional@sh.itjust.works
          link
          fedilink
          arrow-up
          1
          ·
          15 hours ago

          Why can „Bank“ be a 🏦 or a 🛋️? It’s a common feature of many languages that words can have multiple meanings. It’s called Teekesselchen in German, which is funny because Teekesselchen is a Teekesselchen itself: It can either mean „small tea kettle“ or „word with more than one meaning“.

          But more importantly, why is there no emoticon for bench? I had to use a couch instead.

  • marcos@lemmy.world
    link
    fedilink
    arrow-up
    1
    arrow-down
    5
    ·
    2 days ago

    Da fuck?!

    Where doe it declare it’s a public property? Is it some kind of default now? Everything you write becomes one?

    • hdsrob@lemmy.world
      link
      fedilink
      English
      arrow-up
      4
      ·
      2 days ago

      It’s an Expression-Bodied Member (available since C#6). The expression (=>) is just syntactic sugar that the compiler recognizes as a single line property with only a getter (under the hood both versions compile the same).

  • 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
          2 days 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
            ·
            24 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
            2 days 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
              ·
              24 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
                ·
                23 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
                  ·
                  21 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
            ·
            2 days 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
              ·
              23 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
              ·
              2 days 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
                ·
                23 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
              ·
              23 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.

      • bluGill@fedia.io
        link
        fedilink
        arrow-up
        2
        arrow-down
        4
        ·
        2 days 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
          8
          arrow-down
          1
          ·
          edit-2
          2 days 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).