Friday, 4 January 2013

The var keyword in C#. What's in your coding standards?

So this was a subject that led to much debate in the office this week.  When is it acceptable to use the var keyword in C#?  Well, lets start with the obvious situation where everybody will agree it is necessary and we have no choice otherwise.  Anonymous types.  

Take the following example where we have a Linq projection to an anonymous type...

var employeeDetails = from employee in employees
                where employee.County == "Kent" 
                select new { employee.ForeName, employee.Surname };

foreach (var item in employeeDetails)
{
    Console.WriteLine("Name={0} {1}", item.ForeName, item.Surname);
}


var is used twice here.  Once to hold a sequence of anonymous types, then again to iterate through the sequence of anonymous types; but where else is it appropriate for use?  Well actually, most people agreed that it shouldn't be used anywhere else.  It certainly shouldn't be used where it would cause ambiguity, like in the following example... 

var products = _repository.GetProductsByCategory(ProductCategory.Bike);

What is the type of the variable products in the example above?  It's not immediately obvious and requires you to drill down into the method GetProductsByCategory to determine the return type.  Instead, the use of an explicitly typed variable is preferred.  Here is the same example typed correctly. 

IList <IProduct> products = _repository.GetProductsByCategory(ProductCategory .Bike);

Much better.  Also, I don't think var should be used where the type is inferred from the right side of an assigmnet.  i.e...

var age = 31;

Sure, we know the implicit type of the variable age through our own experience, but what about junior members of your team?  Will they know?  Perhaps this was written by a junior.  Did they know the implicit type they would receive when they wrote this?  Were they expecting something else?  Its use here raises too many questions and is best avoided.

I think the biggest split in opinion was around using var when instantiating an object.  To be honest, I've always found C# rather verbose when it comes to this.  For example...

List<Bike> bikes = new List<Bike>();

Isn't there quite a bit of redundancy there?  We explicitly type the variable bikes, then we immediately instantiate that variable, specifying the type a second time.  Personally, I fall into the camp where if the type is specified on the right side of the assignment, use the more concise syntax of...

var bikes = new List<BikeDTO >();

To me, that reads much better.  It's concise and there isn't any ambiguity over the type.  This point however seems to split the room and there seems no way to convince either side otherwise.  I guess this one comes down to what you are used to and what other languages you have had exposure to.  The same verbose declaration in VB for example would look something like...

Dim bikes as List<BikeDTOnew List<BikeDTO>()

However, nobody would ever use that syntax.  Instead, the norm would be to use the conciser form of...

Dim bikes new List<BikeDTO>()

...and move on.

Ultimately, the standard you choose should be consistent and not cause the developer to think too much about it, either at the point of variable declaration or variable discovery.  It's important to settle on something that everybody will buy into and adhere to.  I think it's impossible to please everybody on this one so if permitting its usage for the sake of reducing redundancy is going to cause debate and inconsistency within your code base, perhaps go with the safest option and don't permit it.

What do your coding standards say about the use of var?

No comments:

Post a Comment