Why Does Resharper Want to Use 'Var' for Everything

Why does ReSharper want to use 'var' for everything?

One reason is improved readability. Which is better?

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

or

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

Why should I use var instead of a type?

It's really just a coding style. The compiler generates the exact same for both variants.

See also here for the performance question:

  • Will using 'var' affect performance?

var versus concrete type usage

The following is an extract from msdn...

The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code. One example where var is helpful in this manner is with nested generic types such as those used with group operations. In the following query, the type of the query variable is IEnumerable>. As long as you and others who must maintain your code understand this, there is no problem with using implicit typing for convenience and brevity.

However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required.

Reference: http://msdn.microsoft.com/en-us/library/bb384061.aspx

Good Luck!

ReSharper and var

You don't need to have the type in the line to make it more readable, its a matter of personal preference. I do like the var variation:

var currentTab = tabCaseNotes.TabPages[e.Index];
var itemRect = tabCaseNotes.GetTabRect(e.Index);
var fillBrush = new SolidBrush(Color.Linen);
var textBrush = new SolidBrush(Color.Black);
var sf = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};

Update: I will add a controversial view on it. Unless I am reading code from a book, I don't usually care what's the specific type for understanding some lines of code I am reading. Consider the .GetTableRectangle(e.Index), for which you are not showing the code that operates on it:

var itemRect = tabCaseNotes.GetTableRectangle(e.Index);
//do some operations on itemRect

While reading that specific code I will get more to understand it from the operations on itemRect than from its type. It can be IRectangle, Rectangle, CustomRectangle, and still won't say much on what the code is doing with it. Instead I care more for the itemRect.Height, itemRect.Width or itemRect.GetArea() along with the logic involved.

Update 2: As others have pointed out you can turn it off. Make sure to keep the team with the same practices, or you will probably end up with making changes one way or the other each time a different person touches the code. See: http://www.jetbrains.com/resharper/features/codeTemplate.html

When should I use var and when should I use string, ulong, int, etc

There are a few times where you must use an explicit type, and instances where you must use var.

Must use explicit type:

If you declare a variable without initializing it, or, if you want to initialize a variable explicitly as null

 string myString;
int i;
string someString=null;
DateTime? nullableDate=null;

Must use var:

When you use an anonymous type, or a generic type with one or more type parameters that are anonymous types.

var stuff = new {Name="Bob", Age=25};
var bunchOfStuff = from item in items select new {item.Name, item.Age};

Otherwise

It's a question of style and/or taste

ReSharper: Visual Studio : warning use VAR

Quite a few resharper options are opinion based.

  • Use var instead of actual type name.
  • whatever == true is redundant.
  • this. is redundant
  • etc..

You can turn them all of in the resharper settings, or at the time it prompts you.

Keyword var performance for compilation

In my experience, any difference is negligible to unobservable. I can find no difference whatsoever, even on larger projects. Whatever difference var makes is statistically insignificant, and lost amid the rest of the compilation work.

ReSharper & Implicitly Typed Variables

I see at least two reasons.

First, its the matter of DRY principle: don't repeat yourself. If in future you decide to change type of variable from List<> to Stack<> or LinkedList<>, then with var you'd have to change in one place, otherwise you'd have to change in two places.

Two, generic types declaration can be quite long. Dictionary<string, Dictionary<int, List<MyObject>>> anyone? This doesn't apply to simple List<T>, but you shouldn't have two code styles for different object types.



Related Topics



Leave a reply



Submit