Style guide for using var in C#3.0

One interesting piece of feedback from the MVPs at the summit was quite how many seemed concerned that the new C# 3.0 keyword var – which allows type inference – would be abused. Specifically many seemed concerned that people would type:

var anExampleString = "I am an example";

instead of

string anExampleString = "I am an example";

The heart of the objection is that this becomes lossy; we are no longer explicit about the type. I am not sure that I agree with the strength of this objection.

var can only be used for local variables so the distance between declaration and usage should always be short. Short enough that you can look back and see the type from the declaration. Having the type on the lhs does not help with this lookup, because you still need to look back to the declaration. Your method body should be short anyway, less than a page. If it is longer you need to refactor to Extract Method to Express Intent; look for comments that describe code blocks as seams to extract out a well-named method. If it short then in a lot of cases, inferring the type should be as simple for compilers as humans.

We do potentially lose some information on numeric types as to how the value is stored and I guess that where people are unsure how literals will be translated there may be a desire to be more explicit, but I suspect that in the majority of cases with primitives and built in types, this is not really an issue. My style guidance right now would be explicit where it is otherwise unclear, but default to var where it is obvious so as to avoid unecessary duplication.

Of course when we call a method and assign its return type to var, it may not be so obvious:

var result = myClass.GiveMeYourState()

In this case it is a lot less clear what the type of result is. Intellisense is obviously a help here as it will tell you what methods and properties the type supports, and you may take the attitude that you don’t care what type it is, only what operations you can carry out on it. Still it might be less obvious to a reader what type you are dealing with. I’m inclined to lean toward not needing to know what type it is for now. Hopefully the capabilities of the object should be obvious from the interaction. I am happy I think to rely on tool support to help me figure out the exact type, even if outside an IDE that is not available. I think that even knowing the type, I cannot guarantee to recall the operations that are valid on the class without browsing it. Recognition is always easier than recall and the productivity benefits from intellisense come from the ability to explore a type’s capabilities not the knowledge around its type. Again I suspect my style guidance is to default to var.

I am embracing change here, but I suspect mileage may vary a lot until folks get used to this paradigm.

This entry was posted in Computers and Internet. Bookmark the permalink.

15 Responses to Style guide for using var in C#3.0

  1. Colin says:

    My inclination at the moment is to use var for anonymous types only (because you can\’t assign it to anything else exception object) and make everything else explicitly typed. Other than anonymous types I can\’t, at present, see any other valid use for var.

  2. Unknown says:

    The second example is a better case for using var, since using var here localises the effects of change – if the type returned by myClass.GiveMeYourState() changes (or the name of the type changes, if it is a class that you have defined), then the calling code does not need to change as a result. Which it would without using "var".

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s