this post was submitted on 07 Oct 2025
170 points (91.3% liked)

Programmer Humor

26817 readers
2802 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] UnderpantsWeevil@lemmy.world 114 points 4 days ago (9 children)

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 8 points 3 days 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).

[–] grueling_spool@sh.itjust.works 31 points 4 days ago (1 children)

I have no C# experience and both styles look fine to me. /shrug

[–] Venator@lemmy.nz 13 points 3 days ago (1 children)

I have loads of experience with C# and both styles look fine to me. /shrug

[–] Lemminary@lemmy.world 4 points 3 days ago (2 children)

Same here. Also, this syntax is common in modem JavaScript, I don't get the hate.

[–] Venator@lemmy.nz 8 points 3 days ago (1 children)

this syntax is common in modem JavaScript

I think you might have just explained the hate 😅

[–] Lemminary@lemmy.world 3 points 3 days 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.

[–] MaggiWuerze@feddit.org 2 points 3 days ago

yeah, in kotlin as well, except its '?:' instead of '??='

[–] dejected_warp_core@lemmy.world 14 points 4 days ago (6 children)

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.

[–] NewDark 24 points 4 days ago (1 children)

The question mark makes it nullable

[–] fibojoly@sh.itjust.works 9 points 3 days 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 15 points 4 days ago (1 children)

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.

load more comments (1 replies)
[–] UnderpantsWeevil@lemmy.world 6 points 4 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.

[–] sidelove@lemmy.world 2 points 3 days 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.

[–] Ephera@lemmy.ml 4 points 3 days ago (1 children)

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 4 points 3 days ago* (last edited 3 days 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.

[–] F04118F@feddit.nl 4 points 4 days ago

Nullable: Type? means Type or null

[–] MonkderVierte@lemmy.zip 2 points 3 days ago* (last edited 3 days ago)

When you were forced to learn Java and want to run away seeing C#.

[–] andyburke@fedia.io 5 points 4 days ago
load more comments (4 replies)
[–] xep@discuss.online 16 points 3 days ago (1 children)

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

[–] Jankatarch@lemmy.world 5 points 3 days 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.

[–] CrackedLinuxISO@lemmy.dbzer0.com 27 points 4 days ago* (last edited 4 days ago) (2 children)

Gary should learn about Lazy 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 7 points 3 days 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.

[–] GreenMartian@lemmy.dbzer0.com 6 points 3 days ago

Also, Lazy is thread safe. Gary's code is not.

[–] Mozingo@lemmy.world 26 points 4 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 16 points 3 days ago (1 children)

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.

load more comments (1 replies)
[–] Shirasho 13 points 4 days ago (1 children)
[–] KoalaUnknown@lemmy.world 9 points 4 days ago* (last edited 4 days ago) (2 children)

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 2 points 3 days ago (1 children)

Second is more readable.

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

[–] brotundspiele@sh.itjust.works 3 points 3 days ago (1 children)

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.

[–] MonkderVierte@lemmy.zip 2 points 2 days ago

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

Yep. Unicode has lots of useless smileys but is missing some important ones. And some often used (like facepalm) are in hard-to-decipher realistic style instead of the usual round face.

And thanks for the explanation.

load more comments (1 replies)

One day we might be able to create operators with three or maybe even four question marks. Imagine the possibilities!

[–] kubica@fedia.io 5 points 4 days ago
load more comments
view more: next ›