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
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
.
From Department of Declaration Redundancy Department (from Jeff's Coding Horror):
"I use implicit variable typing whenever and wherever it makes my code more concise. Anything that removes redundancy from our code should be aggressively pursued -- up to and including switching languages."
I myself think it is worth taking about, but creating a comprehensive guideline on when to use or not would be overkill.
I have had cases (when I foreach through a Table.Rows collection) when using var resulted in the type being of some base class rather than the actual DataRow type. That is the only time I have had trouble with var.
You may consider Microsoft's opinion to be relevant, since C# is their language:
"However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required."
See MSDN - Implicitly Typed Local Variables (C# Programming Guide), last paragraph.
You should also be aware that var removes the compile-time datatype test on the initial assignment.
var x = "mistake"; // error not found by compiler
int x = "mistake"; // error found
Since most variables are only assigned once, consistent use of var removes almost all datatype tests on variable assignments.
This makes your code vulnerable to accidental changes e.g. those made by merge tools or tired developers.
I wrote a blog article on this topic a few months ago. For me, I use it every where possible and specifically design my APIs around type inference. The basic reasons I use type inference are
http://blogs.msdn.com/jaredpar/archive/2008/09/09/when-to-use-type-inference.aspx
It can hurt readability if it is misused. However completely forbidding it is a bit strange as your colleagues will have a tough time using anonymous types without it.