Resharper: vars
Asked Answered
G

8

21

Why does Resharper want you to change most variables to var type instead of the actual type in the code?

Gentlefolk answered 17/11, 2008 at 20:26 Comment(0)
U
32

It's just an option. You can disable it:

ReSharper -> Options -> Code Inspection -> Inspection Severity -> Code Redundencies -> Use 'var' keyword where possible: change this to "Do not show"

There's also the context (lightbulb) option which will take you in each direction - this is under ReSharper -> Options -> Languages -> C# -> Context Actions -> "Replaces explicit type declaration with 'var'"

Uranometry answered 17/11, 2008 at 20:30 Comment(4)
I guess from your knowledge of Resharper you think it is worth the licensing price?Gentlefolk
Absolutely. I love it. Then again, for me as an MVP the licensing price was $0, so I'm biased ;)Uranometry
With ReSharper 6, I found this under ReSharper > Options > Code Inspection > Inspection Severity > Language Usage Opportunities > Use 'var' keyword when possible. Set it to 'Do not show'.Kestrel
This option, while useful to learn about new language features can be bad for new programmers that follow it to be book and make everything var! Maybe this should be off by default.Merissameristem
G
22

I saw a video from Hadi Hariri, where he was presenting Resharper 6.x. His reasoning was, if you are forcing a user to use "var", you are actually forcing him to name the variable in a more meaningful way, that way all the names are readable and make more sense.

Guadalquivir answered 30/8, 2011 at 17:23 Comment(0)
I
6

By default, it will "green squiggle" declarations of this type :

Person p = new Person();
^^^^^^

Because of the repetition.

It will also suggest (small green underscore) var when it can be inferred :

Person p = repository.GetPerson(1);
¯¯¯

In this case it can be infered because of the return type of the GetPerson method.

As stated by Jon Skeet, you can disable these suggestions in resharper's options.

Ingeingeberg answered 17/11, 2008 at 20:33 Comment(0)
J
5

To answer your question ... because someone at JetBrains decided that was the "preferred" way.

To change it follow Jon's answer. Additionally you can also change ReSharper's behavior when doing Code Cleanup (which I use a lot) under the Code Cleanup section in ReSharper options. Set to "Use Explicit Type".

Jarnagin answered 17/11, 2008 at 20:33 Comment(2)
This is prolly the only thing i dislike with Resharper, out of the box. IMO it promotes bad programming ... but var vs !var is a religous war .. so each to their own.Mellins
@Mellins , I would agree, but floatingmarbles answers actually makes a bit of sense :). It will force you to have a meaningful name ... If only I could get over my bad habit of writing : var v = ... :)Dday
G
3

This is the explanation on the JetBrains code inspection wiki about it: http://confluence.jetbrains.net/display/ReSharper/Use+%27var%27+keyword+when+initializer+explicitly+declares+type

If you see the class to the right there is no big need to see it on the left as well. Also it saves space and reduces code if the class name is quite long. Personally I don't use var for simple types like string, int and so on but do use it for something like var dictionary = new Dictionary<string, int>() to save space.

Guenna answered 23/11, 2012 at 9:28 Comment(0)
P
1

I think it suggests you both ways. If you have a explicit type - you can change it to var. If you have var - can change it to explicit. Just to make it faster for you to change if you think it is appropriate of course.

It might be good to use vars, for instance, for loop variables, when iterating a collection, so on, when the type is "implicit" for you (it is always implicit for compiler of course, when Resharper suggests it) and its absence doesn't make the code less readable. Also, I like it to shorten some declarations, which may grow quite long with generics. Like, IList(IDictionary(SomeType)) myVar = List(IDictionary(SomeType)) () would not loose much if you write "var" at the left side of the assignment.

(Replace parantheses with angle brackets ;)

Of course, I would try to use vars with care, to improve readability and not vice versa.

Pilloff answered 17/11, 2008 at 20:35 Comment(2)
(you can use angle brackets if you contain within back quotes) SomeCode<int>.Derian
Conversion to explicit type is only a context action (at least in my R# 5.1.3). So it seems that JetBrains prefers the 'vars'. But it's ok for me because I do not look at suggestions so much.Felty
P
1

For me, it is definitely worth the price...(even if I had to pay it myself). But it can slow down your VS. It can get really slow if you have files like 5000 lines of code.

What I still don't get however is why I am the only one in the team using it...

Pilloff answered 17/11, 2008 at 20:41 Comment(3)
Yeah, I'm looking forward to the next release, which is supposed to be performance-focussed. Also, me too :)Derian
A file with 5000 lines of code - wow. Sounds like some refactoring might be in order. BTw, you can split up the code file by defining partial classes.Saponify
actually I have one with 6000. partial classes would not help - DNA :)Pilloff
A
0

Vars help to make the code more readable inside a method, especially if you use generics.

As Jon says, it is just an option.

Atbara answered 17/11, 2008 at 20:33 Comment(1)
Not in every case. Consider the example Status satus = foo.DoSomething(). Writing this with var hides the actual return type and makes it more unreadable. status can be anything eg an int.Cousteau

© 2022 - 2024 — McMap. All rights reserved.