Here we are almost 2 full years since the var keyword was released to the wild, and only a few short months (I hope) till the next release of the .net framework and still the hatred for the var keyword lives on.
The other day I woke up to find the following rant waiting for me in an IM window:
[21:46] VarHatingDude: God I hate the var keyword
[21:46] VarHatingDude: worst thing ever invented
[21:47] VarHatingDude: deferred type is bullshit
[21:47] VarHatingDude: I want to see my types defined in my class
[21:47] VarHatingDude: before I mouse over them
[21:47] VarHatingDude: I will never use that piece of shit
[21:47] VarHatingDude: it's called lazy programming
At first i just kinda dismissed this rant and moved on. However, something about this made me think I should ponder the rant a bit more. After giving this a bit more thought here is what I have to say to the ‘var hatters’ out there.
- RE: Differed Type: Is not a ‘differed type’, but instead it is an inferred typed variable. What does this mean? (from MSDN) Local variables can be given an inferred "type" of var instead of an explicit type. The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The inferred type may be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library.
What does this mean? Basically I can use var on the left hand side and the compiler will infer its type based on the INITIAL context it was used. Once its type has been inferred by the compiler it cannot be swapped or changed.
- RE: Lazy Programming: I have to say I 100% disagree with this. To me using var is more about clean reading and more concise code. Take the following code
Person person = new Person();
The code above tells me in 3 places i am dealing with a person and this is a bit of overkill. If I use var as below I am not repeating myself over and over again:
var person = new Person();
- Var allows for cleaner refactoring when you follow interface driven development. Many times I have changed the interface that I return (not simply doing a rename) from an object/method and as long as the new interface contains the same signature there is no need to recompile or recode my changes.
If you still hate var after 2+ years of it being out check out the follow blogs for other reasons to like/use it.
But like everything else in programming it all boils down to personal taste at the end of the day. However, if you do not like something that is cool, do your own thing your own way.
08-14-2009 2:41 PM