Original article :-
http://www.infoq.com/news/2008/05/CSharp-var
C# 3 added the keyword "var". This allows for local type inference when the compiler can unequivocally determine what type the variable should be. There is, however, some debate as to when it should be used.
Ilya Ryzhenkov from the IDE tool company JetBrains outlines some benefits for using var,
It induces better naming for local variables.
It induces better API.
It induces variable initialization.
It removes code noise
It doesn't require the using directive.
Dare Obasanjo of RSS Bandit does not agree. He wrote a response to Ryzhenkov's position after seeing what he thought of as detrimental changes to his open source project. He counters with,
It's interesting how not only are almost all of these "benefits" mainly stylistic but how they contradict each other. For example, the claim that it leads to "better naming for local variables" really means it compels developers to use LONGER HUNGARIAN STYLE VARIABLE NAMES. Funny enough, these long variable names add more noise to the code overall since they show up everywhere the variable is used compared to a single type name showing up when the variable is declared. The argument that it leads to "better API" is another variation of this theme since it argues that if you are compelled to use LONGER MORE DESCRIPTIVE PROPERTY NAMES (e.g. XmlNode.XmlNodeName instead of XmlNode.Name) then this is an improvement. Someone should inform the ReSharper folks that encoding type information in variable names sucks, that's why we're using a strongly typed programming language like C# in the first place.
One more thing, the claim that it encourages variable initialization is weird given that the C# compiler already enforces that. More importantly, the common scenario of initializing a variable to null before it is used isn't supported by the var keyword.
Dare backs up his claim with this line from the official C# Language Reference,
Overuse of var can make source code less readable for others. It is recommended to use var only when it is necessary, that is, when the variable will be used to store an anonymous type or a collection of anonymous types.
The complaint that var reduces readability is not shared by everyone. Arnon Rotem-Gal-Oz writes,
As for the code readability claim, I prefer to focus on stronger methods like keeping methods short, meaningful method and variable names and supporting tests (which can actually help you understand how the code behaves...). Not to mention that, if you really really need that, resharper will tell you the type if you put the mouse over the var keyword ;)
Chris Sutton seems to go further and implies that the type really doesn't matter.
Then the suggestion came up that you should only use var when you don’t know the type. Here is where I differ in opinion and usage. Look at the following snippet
var procs = from p in ServiceController.GetServices()
where p.Status == ServiceControllerStatus.Running
select p;
procs.ToList().ForEach(p=> Console.WriteLine(p.ServiceName));
procs is certainly IEnumerable but it doesn’t matter to me. I primarily care that procs is a list and that the individual items in the list have a Property called ServiceName. The underlying type is important to the compiler, but the people that have to read code aren’t compilers right?
Wednesday, July 16, 2008
C# debate : When should we use var ?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment