Even today there are those of you who doubt the power of the var keyword in C#! Or rather, there are those of you who steadfastly refuse to use it.
I think the var keyword provides a nice symmetry for local variable declarations. No one variable appears more important than another just because it has a type name with more capital letters.
var newPatient = new Patient(); var surgicalProcedure = Procedures.Rhinoplasty; var lengthOfStay = TimeSpan.FromDays(2);
We can also talk about readability, type inference - blah blah blah. But, I think the following generalization is the real benefit of the var keyword.
Developers who use the var keyword tend to take greater care in naming their variables.
I think the carefulness is a natural reaction to the lack of type information. I've seen quite a bit of code like this:
Procedure proc = Procedures.Rhinoplasty;
But, rarely do I see abbreviations and shortcuts with var.
var surgicalProcedure = Procedures.Rhinoplasty;
If you are still wary of var, give it a try for the day. I think your variable names will thank you.
Comments
I'm sorry, under NO circumstance is this:
var numberOfEmployees = ...
better than this:
int numberOfEmployees = ...
Pro<CTRL-Space><Enter> surgical<CTRL-Space><Enter>=Pro<CTRL-Space><Enter>.R<CTRL-Space><Enter>
will become
Procedure surgicalProcedure = Procedures.Rhinoplasty
With var, R# won't complete my variable names, that lazy me tends to use p rather Prodecure in this case...
var citiesByState = GetMap();
is more clear than
IDictionary<string, IEnumerable<string>> citiesByState = GetMap()
or even
IDictionary<string, IEnumerable<KeyValuePair<string, int>>> citiesByStateWithSize = GetMap()
The generics get in the way of the meaning
StatesWithSizeCollection citiesByState = GetMap();
Second of all, @anon is correct on all counts. The abuse (not use) of var, leads to lots of problems.
Unless you are in academia, MOST developers see "var" as a shortcut, not a tool. If they were to "lazy" to write out the declaring type, what makes you think they will suddenly be motivated to use quality variable names? Instead, you see "var object = ..." or "var o = ...". If you think this doesn't happen, you are either working for a strict software company or you work in a college. This absolutely happens in the other 95% of the programming world!
Var has a VERY specific purpose for TRUE dynamic typing. If you are using it any other place, it's out of laziness, IMHO.
Also, @Chris - under what circumstance would you change the return type and you "not care" about all the places where it's referenced? I would absolutely, positively want the my code to break if I change the return type so that I can review all the places it was referenced.
Var is the VB6 "variant", all over again!!
GOOD:
var session = new Session();
var surgicalProcedures = session.Load<SurgicalProcedure>();
BAD:
var session = Session.Default;
var surgicalProcedures = session.LoadSurgicalProcedures();
In your example, I would write:
var newPatient = new Patient();
SurgicalProcedure surgicalProcedure = Procedures.Rhinoplasty;
DateTime lengthOfStay = TimeSpan.FromDays(2);
You're tests should catch any untoward effects (although I've NEVER seen a bug in my code due to unwanted polymorphism or other typing error). You are testing your code, right?
Welcome to the 21st century.
That is an extreme case (but extremely plausible). Even in common cases, all you are doing is repeating information that is already written. it is like saying "Greeting Paul Hi Paul!" in english.
* I don't understand the difference between variant and implicit typing
* I like writing code that doesn't do anything.
* I don't believe in refactoring
* I think polymorphism is a silly word
* I don't understand polymorphism
* I think OOP is for nerds
* I don't understand polymorphism
* I don't understand polymorphism
* I don't understand polymorphism
* see above
1) var is perfectly type safe, not using it communicates nothing of vital importance. It is not duck-typing and it is not dynamic (on that note, heaven forbid we even discuss the value of 'dynamic' with people who have yet to accept 'var'). If you simply don't like it fine, but stop pretending it is bad for any reason other than your personal preference (which is an outdated preference at best)
2) Do we really not have bigger fish to fry than whether or not it's ok to use var?
...
var c = a + b;
...
a = c;
Why shouldn't I use var for c? If I change the type of a and b, c's type changes automatically and does not affect the meaning of the code. As long as c's type is the same as that of a and b, the specific type is irrelevant.
If I don't use var, I run the risk of changing the type of a and b but forgetting to change c. Now I'm relying on a compiler warning (hopefully) to catch my mistake. It's better to be in a situation where the mistake is not possible in the first place.
However, if my code somehow made use of, say, Int32.MaxValue, then the code would be affected if its type changed, and I would not use var.
Also, the fact that the return type is unclear is exactly the point sometimes:
var list1 = GetSomeList("foo");
var list2 = GetSomeList("bar");
foreach (var z in list1) {
list2.Add(z);
}
This code does not care what the type of z is, and that is the point. Adding type information would get in the way of the intent and make it more difficult to refactor. The type stored in list1 and list2 does not matter.
Yes, this could be done with a generic class, but you might not have that option, and why write an entire class to iterate over a collection when 3 lines of code will do? The point here is to write change-resistant code, not to create a reusable abstraction.
Also, try looking at static, type-inferenced languages such as Haskell, OCaml, F#, and Scala. They do just fine with all of their "laziness."
Please spend a week or two in a dynamic language (try Ruby). Be sure to really learn it, write something real, with real tests. Maybe two weeks isn't enough. Take a few months.
Then come back to C# and give us your thoughts on the var keyword. Your most likely response: "Ugh, I hate typing var, why can't we just have Option Implicit!"
Also, the amount of FUD/just plain wrong statements in this comment thread is sickening. var is nothing more than a shortcut, it's not dynamic typing, it's not VB's variant. It's a lovely shortcut too. I see no reason to ever *not* use var. Want to know the type? Hover over it the variable.
@Aaron - I'll go one step farther regarding your point on hovering: Sure, go ahead and make the odd variable type explicit where it adds significant clarity. Sure, go ahead and hover over the occasional query result variable to make sure you did produce your desired type. But if someone has a constant need to see the exact types of every one of their variables, their code is already screwed and var is neither the cause or cure.
* I understand that var is not dynamic typing, and it is not equivalent to the infamous VB Variant type. Several seemed to imply that anyone who is opposed to the use of the var keyword -- for ANY reason -- doesn't know the difference, or doesn't understand polymorphism. This simply isn't true.
* Speaking of polymorphism, I would argue that using var for this purpose is obscure at best. There are better and clearer ways to do this in C# (e.g. interfaces and abstract classes). And surely you understand that in the following code...
A1 foo = new A2();
...that A1 and A2 are actually *different things* from a language perspective? Namely, a declared type and an instantiated type?
* I understand that there is a vocal segment of the .NET developer community that is so consumed with Ruby Envy (tm) that they can't see past their own ergonomic keyboards. I get it. I've written Ruby. Ruby is nice. But we are talking about C# here. I don't think that attempting to morph one language with a given paradigm into another completely different one is healthy for the language. You want super-terse syntax and dynamic typing? REALLY hate C#'s verbosity? Then maybe C# isn't for you.
* If you *really* want dynamic typing in C#, then use the dynamic keyword. That way, you are explicitly expressing your intent that the variable in question is going to be flying crazy and loose for the rest of its lifetime.
This is a flame war that is only supported by people who don't understand what "var" is in C#. It's just syntactic sugar, nothing like variant or dynamic. The compiler will translate it to the type returned by the right-side statement.
There isn't a single reason for this discussions to exist :)
int numberOfEmployees = ...
is better than
var numberOfEmployees = ...
The variable name tells me it is a positive whole number. What more do I need to know when reading the code?
Every argument against using var is rooted in the thought process that somehow the type of an object is more important that what an object DOES.
That thought process is the result of a lack of understanding of OOP, period. End of story.
Be offended, of become a better software engineer, the choice is yours.
"It amazes me that this discussion even needs to take place. Why in 2011 are .NET devs still debating whether it is ok to use var?"
My thoughts exactly! Move along, nothing to see here.
Not using var almost feels like the use of Hungarian naming in a different form. If I really need to know the type *that* bad, the tools I use can reveal it quickly enough. If I need to do that though, I have to wonder whether this is a readability failure of the original author.
Var does seem to promote better naming; it does result in fewer check-ins ( a big plus); it's easier to type and DRY.
My vote is yes to time saved writing, time saved reading, time saved scrolling right, and general pleasure of its aesthetic leveling of the "act of assigning something to a variable".
Dictionary<string, object> result = GetResult();
That doesn't become a worse line if it uses var. It doesn't become a better line for using Dictionary<string, object>. A bad variable name is a bad variable name.