Question

Possible Duplicate:
Why does ReSharper want to use 'var' for everything?

I have ReSharper 4.5 and have found it invaluable so far but I have a concern;
It seems to want to make every variable declaration implicit (var).
As a relatively new developer, how much should I trust ReSharper when it comes to this?

Take the below code snippet from a method that Paints Tab Headers.

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

Resharper wants me to change all 5 of those to var. I have read the following similar post, Use of var keyword in C#, but I would like to know from a ReSharper standpoint.

Was it helpful?

Solution

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

OTHER TIPS

Resharper is primarily concerned with helping you refactor code, and the var keyword generally makes refactoring easier. For example, if the return values of any of those functions ever change to a compatibile type, you don't have to change any of this code. It's therefore now a little easier to refactor your tabCaseNotes type, for example.

Personally, I'm often inclined to leave your first two lines alone, because I like to see the type name for a variable explicitly listed somewhere on the line where the variable is declared. If anything, I might look for an interface to use instead, so that I also gain the same "generic-ness" as with the var keyword without losing any important readable type information. However, I would definitely use var for fillBrush, textBrush, and sf.

Resharper doesn't want you to use var, it is giving you the option. If you do use var it will then give you the option to use an explicit type, so you can't win:-).

EDIT - interesting link discussing the topic.

It seems it can be turned off, go to Resharper -> Options -> Code Inspection -> Inspection Severity and scroll down a little to see the options related to var.

In C#, I prefer to use var everywhere. Why? For the same reason that I use firstName instead of strFirstName or amount rather than intAmount. Sure, the more verbose way is more readable on a piece of paper or—as you pointed out—a book, but none of my code has shown up in a book yet.

Now, I asked a co-worker of mine a while back about this intAmount business and he brought up a very good point. He said that it's useful in a straight-up text editor, but when you have Intellisense, you can get the same information and more by just hovering over the variable.

Lastly, although I appreciate the happy medium that others have pointed out (i.e. using var with the new statement) and the argument is valid and strong, I'm inclined to steer away from that on the sole basis of consistency and short-hand readability. My consistency argument is that if readability is so important to you, then why don't you also use intAmount so you can tell the data type later on in the code?

Resharper thinks it is best-practice, but some people disagree as you have read in the linked post. I like to use explicit declaration for increased readability, but to each their own. If you want to use explicit declaration, you can disable the rule in Resharper.

This question is a really good way to start a flame war. However, you should do whatever you and whoever you're working with think is most readable. There are good arguments for both sides of the debate about var.

That said, if you think it's more readable to explicitly declare the type, that's your business. You don't have to do everything Resharper tells you to. You can even disable that rule if you want to.

You can indeed turn it off, and I have. I'll admit that it's nice in some cases where the class type name is long, like:

SuperDisconfibulator sd=new SuperDisconfibulator();

would be a good candidate for being shortened to var, but personally that's the only time that I would want it to change. I don't think its a good idea to use it when a variable is being assigned from the return value of a method (like in your second line), because it might not be immediately clear what exactly the variable type it is it returns.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top