What's a readability
What's a readability
What's a readability
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.
Gary should learn about Lazy <T>
I put blame on any of his senior coworkers who didn't use this as a teaching opportunity during PR.
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.
Also, Lazy is thread safe. Gary's code is not.
The problem with Gary is thinking that Singletons are ok to write in the first place.
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.
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.
This is not thread safe.
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; }
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.
What's wrong with this? I don't get it. Perfectly understandable code to me. Can someone explain?
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.
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.
few trick
Is... Its that a fork bomb..? Stored inside a class?
Edit:
Nah I'm a dummy it don't do that at all.
Oh neat. Thank you. I was assuming Singleton was like lambda and it was returning instances of itself.
I didn't read the code very closely.
Looks like there's just a private constructor and a public getter for the instance.
Da fuck?!
Where doe it declare it's a public property? Is it some kind of default now? Everything you write becomes one?
Right there where it says "public". What are you talking about?
Oh, right. I read it all wrong for a moment.
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).
What do you mean? Why does what need to be a public property?
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.
I have no C# experience and both styles look fine to me. /shrug
I have loads of experience with C# and both styles look fine to me. /shrug
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.The question mark makes it nullable
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.
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.
It transforms a strict variable into a nullable variable. I most commonly see it with primitive types.
So, for instance
is an illegal assignment, but
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.
Nullable:
Type?
meansType
ornull
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...
The only thing that's not obvious to me is that
??=
doesn't seem to invokenew 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.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).
🤝
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.
Boiling down multi-line expressions into single line statements has been a trend in Comp-Sci for a while.
That
format has been around for decades.
I generally prefer it to clunky if-statements
When you were forced to learn Java and want to run away seeing C#.