Changhong's Technical Blog

  Home  |   Contact  |   Syndication    |   Login
  16 Posts | 0 Stories | 50 Comments | 3 Trackbacks

News

Archives

Post Categories

Blogs I read from kiwis...

Blogs I read...

If you use resharper with Visual Studio 2008, you may have noticed that it keeps suggesting you use implicitly typed local variable declaration i.e. the var keyword. To me, it is a bit annoying.

 

You can actually find why people think it should exist in the first place:http://resharper.blogspot.com/2008/03/varification-using-implicitly-typed.html

 

There are some valid points, but I am not convinced. I think it is less readable in most of the case. I can’t agree with Ilya that it induces better naming for local variables or better API. You got to name your variables properly no matter what you use, right? It won't make a good programmer better, but it will make a bad programmer even worse.

 

I do agree that compare to

Dictionary<string, Employee> employees = new Dictionary<string, Employee>();

That var employees = new Dictionary<string, Employee>();

does have same readability and it does save you a few key strikes. The benefit is really small, and you have to make judgment on it. The problem is you may have different ideas at different time and different team members may have their own habit. So, in your team, if the coding standard is “Use var whenever you think it good”, it will inevitably cause inconstant coding style. I would rather avoid it by having “Never use var”, you would lost too much after all.

 

So, if you don't like it neither, this is the way to turn it off. From “Resharper” menu, go to “Options…”, and from “Code Inspection”, then from “Inspection Severity”, find "Use Var keyword when possible” and set to “Do not show”

 

 
posted on Monday, September 08, 2008 6:24 AM

Feedback

# re: Turn off Resharper’s suggestion of using implicitly typed locals 1/6/2009 3:55 AM Hans
Just wanted to let you know: You're not alone!
Apart from the fact that var is handy e.g. with LINQ operations, I don't like this lazy declaration style very much. Or maybe I just have to get a little warmer with it...

Anyway, at this time I have disabled the mentioned ReSharper options, too, to prevent it from pointing this out over and over again.

# re: Turn off Resharper’s suggestion of using implicitly typed locals 3/10/2009 2:33 AM Xavier
Thank you for your post. I just installed Resharper and I was wondering why it kept suggesting using "var"!

I use "var" for LINQ queries (because it was done for that) and nothing else!

I saw a wonderfull example of "var" and "extension methods" in the help of QuickGraph (CodePlex):

var edges = new Edge<int>[] { new Edge<int>(1,2), new Edge<int>(0,1), ... };
var graph = edges.ToAdjacencyGraph(edges);

Try to understand how to use it for your own needs!
It took me half a day!

# re: Turn off Resharper’s suggestion of using implicitly typed locals 3/31/2009 4:07 AM M
thanks, Var just makes the code less readable... been googleing this problem for a while..

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: