Coder Perfect

Why does ReSharper insist on using ‘var’ everywhere? [duplicate]


I’ve only recently begun to use ReSharper with Visual Studio (after the many recommendations on SO). I used a current ASP.NET MVC project to test it out. One of the first and most common suggestions I’ve received is to replace most/all of my explicit declarations with var. Consider the following scenario:

//From This:
MyObject foo = DB.MyObjects.SingleOrDefault(w => w.Id == 1);
//To This:
var foo = DB.MyObjects.SingleOrDefault(w => w.Id == 1);

and so on, even with basic types like int, bool, and so on.

What’s the point of recommending this? I don’t have a computer science or.NET background, and I’ve only lately “fallen into”.NET programming, therefore I’d like to know what’s going on and whether it’s beneficial or not.

Asked by Chris

Solution #1

The var keyword is highly overused, according to ReSharper. It can be used in situations where the type is obvious:

var obj = new SomeObject();

If the type isn’t evident, it’s best to write it down:

SomeObject obj = DB.SomeClass.GetObject(42);

Answered by Guffa

Solution #2

Improved readability is one of the reasons. Which is the superior option?

Dictionary<int, MyLongNamedObject> dictionary = new Dictionary<int, MyLongNamedObject>();


var dictionary = new Dictionary<int, MyLongNamedObject>();

Answered by Mark Sherretta

Solution #3

Personally, I like to disable this suggestion. Var can often improve readability, but it can also reduce it, as you said (with simple types, or when the resulting type is obscure).

I prefer to be able to select when to utilize var and when not to. But, then again, that’s just my opinion.

Answered by Bryan Menard

Solution #4

Variables can improve code readability but reducing instant comprehension. It can, however, make the code less readable in other cases. It is sometimes used in a neutral manner. The relationship between readability and comprehension isn’t proportional; instead, it varies depending on the situation. Both are sometimes increased or decreased at the same time.

pe to the reader, or if type information is required to understand the current program segment.

Bad naming, for example, might lead to var, which reduces code understanding. This is not, however, var’s fault:

var value1 = GetNotObviousValue(); //What's the data type? 
var value2 = Math.Abs(-3); // Obviously a numeric data type. 

It’s not always necessary to use var for simple data types when the code is more understandable without it:

var num = GetNumber(); // But what type of number?
// vs. 
double num = GetNumber(); // I see, it's a double type. 

Var can be used to hide data types that you don’t want to see the complexity of:

    IEnumerable<KeyValuePair<string,List<Dictionary<int,bool>>>> q = from t in d where t.Key == null select t; // OMG! 
    var q = from t in d where t.Key == null select t;

    // I simply want the first string, so the last version seems fine.  

When there is an anonymous type present, you must use var because there is no type name to call it by:

var o = new { Num=3, Name="" };

You can rely less on your knowledge via strict code reading without a help when Visual Studio Intellisense provides type information notwithstanding var. It’s generally a good idea to presume that Intellisense isn’t available to everyone.

In conclusion, based on the instances above, I believe that using var at will is not a good idea because most things are best done in moderation and according to the circumstances at hand, as illustrated below.

Why does Resharper utilize it by default everywhere? I’d suggest for ease, because it can’t parse the nuances of situations to decide when best not to use it.

Answered by John K

Solution #5

The option for the “Use implicitly typed local variable declaration” proposal in ReSharper (8.02, but likely other versions) can be altered to your liking, whatever that may be, by first entering the options menu for ReSharper:

Then modify the “Inspection Severity” of your chosen language, in my instance c#, under “Code Inspection.”

As you can see, there are choices to tweak all of ReSharper’s recommendations. I hope this helps someone like me who already has a var usage strategy in place and just wants ReSharper to follow it:)

Answered by Erikest

Post is based on