Resharper: "Use implicitly typed local variable declaration"

I’ve been using Reshaper for a couple weeks now. I like it a lot. When I was given the option to get a license for this, I responded that I would rather Code Rush. Afterall, I write more code than I refactor.

That ended up being an over simplification, though. Resharper has lots of great goodies in it. Though I would still like Code Rush, Reshaper is great all by itself. I learn to appreciate it more any day.

Except for the “Use implicitly typed local variable declaration” hint.

As an example, I have this line of code:

ServiceDescription description = attribute as ServiceDescription;

the type, ServiceDescription, is underlined with the fore-mentioned hint. Basically, its telling me to define the type as VAR and let the compiler figure it out for me.

I’m not a fan of that suggestion at all. If I know what type it is, then I want to specify the type. I don’t need the compiler to figure it out for me. If I end up specifying a less than optimistic type (ie: should’ve used XmlReader instead of XmlTextReader), then Resharper or FxCop will let me know, and I’ll learn from my mistake rather than just let the compiler do its voodoo for me.

However, I don’t want to be irrational and just blindly shut off the hint. I wanted to find the justification for that hint, so I started poking around. It seems that there are 2 prevailing schools of though on this: Those that think you should use it for everything, and those that think you should only use it for anonymous types.

After reading a few different things, I have committed to my opinion expressed above: If you don’t know what type it will be (because its anonymous), the use var. Otherwise, specify the type.

A guy from resharper justifies it here:

While interesting, it doesn’t sell me. Some comments on some bullets:
– Its is required to expres variables of anonymous type – no kidding. that’s why it was invented.
– It induces better naming for local variables – that’s putting a square peg in a circle hole. Its handholding at best. If you name you’re variable CURRENT then its scope should be so small as to always remain obvious what it is. If it isn’t obvious, then you named it wrong, and declaring it of type var isn’t going to make you name it any better.
– It induces variable initialization. – Again, I don’t need VAR to force me to do that.
– It removes code noise. – Maybe. I’d like to see some samples before I buy it.
– It doesn’t require a using directive – so what? Are using directives troublesome to anyone? Heck, Resharper puts it in for you. If you don’t have resharper, then CONTROL+. will put it in for you.

That’s my story.


2 Responses to Resharper: "Use implicitly typed local variable declaration"

  1. hamletcode says:

    I’ve evolved from this opinion since writing the post. I now use VAR more often than I used to.

    I started by saying “if the type on the right is obvious, then var is ok. If it’s not, don’t use it.”

    So, I’d use it for VAR READER = NEW STREAMREADER().

    but I wouldn’t use it as


    because, just by reading it, you don’t know what GETREADER returns.

    But as more time passes I find myself using it in other scenarios as well.

  2. hamletcode says:

    How times changes. I’m now annoyed by languages that won’t let me use var. (I’m looking at you, java.)

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: