Changhong's Technical Blog

  Home  |   Contact  |   Syndication    |   Login
  13 Posts | 0 Stories | 82 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”

 

 
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
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..

# re: Turn off Resharper’s suggestion of using implicitly typed locals 1/7/2010 3:29 AM Nir
Thanks, it's very annoying. Your explanation was very clear.

# re: Turn off Resharper’s suggestion of using implicitly typed locals 4/9/2010 4:40 PM Adam
I agree completely. When code is close together,
var x = new Something();
return x.field;

you can tell what it is... but when there is a lot of code separating it, it makes it hard to tell.

Using var reminds me of the way dBASE / FoxPro pretty much started out (without any type), so you would have
x = 1
y = "some string"

and later on, checking
if (x=y)
would cause errors.


# re: Turn off Resharper’s suggestion of using implicitly typed locals 8/5/2010 8:41 AM Chris Marisic
The var keyword should be used for all complex types IMO, for primitive types it's a subjective view.

var is impossible to make the code less readable. It's not possible to do var bob. lalalalalalala bob = new bob(). Having Bob bob = new Bob() is farrrrrr less readable. This is not the dynamic keyword. It's merely removing verbosity from the code that adds noise.

# re: Turn off Resharper’s suggestion of using implicitly typed locals 8/24/2010 7:01 PM Bob Miner
Thanks for this post. I use var for LINQ but believe otherwise a more specific type is appropriate and clearer when possible. var reduces clarity in my opinion, hiding type information.

# re: Turn off Resharper’s suggestion of using implicitly typed locals 12/21/2010 5:28 PM Keener
I say let me decide if I want var or not, I don't need a suggestion on it. This should be turned off by default. Bless you for finding out what setting this was. It will always be turned off on my machine.

# re: Turn off Resharper’s suggestion of using implicitly typed locals 4/1/2011 6:36 AM Casper
Thank you for letting me get of this retarded warning!

# re: Turn off Resharper’s suggestion of using implicitly typed locals 6/23/2011 1:31 PM Strict Typer
Fantastic tip! Thank you!

# re: Turn off Resharper’s suggestion of using implicitly typed locals 8/30/2011 12:50 PM Dave
Thank you! This was driving me nuts in the trial and making think about not buying it!

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