Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would var be a bad thing?

I've been chatting with my colleagues the other day and heard that their coding standard explicitly forbids them to use the var keyword in C#. They had no idea why it was so and I've always found implicit declaration to be incredibly useful when coding. I've never had any problems finding out what type the variable was (you only hover over the variable in VS and you'll get the type that way).

Does anyone know why it would be a bad idea to use the var keyword in C#?

like image 432
Spoike Avatar asked Feb 13 '09 11:02

Spoike


People also ask

Why VAR should not be used?

VAR could stop bad offside decisions being made and decrease diving and acts of simulation as players will know that these incidents will get looked at again. However, people who would prefer not to have VAR in the game say that it will slow the game down because we will have to wait for a decision to be made.

Is it okay to use var?

var can even hold various objects and will behave properly. As you probably already know, C# has supported the variable type var since version 3.0. Ever since, the debate has raged on: you should always use var; you should never use var.

Why are people still using var?

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. If you are writing for old platforms like .

When should VAR be used C#?

By using “var”, you are giving full control of how a variable will be defined to someone else. You are depending on the C# compiler to determine the datatype of your local variable – not you. You are depending on the code inside the compiler – someone else's code outside of your code to determine your data type.


2 Answers

The writers of the .Net Framework Design Guidelines (awesome book) that came out in November 2008 recommend considering using var when the Type is obvious and unambiguous.

On the other hand, if using var would result in an ambiguity when reading the code, as Anton Gogolev pointed out, then it's better not to use it.

in the book (Annex A), they actually give this example:

var names = new List<string>(); // good usage of var  string source = GetSource(); var tokens = source.Split(' '); // ok; most developers know String.Split  var id = GetId(); // Probably not good; it's not clear what the type of id is 

It's possible that, to ensure that readability is not subjected to the whims of lowly developers, your organisation has decided that you were not worthy of var and banned it.
It's a shame though, it's like having a nice tool at your disposal but keeping it in a locked glass cabinet.

In most cases, using var for simple types actually helps readability and we must not forget that there is also no performance penalty for using var.

like image 126
Renaud Bompuis Avatar answered Oct 19 '22 03:10

Renaud Bompuis


var q = GetQValue(); 

is indeed a bad thing. However,

var persistenceManager = ServiceLocator.Resolve<IPersistenceManager>(); 

is perfectly fine to me.

The bottomline is: use descriptive identifier names and you'll get along just fine.

As a sidenote: I wonder how do they deal with anonymous types when not allowed to use var keyword. Or they don't use them altogether?

like image 29
Anton Gogolev Avatar answered Oct 19 '22 02:10

Anton Gogolev