Why not use var everywhere?

With the exception of code that is not human readable, is there another reason not to use var for each variable in functions? I mean, performance forced not to use int, SqlCommand, string, but instead to use var?

+6
source share
4 answers

This is roughly implicit and explicit typing . C # .NET is a typed language, which means that you determine what type of data is stored in memory. If you do not define this, you make some operations less secure, so you want to explicitly specify as much as possible. However, in some cases, the type is really obvious from your code, so you can simply leave it to the compiler to figure out what type the variable should be, which is implicit typing .

The problem with the lack of types is that in memory, which is essentially a bunch of 1 and 0, this data can mean anything, so if you initially entered an integer at location 000001 and then try to read it as Cat (just imagine , that this is some type), then nothing of what you just read from memory will make a big difference. That's why a type system was invented to tell you what kind of data you store there, and to make sure that you read the data back, which can be understood by people again, but for the machine it is not really a question at the end of the day, what is the data.

-9
source

Similarly, using or not using "var" does not change other observable program characteristics, such as its performance. The question is whether or not to use the “hinge” hinges by its effect on readers and code maintainers, and not on its effect on the compiled artifact.

Have a look at this wonderful article: http://blogs.msdn.com/b/ericlippert/archive/2011/04/20/uses-and-misuses-of-implicit-typing.aspx

+15
source

This is definitely not a performance hit. var is actually not a type, but a placeholder in your code, which means "I don't want to write the type of this variable." In fact, if you hover over text in Visual Studio, it will show a tooltip indicating the type you decided to write for too long!

For all its seriousness, however, you should usually use var when it clears that type of code, so that others are not confused when reading.

+7
source

Yes, use var everywhere.

I love var. This saved me from keystrokes. This helps me to code the “use the first” way, and it makes refactoring more powerful.

It doesn't matter which type is named. Intellisense tells me what a type can do, that's all that matters.

Even if I knew the type name, it would not help much if intellisense were broken, because I would not necessarily know which specific method or property is called only from the type name.

Some features in which var improves quality:

  • Method call (not knowing what it returns, especially if it's a generic type)

This one is big. I often don’t remember how a method returns, but I know the name of this method. Getting the return type out of my head slows me down. I just write var , call the method with my inputs, and voila intellisense tells me that this is a return type and what I can do with it.

 // Imagine a method that creates a return type that gets some generic type from the call arguments Tuple<TA,TB,TC,TD,TE> Combine<TA,TB,TC,TD,TE>(TA a, TB b, TC c, TD d, TE e); // GOOD: Call this with var var combo = Combine( "Some text", 42, true, new Dictionary<int, List<string>>(), "Other text"); // BAD: Without var Tuple<string, int, bool, Dictionary<int, List<string>>, string> combo = Combine( "Some text", 42, true, new Dictionary<int, List<string>>(), "Other text"); 
  • Return Type Refactoring

If I use var , the compiler immediately tells me where this type is used incorrectly (perhaps the new return type does not have the same property names).

If I did not use var , I would just get an error when assigning the type. Then I would have to change this type to a new type (each place was called), and then I finally get warnings that this type is being used incorrectly.

  • Focus on the names of the names, not the types of type names.

var is one of the best things that has ever happened to C #.

 // BAD: No var Dictionary<int,List<Tuple<int,bool,string>>> ahhhhThatWasDifficult = new Dictionary<int,List<Tuple<int,bool,string>>>(); // GOOD: With var // I can think of a good name before writing this complex type var validNameCountDictionary = new Dictionary<int,List<Tuple<int,bool,string>>>(); 

If I still have not convinced you, well, you have no choice if you want to use:

  • Anonymous types
  • Linq

So why not go all the way and use var everywhere.

I know this is unclear, but sometimes I do it, so I can always use var and look at my code sequentially:

 var number = (int?) null; 

Because I love var.

PS I'm a little sad that let replaces var with Typescript / ES6, but Javasctipt var ! == C # var

+4
source

Source: https://habr.com/ru/post/887731/


All Articles