If only I were

Building Great Software

Clarity with var in C# 3

I was in a class the other day and the instructor introduced the var keyword and then showed us some of the standard fare examples of how to use it.  Then the discussion turned to when to appropriately use var.

An obvious place to use var is when you do a LINQ query and you are selecting a limited set of columns back from the original type:

NorthwindDataContext north = new NorthwindDataContext();
var emps = from e in north.Employees
           where e.City == "London"
           select new { e.FirstName, e.LastName };

There isn’t much choice here since emps is an anonymous type made up of the employee’s FirstName and LastName. The custom or partial projection (everything after the select keyword) forces the result to be an anonymous type.

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:

using System.ServiceProcess;
.
.
.
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<ServiceController> 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?

I’m very glad that the compiler and runtime do what they do with types, but I want the reader’s focus on the simple LINQ query and iterating over the list it to get a property, not on the type. Replacing var with IEnumerable<ServiceController> is harder to read and less useful.

I think carefully picking your focus can improve your code’s clarity.

Update 5/24/2008: Dare Obasanjo has written one of the best posts expressing caution about abusing var. If var genuinely makes it harder to read your code then you should consider replacing it with an actual type.

Advertisements

Written by Chris Sutton

December 13, 2007 at 10:36 am

Posted in Technology

Tagged with , , ,

4 Responses

Subscribe to comments with RSS.

  1. Great points, Chris. “Code readability” is an issue that sometimes gets overlooked as you’re programming.

    Javier Lozano

    December 13, 2007 at 11:02 am

  2. Just a difference in opinion but I find the IEnumerable easier to read because I don’t even have to look at the query to know what the developer intended to return.

    Justin

    December 13, 2007 at 6:04 pm

  3. Yea, dude, you’re not making much sense, if you think that var is more readable that IEnumerable than maybe readability to you means fewer words.

    foobar

    May 13, 2008 at 6:09 pm

  4. @foobar Your comment didn’t explain why you think IEnumerable is more readable than var in this case. Step up and give a real argument.

    Chris Sutton

    May 13, 2008 at 6:50 pm


Comments are closed.

%d bloggers like this: