Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not use var everywhere?

Tags:

var

c#-4.0

Except for non human readable code is there another reason not to use var for every variable in functions? I mean is it performance hit not to use int, SqlCommand, string but use var instead?

like image 840
Dominating Avatar asked May 10 '11 14:05

Dominating


People also ask

Why should we not use VAR?

The Var Keyword This means that if a variable is defined in a loop or in an if statement it can be accessed outside the block and accidentally redefined leading to a buggy program. As a general rule, you should avoid using the var keyword.

Is using var good practice?

Using var is lazy. While var is certainly easier to type than Dictionary<int,IList> , if the variable isn't named well, you'd never know what it refers to. Using var makes it hard to know what type the underlying variable actually is.

Why is var deprecated?

Because var only offers a duplicate yet limited subset of functionality, this RFC proposes the deprecation of var in favor of public . This alias was left intact for PHP 7. public , protected , and private can also be used to declare static members and constants (coming in PHP 7.1) whereas var cannot.

Do people still use VAR in JavaScript?

For you question directly, var is probably still used for legacy reasons. It is supported in all versions of JavaScript and it would be a bother to change every example on the internet. The only real advantage to var is it's compatibility.


2 Answers

Yes, use var everywhere.

I love var. It's saved me loads of keystrokes. It helps me code in a "use first" fashion, and it makes refactoring more powerful.

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

Even if I knew the type name, it wouldn't help me much if intellisense is broken, because I wouldn't necessarily know what a specific method or property is called just from the type name alone.

Some specifics where var makes things better:

  • Calling a method (without knowing what it returns - especially if it is a generic type)

This one is big. I often don't remember what a method returns, but I know the name of that method. Having to pull 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 what that return type is 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");
  • Refactoring a return type

If I use var, the compiler immediately tells me where that type is being misused (perhaps the new return type doesn't have the same property names).

If I didn't use var, I would just get an error about failure to assign the type. Then, I would have to go change that type to the new type (every single place it was called) and then I finally get the warnings where that type is being used incorrectly.

  • Focus on naming variables rather than retyping type names.

var is one of the best things that 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 haven't convinced you, well you have no choice anyway if you want to use:

  • Anonymous types
  • Linq

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

I know it's obscure, but I'll even do this sometimes just so I can always use var and have a consistent look to my code:

var number = (int?) null;

Cause I love var.

P.S. I am a little sad that let is replacing var in Typescript/ES6, but Javasctipt var !== C# var

like image 121
Rick Love Avatar answered Sep 17 '22 14:09

Rick Love


There are rare / occasional cases when you are not declaring a variable at the same moment you retrieve a value for it. So var cannot effectively be used in these instances.

Example - calling a method which will act solely based on the type of an object passed to it.

Derived d = null; // there is nothing to infer the type from
var someInfo = GetInfo(d);

...

// Base is an ancestor of Derived    
Info GetInfo(Base b)
{
   if (b is Derived) return ...;
   if (b is Derived2) return ...;
   ...
}

Perhaps alternately you could do:

var d = default(Derived);

or

var d = (Derived)null;

but neither is any more readable IMO. I wouldn't do that just for the sake of using var.

like image 30
StayOnTarget Avatar answered Sep 16 '22 14:09

StayOnTarget