Microsoft LINQ Books

Programming Microsoft LINQ & Introducing Microsoft LINQ
Welcome to Microsoft LINQ Books Sign in | Join | Help
in Search

Marco Russo

Local Type Inference - Best Practices

Local type inference allows using var everywhere for local variable declaration. While this is syntactically correct, it is not a good practice, at least in my opinion. I tried to write down what could be a set of best practices using local type inference and I'd like to receive some feedback...

In general, you should use the var keyword when it is unavoidable (like for anonymous types that we will describe later). Conversely, you should use an explicit type when there is no initializer, when a type cannot be inferred (e.g. if the initializer is a lambda expression) or when the inferred type is not the one you want (e.g. too specific).

In any case, you should choose between var and an explicit type using the one that improves the code readability. What is a readable code might be subjective: the following list represents what are practices that in my opinion improve code readability.

  • DO use var for anonymous types
    var c3 = new { Name = "Tom", Age = 31 };
  • DO use var for query expressions
    var query = from c in customers where c.Discount > 3 select c;
  • DO use var for complex generic types (this is a more general case that includes query BLOCKED EXPRESSION
  • CAN but SHOULD NOT use var for new results in case of known type instance
    var customer = new Customer();
    var numbers = new int[] { 1, 3, 5, 9 };
  • DO NOT use var for constant
    var x = 5; // Bad Practice
  • DO NOT use var for simple expression assignments
    var amount = customer.Amount; // Bad Practice
    var x = y / z; // Bad Practice
    var count = list.Count(); // Bad Practice

What do you think about this? Let me know your opinion, thanks!

Published lunedì 17 settembre 2007 8.36 by Marco.Russo

Comments

 

Claudio Brotto said:

Hi Marco :-)

Here is my personal opinion.

Since type inference is just a compiler feature (with no difference in the generated IL code) I agree with you when you say that the most important perspective to be considered is code readability.

Personally, I would never use type inference unless it is a strict requirement.

The only situation when I would rather choose to use type inference is when dealing with types with very (very very very) long names.

I mean something like:

Some<Extremely<AndAlsoQuite<Unusual<TypeName, Int32>, String, String>>> foo = new Some<Extremely<AndAlsoQuite<Unusual<TypeName, Int32>, String, String>>>(1, 2, 3, 4, 5).

(and I hope I did not miss some angle bracket !).

Interesting issue, though :-)

Ciao

Claudio

settembre 19, 2007 22.08
 

JoeBuddha said:

I take a bit of issue with #4. I always thought is was strange to have to repeat the variable type as in: Customer customer = new Customer();

I find it enhances readability to say: var customer = new Customer();

Not only does it not require repeating the "type", but the variable names line up and are easier to scan.

Mind you, I'm not advocating: var customer = SomeClass.GetMyFavoriteCustomer();

where the type isn't repeated at the end...

febbraio 18, 2008 19.37
 

belgaard said:

Naturally, I agree that the issue we are discussing is code readability - and that topic is inherently subjective in nature.

As JoeBuddha, I would accept

 var customer = new Customer();

since it is obvious that the variable is of type Customer and it is nice to have a little less code to type and maintain.

In fact, I would probably (I have not fully made up my mind yet) also accept,

 var customer = SomeClass.GetMyFavoriteCustomer();

I realise that it is not evident from the code that the variable is of type Customer - if you see it on print or out of your VS context!

I have (finally) stopped printing out code and I always almost look at my code from within VS; and VS know my code and tells me in a nice tooltip what the actual type is. Or is this only with ReSharper 4.0 enabled?

So my point is that since I am looking at my code using tools which know the concrete types, I tend to use var more than Marco, Claudio and JoeBuddha.

See also the opinions on the topic from JetBrain's Ilya Ryzhenkov at http://resharper.blogspot.com/2008/03/varification-using-implicitly-typed.html.

PS: I am not affiliated with JetBrains - I just happen to like the combination of VS and ReSharper:-).

aprile 2, 2008 16.26
Anonymous comments are disabled
Powered by Community Server (Personal Edition), by Telligent Systems