Stop Using the var Keyword!

preview_player
Показать описание
HI, I'm Michael and in this video, I discuss the var keyword in .NET. Should you be using it? Is it overused? Maybe there's a better way to declare variables.

Don't forget to comment, like and subscribe 🚀

💬 JOIN US ON DISCORD

🧑‍💻 LET'S CONNECT ON SOCIAL

#csharp #dotnet
Рекомендации по теме
Комментарии
Автор

Full transparency: Check out any other video on the channel and you'll see me using var almost exclusively. 😁

This was really just fun to stir the pot. People are VERY passionate about the "styling" of their code.

BaldBeardedBuilder
Автор

I'm actually part of the varriors and from my side I have to say that especially since .NET 8 I'm turning to List<string> list = []; more than var list = new List<string>(); but for me that is literally the only example where I think that makes sense. Since when changing code you have to always change the type on the left hand side if the type on the right hand side changes. Also it feels kind of redundant to me if we have the feature of inferring types. And to touch on the "readability" aspect most IDEs nowadays display the dynamic type to you in some sort of way. So for example in VS with Resharper you are really not seeing var something = "Something" but more var something:string = "Something" so you keep the readability of string something = "Something"; But I understand that this is a highly debated topic and I still wanna say that I enjoyed the video even if I don't agree with the point you are making.
Keep up the good work man💯!

mitar
Автор

I<SubjectPronoun> do<AuxiliaryVerb> not<Adverb> agree<Verb>.

nicholaswestby
Автор

Yes - think of your brain as a CPU with a limited number of cycles. Any of those cycles spent having to change the type declaration when you change the type of object assigned to it, or having to look up exactly what type a method returns are cycles that could be used for far more important things like the overall software architecture and system design. I'm team var.

edandersen
Автор

Focus on the algorithm, not the type. Leave it to the compiler.

MaximCherednik
Автор

What about variables that store the return value from a method that returns a tuple?

TheHomelessPotato
Автор

Maybe brains work different. My counter-point to this is that understanding the variable type is often wasted brain-CPU cycles. Context is everything. What the purpose of the line of code is and what its context in its surrounds is is 99% of reading and understanding code. 2 variables might be strings, but one might be a key and the other might be a UI label. 2 different things that are pretty much as unrelated as an int and a bool. Knowing that they are strings are actually not important at all. Knowing one thing is a key (and how it is used) and the other is a UI label is what matters. And once you understand the code, the type is automatically understood along with it. Maybe for some, seeing the type helps the learning process, but tbh, most junior and mid devs I work with make code changes that break things because they cant fully explain the reasons behind their changes. Take time to understand the code before making changes, and always be in a situation where you can 100% explain & motivate why you are making certain changes to the code base (even if you are wrong, it at the very least needs to make sense to you - and not just "it looks like it works!")

Denzilb
Автор

I find it annoying when reading code where most - if not all - variables are defined as var. Just as you say, I have to use extra time determining what the type is, especially if it's a return type from a method, then I have to use IDE features to show me the type. If it's concrete, then I just know. Mouse-over, there are the properties/methods. Nice.

Plankmeister
Автор

I use var only for anonymous types. I like the ease of var, but I find that using var makes the code less obvious and requires me to be in an IDE to roll over the code to see what type var is. At least with Resharper, I can replace all var with the actual type with one command and I am betting the default Visual Studio install has a similar capability too.

LetYourNerdBeHeard
Автор

Might be an L take but using var makes u get more creative with the variable names plus refactors are easier in some cases not needing to worry everywhere about the return type needing to be changed(locally). I get the points of the video, but it really depends in the end on the team. Var is actually one of the things that i loved about C#, on the left of = i got all my names and on the right i got all the types or the things returning it thus i can focus more on the logic than the types.

turcanuioangeorge
Автор

If you are programming against interfaces why do you need to know the type?

_GrumpyOldPunk
Автор

1:47 using var actually leads to better simpler code that’s easier to reason about, read, and refactor. The type isn’t the important part of the code. It leads to better variable and method names. Also, having to stop and read the type actually stops us from being able to read the code quickly. It slows us down. You shouldn’t need to understand or know the type for the same reason we prefer interfaces. Think of var as an interface.
2:30 the = new() syntax makes it really hard to find occurrences of instantiation.

brnto
Автор

So if VS output the inferred type next to var for you that would solve all the issues, right? The value of knowing the exact type is pretty minimal.

TxMasterG
Автор

much easier to refactor code when vars used. for example you rename a type and don't have to go through and update the places its used as a variable

shyguy
Автор

I’m the lazy dev that uses var but then right clicks on it to change to the explicit type.

csteelecrs
Автор

There is no runtime performance overhead, it's compile time (compiler) overhead which even is highly performant so i don't see a point of arguing to not use var

sanampakuwal
Автор

I dont see how excluding it from the right is a win over excluding the type on the left.

It's not really needed to understand most code. It greatly aids in refactoring, and it's actually less ambiguous for the compiler. The better list is when to not use it.

This isn't even close. Type inference is added to every language for a reason.

DarnDave
Автор

I use var because i hate when my variables aren't aligned in their declaration.

And when i need to do a nullable type i don't initialise i do this:

var foo = null as Bar;

Instead of:
Bar? foo;

WDGKuurama
Автор

var is very useful, when your right side is something, what can be changed with returned type or complex type like tuple or complex generic

oshastitko
Автор

Readability ... If you type each variable it'll look like the fence of the circus

andrasmargittai