Bald Bearded Builder
csharp/should-you-be-using-the-var-keyword-in-csharp

Should You be Using the var Keyword in C#?

Choosing among `IEnumerable`, `ICollection`, `IList`, and `IQueryable` depends on whether data needs iteration, mutation, positional access, or remote query processing.

Loads from YouTube when you press play

Transcript 3 topics

Why var improves readability

0:00

Love VARs or hate VARs? Hopefully you can recognize some of the benefits of using them. Even those that really hate VARs can admit there's something nice about typing VAR instead of say dictionary int of dictionary, GUID, order detail. And even when you're reading it, really long type followed by essentially a really long type can be pretty draining. But other than being a little lazy and saving some key presses, there are some legitimate other benefits.

For instance, if your return variables are all VARs, then if you change the return object of a method, you don't have to go change all those places. You just need to update your method. That's really nice. Some people just like the prettiness of it and I get it. That wall of VAR with variable names is pretty nice to scan over.

And hopefully using VAR makes you more intentional about your naming of variables and methods. And that's not even counting the benefits of pulling link expressions into IQueryables or IEnumerables.

Name variables descriptively

0:59

But there are some things you really need to remember when you're using a VAR. Like I just mentioned, be sure you're naming things descriptively. Single letter variable names, VAR C equals new customer. That's just not okay. This next one's a little bit more of a personal preference, but I like to stay away from result names or results.

It's a little ambiguous. Like if you called var result equals get active orders, are you giving me the active orders or are you telling me how many active orders there are? I would rather see that variable named active orders or active order count or explicitly typed with an int or list. Now admit that is just a personal preference. I just like to be as explicit as implicit can be.

That also means naming your methods well. So if you're gonna return active orders, get active orders is good. But if you're gonna return the count, maybe you want to rename that like get active orders count. Of course, this also relies on using good pull request processes.

Prefer team consistency

2:03

Someone or hopefully everyone needs to be making sure we're following the standards and being as explicit as we can be. Now we're talking about constructing objects. I'm gonna point you to the new, new syntax. It's really short and it really gives you the best of both worlds. But of course, it is dependent on what version of dot net and c sharp you're using.

Now you might be watching this and saying, hey, wait a minute. Aren't you the guy that just told us to never use var? Well, here's my argument. Use it, don't use it. It's really personal preference.

The code at the end of the day is the same. But I will say this, and this is the most important part of either of these two videos. Consistency is greater than preference. If you prefer VAR or you don't, if your team or org has standardized on one or the other, you do what the team does.

That's much more important and it makes it much more readable and accessible for your entire team. Until next time.

Sign in to join in. Reading needs nothing.