To var or not to var – c# implicit variable declaration

January 13th, 2011

There is always a lot of talk about coding conventions and best practices, and with C# 3.0 came implicitly typed local variables which enables the var keyword to be used when declaring variables. The variable will in fact be strongly typed. The compiler takes care of that by determining the appropriate type. The question is in which cases should we use var instead of the usual explicit declaration?

As many things, this is somewhat a matter of taste. That is, unless we’re talking about anonymous types (for instance when using LINQ) in which case you have to use var. Generally, there are three different camps regarding this. Some don’t use var unless they have to, some use it only when the type is obvious, and others use it all the time.

Let’s go through the pros and cons:

What are the benefits of using var

  • Dont Repeat Yourself (DRY) – Redundant code should be avoided.
  • Faster creation of code
  • Calls for improved naming of variables – can work as a reminder for descriptive naming
  • Improved readability – in some cases (where the repeated type is long and complex)
  • Less code modification if you later need to change the type
  • Clearer distinction of when you really want to specify the type

The disadvantages of using var

  • Loss of readability – in some cases (where the type isn’t obvious)
  • Changes to the type could introduce bugs which the compiler otherwise would have caught for the developer

The main argument for not using var is usually loss of readability. Let’s take a further look at that. Consider the following:

1
Dictionary<string, List<string>> myDictionary = new Dictionary<string, List<string>>();

Or…

1
var myDictionary = new Dictionary<string, List<string>>();

Which do you find more easy to read? Let’s not forget about intellisense by the way, which you often have to use anyway.

Obviously there are situations where the contrary applies, but repeating complex types seems redundant and I see no benefits doing so. On the other hand, I find it unnecessary to use var for primitive types such as int or string.

As for the compiler not complaining after the type has been changed – by avoiding the use of var in such situations, for instance when declaring a variable in a for-loop declaration, you should be safe anyway. In some cases it’s even safer to use var in for-each loops as it could force you to write an explicit cast.

What about consistency and clear guidelines?
Organisations usually prioritize consistency and simple, clear guidelines over all of the pros and cons listed above, which probably will result in guidelines to advocate one of the two extremes regarding this; never to use implicit variable declarations – or use it everywhere. That means the ideal theory will be ignored.

Conclusion:
In my opinion the var keyword should certainly be used with care, but redundant declarations should be avoided. Therefore it would be preferred to use implicit declarations wherever it’s appropriate, i.e where the type is clear and obvious. Consistency shouldn’t be neglected, but I believe developers in the an organisation would identify the same situations where the type is ambigous.

In the end it’s really about finding a sensible balance. If keeping in mind to use var to avoid repetition and obvious type declarations, the answer is pretty clear of when to var or not… where the type is reasonably obvious.

What are your preferences? Leave your replies in the comments section!


Here are a couple of good/bad examples based on a reasonable usage of implicit variable declaration…

+ Good:
var numbers = new int[] {1, 2, 3, 4};
var stringbuilder = new StringBuilder();
var cars = new List();
var orders = new Dictionary();

+/- OK with either (but prefer explicit declaration):
int pages = 10;
string username = “john”;
var username = “john”;
var order = GetOrder(orderId); // ok if the type is Order, otherwise not
for (var x = 1; x < 10; x++)

– Bad:
var settings = GetInboxSettings(); // not obvious at all
var userId = GetUserId(); // ambigous, is this guid or int?
Dictionary orders = new Dictionary(); // redundant

3 Responses to “To var or not to var – c# implicit variable declaration”

  1. PhilM says:

    Thanks for posting you comments, and agree about your final conclusion about finding the right balance, but I really cannot agree with the benefits your proposing that come with using var.

    > Dont Repeat Yourself (DRY) – Redundant code should be avoided.

    What is DRY about using var. It is one word, and the name of an object is normally one word. Ok, there may be a bit of namespacing, but then you can define these in your use statements.

    > Faster creation of code

    I find it quicker to type Object object = new {tab} with VS intellisense, or even quicker Ob{tab} o{tab} = n{tab} with Resharper. With var, you have to create your own local name, and in fact it most of the cases I have seen in conjunction with var, local naming is worse because it’s not helped. Also if using resharper, you cannot create a strong type method on the fly, with the right object types, if the original starting object is var.

    > Calls for improved naming of variables – can work as a reminder for descriptive naming

    It doesn’t enforce this, so using var does not make a lazy developer a better developer.

    > Improved readability – in some cases (where the repeated type is long and complex)

    I just don’t agree. When a object is created from a method, it may not be explicit to the reader of the code what object is being returned.

    > Less code modification if you later need to change the type

    How often do you actually change objects.

    > Clearer distinction of when you really want to specify the type

    Do understand what you mean.

    Finally, I’m not saying never use it, just use it when it should be used, not everywhere. NB You can still name variables badly if using explicit object names.

    Cheers
    Phil 🙂

    • Thanks for your thoughts, I’ll try to reply each point below.

      > What is DRY about using var. It is one word, and the name of an object is normally one word. Ok, there may be a bit of namespacing, but then you can define these in your use statements.
      Actually, I’m not referring to the namespacing, but the fact that the variable type is written twice in the same row when it’s not necessary. Even one word can be DRY.

      > I find it quicker to type Object object = new {tab} with VS intellisense, or even quicker Ob{tab} o{tab} = n{tab} with Resharper.
      Sure, this depends on wether you use resharper, and your habits while programming. Regarding worse naming when using var, the developer had certainly been sloppy and the naming would most likely be just as bad if they would have used explicit declaration.

      > It doesn’t enforce this, so using var does not make a lazy developer a better developer.
      Absolutely, what I meant was that it can work as a reminder for non-lazy developers.

      > I just don’t agree. When a object is created from a method, it may not be explicit to the reader of the code what object is being returned.
      That is why I wrote “in some cases”, so I fully agree with that. 🙂

      > How often do you actually change objects.
      Not very often, but it happens.

  2. > Bad: Dictionary orders = new Dictionary();

    On the:
    – “Dictionary orders = new Dictionary();” VS “var orders = new Dictionary();”

    From experience I can’t say that either of them is the easier to read, maybe I am just so use to reading code that my yes glance over such “lines” really fast, but at least I can’t feel a difference on those two in terms of “readability”, that goes for limited “generic” nesting as well, and I can only agree with Phil, that when having ReSharper’s intellisense it is sometimes easier to write the declaration in front.

    So I would accept both variations with not problem.

    > Improved readability – in some cases (where the repeated type is long and complex)

    If you have to long and complex types, you might consider to take your refactoring hammer out the box instead of just putting “var” in front of it and “now it is all good”.

Leave a Reply