Hi
I see "var" a lot in code. I understand that it can be used when
dealing with "anonymous types" in LINQ (I think).
But most times I see it like the following examples:
XmlDocument doc = new XmlDocument();
var root = doc.CreateElement("Message");
Why "var" here - why not XmlElement?
Or:
(obtaining a single user with LoginName == "admin")
var user = UserList.Single(u => u.LoginName == "admin");
Why "var" here, and not the actual type "User"?
Oh my, that counts as a "flamebait" in this newsgroup
Does using var in these types of situations have any advantages (over
and above typing a few extra characters for the real type name)?
Does it make it harder to read and understand the code if var is used
as above (it does for me at least)?
There are, essentially, 3 schools of thought:
1) Use "var" only when needed (i.e. with anonymous types).
2) Use "var" where the actual type is obvious from the initializer
(the definition of "obvious" varies).
3) Use "var" everywhere it's shorter (essentially all locals, with
exception of those used for out/ref arguments, or initialized with a
null or a delegate).
There isn't much to say about #1 - it's pretty obvious what it is
about.
#2 is interesting, and seems to be what most C# developers subscribe
to (at least from my personal anecdotal experience). For example, it
would seem that the following is crystal clear:
var list = new List<int>();
There's no purpose in repeating the type on the same line of code
twice - it really doesn't help to read code. The same goes for casts:
var form = (Form)button.Parent;
However, the following are less obvious - some consider them
appropriate, some do not:
var foo = FooFactory.CreateFoo();
var bar = foo.Get<Bar>();
I used to be in #2 camp, but have gravitated to #3 as time went by...
I have my reasons for that, but I can't help thinking that perhaps the
underlying motive is still laziness, and the rest are just post-facto
rationalizations

Nonetheless, here's how the thinking goes...
The major point of both #1 and #2 is that knowledge of types of
expressions and variables matters at all times. I claim that, in
practice, it does not - static typing should serve as an instrument to
help catch typos and non-sensible things (barking cows and mooing
dogs, etc), but it can do so perfectly well with type inference, as
evidenced by many FP languages - ML family and Haskell in particular.
Meanwhile, what the programmer should care about is the meaning - and,
for the code to be truly readable, it should be clear without knowing
the precise types. Descriptive identifiers help much more than
explicit type annotations there, and in most cases, are perfectly
enough to achieve good readability which isn't further improved by
explicit typing. For example:
var employee = People.FindByName("John");
var manager = employee.Manager;
...
In the above code, do you really need to know the types of variables
involved? Or is the intent of the code entirely clear through the
names of variables, members, and methods? I believe the latter is true
- and it will hold true so long as the naming is good; so if it ever
breaks down, and you become confused by "var", that means that
somewhere else your naming is not clear enough, and _that_ should be
improved. Well, or else you have methods that span 2+ screens - at
which point "var" is the least of your problems.
By the way, I wonder how many of people in #2 and especially #1 camps
regard "let" in LINQ queries, which provides no way to specify the
type of the variable explicitly...
