var – Considered harmful
.NET, Spleen Vent May 8th, 2009There seems to be a new phenomenon in the c#.net 3.5 world, where people are using the “var” keyword, because, well I’m guessing they are lazy. Take this example, what is MORE READABLE:
var sourceRect = new ComparisonRectangleContainer();
OR
ComparisonRectangleContainer sourceRect = new ComparisonRectangleContainer();
Now, I know that will be straight forward, you can see what the new type is, it will be a ComparisonRectangleContainer, but what happens when you use var when getting a response from a method, like the following:
var parallelCombiner = FirstPass();
Now, what does FirstPass return? Is it an IList<String>? No, I have to mouseover to find out:
Ok, so it turns out it returns an ImageParallelCombiner, the first bit of code does not make it clear.
You should always write code for HUMANS, not the compiler
The following is PERFECTLY valid c#.net code, but is it good code?
var @this = new @class();
@base.@stackalloc(delegate(@if @event)
{
if (!@return.@bool(@event)) return;
@is.@const(“S[" +@event + "] matched T[" + @return.@override(@event) + "]“ );
@this.@implicit(@event);
});
Note – you can prefix variable names with @ if you want to use a keyword – i.e. @class is a valid variable name, so is _ (underscore) – perfect if you want to make things as hard as possible for people to read it, like the “var” keyword.


Recent Comments