Bald Bearded Builder
csharp/stop-using-the-var-keyword

Stop Using the var Keyword!

Explicit C# types can reduce the mental effort of understanding inferred variables, while target-typed constructor syntax offers a concise compromise.

Loads from YouTube when you press play

Transcript 3 topics

Why var hurts readability

0:00

You have to stop using the VAR keyword in .net. And before you blame me in the comments, hear me out for just a minute. There are basically three types of developers who use the VAR keyword in .net. First up, you've got the VAR Now the VAR are those who would use it everywhere. You get a VAR.

You get a VAR. Heck, if they could start off classes with public VAR class, they probably would. Next, I've got the lazy devs. And while I would normally say laziness is a good quality because hey, it makes us want to automate all the things. In this case, it goes too far because they're like, well, if I put the type on one side, I'd can't be expected to type all that again on the other side, My fingers might cramp.

Then you got the it depends developers. They're the ones who like to cite Microsoft's guidance that well, if the type is explicitly understood on the right side of the expression, you should just use var on the left side of the expression.

Prefer explicit types

0:56

Okay. Well listen, I'm gonna cover all of these and why they are wrong. First up, you warriors. All I can tell you is you're animals. That's all I can say.

And for the lazy dabs, let's be real. Are you typing the type on the right side of the expression or are you using IntelliSense or Copilot? Heck, full transparency, you're just typing tab a bunch of times. And for that it depends crowd, let's talk about the Microsoft guidance. The problem with saying if the right side of the expression is clear or obvious is that that's subjective.

What I think is clear and obvious may not be clear and obvious to you and vice versa. So Michael, are you saying we should never use the VAR keyword? Well, kinda. But let me tell you why. First of all, think of your brain like a CPU.

You've got a limited number of cycles before that thing shuts off. Even with an expression this simple, I have to use brainpower to scan across that line and see the variable's name and inferred type. That's compared to this code where without moving my eyes I can see the name and type of that object without moving. There's no computation used to infer the type, it's explicitly stated.

Now you can say that's a really simple example but you can imagine as the complexity of the type increases, so does the number of CPU cycles that I have to use to figure out what it is. So how about we compromise?

Use target-typed new

2:16

Maybe we start using the new constructor syntax as a middle ground. It's really sweet, really short. It makes it really easy for everyone to know what the type of that object is and spend less CPU cycles figuring it out and you lazy devs don't have to type it twice. Win win. I know I'm gonna get a lot of heat for this.

Let me know in the comments what you think and how often you're using the VAR. Until next time.

Sign in to join in. Reading needs nothing.