March 10, 2008
@ 10:51 AM

The new Resharper seems to favor the "var" keyword and I don't understand why.  Is it some performance benefit?  To me, if you know the type, then write it as such.  Consider the following:

Person p = new Person("Tim", "Barcz");

Resharper doesn't like this and suggests the following:

var p = new Person("Tim", "Barcz");

Why?  Just because you can do something, I don't think you should.  Maybe my brain hasn't shifted back to a var world yet, but the second snippet is less readable than the first.  To me it's a case of Don't Make Me Think.  With "var", I now have to stop and think, even for a second, what type is being returned.


 
Monday, March 10, 2008 12:13:55 PM (Central Standard Time, UTC-06:00)
Check this discussion: http://resharper.blogspot.com/2008/03/varification-using-implicitly-typed.html
Ilya Ryzhenkov
Monday, March 10, 2008 12:25:03 PM (Central Standard Time, UTC-06:00)
Amen!!
Anonymous
Monday, March 10, 2008 3:03:28 PM (Central Standard Time, UTC-06:00)
I think it suggests var if the type is immediately evident from the right side of the statement. I get the most benefit from "var" to cut down on the awkward and noisy generic declarations.

Chris
Monday, March 10, 2008 3:06:22 PM (Central Standard Time, UTC-06:00)
@Chris,

So in this case are you saying that you do or don't like the var?
Monday, March 10, 2008 7:41:49 PM (Central Standard Time, UTC-06:00)
I'm leaning toward ReSharper's usage. If it is obvious from the right side of the statement what the type is, I'd generally go with var. I don't see any reason to declare the type twice within one line and since var is only three letters it is nearly always going to be less typing.

With the example you gave I could honestly go either way as it is only 3 characters longer than with var. But with more complex declarations especially generic declarations(List<Person>) I lean strongly toward var. I like what generics do for us, but their syntax is very noisy and distracting.

Over the next couple of years I think the influence from Ruby and Python will spread through our strongly typed crowd and we'll loosen our grip on having to explicitly type everything.

Chris
Comments are closed.