Var Wars – Abuse of the CSharp var

CSharp 3.0 introduced the var keyword for declaring variables without having to explicitly specify the type. This was done for using anonymous types returned from LINQ queries. Now, I am seeing many developers use it all over their code and think its a good thing.

Wasn’t C# developed to enforce strongly typed programming?

Var just makes it harder to infer the type when reading and maintaining code.

Take for example this LINQ Query:

var user = from u in Users where user.Status == UserStatus.Active
select u;
It is hard enough to infer the type from the above query, let alone infer the type from a statement like this:
var index = 1; (Is index an int, long?)
Just because a compiler can infer the type does not mean a human should have to be forced to do it when reading it. If we wrote code just to make compilers happy we should be writing Assembly. Why then use an Object Oriented Language? But since code is written for developers to be able to read and maintain it and not just for compilers to compile it, it should be as descriptive and readable as possible.

Some say it cuts down on repetitive code when used in a statement like this:

var user = new User();

For those who are so obsessed with cutting down characters and lines of code, here is a small piece of advice that will better save you coding time and characters: focus on better design and architecture. If you are so concerned about repetitive code – your energy will be well rewarded. Well designed code is always shorter, cleaner, easier to read and also works better!

Cutting down on type names is like removing the wrapper from a drink. You have to infer what is in it by drinking it. For those who don’t care about their calories go ahead, but for the rest us, please put the wrappers!

The only time you really have to use var is for anonymous types. There is no other way to get an anonymous type. For everything else a type name can and should be used for variable declaration.

So for those var abusers, spare the rest of us some inferring and just declare your variables with a type name.

6 comments:

  1. Bah. Well-named variables negate the name for an explicit type in the declaration. This is especially true for assignments with constructor calls on the right hand side. For numeric types, use the proper suffix on literals (1f, 1l, 5.00m, etc) to help readers infer the type. Using 'var' greatly improves code readability--blocks of variable declarations are more easily identified because the variable names are all left-aligned. If inferring the types is really that burdensome, try using 'var' combined with an explicit cast (var user = (User)Some.Property;). Or just hover the mouse cursor over any variable usage to see its type.

    ReplyDelete
  2. "(var user = (User)Some.Property;)"

    _That_ is retarded.

    "just hover the mouse cursor over any variable usage to see its type"

    So now I have to pick up the mouse to figure stuff out?

    No, var abuse is rife and damn stupid. It doesn't aid readability one bit.

    ReplyDelete
  3. Love the post. I'm fighting a proliferation of its use at the office right now and stumbled on your blog looking for common sense reasoning to what I intuitively felt was a bad idea.

    Thank you!

    ReplyDelete
  4. thank you for sharing, this is a compelling website content interesting

    ReplyDelete