[ACCEPTED]-ReSharper and var-implicit

Accepted answer
Score: 46

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

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

Score: 22

You don't need to have the type in the line 21 to make it more readable, its a matter of 20 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 19 will add a controversial view on it. Unless 18 I am reading code from a book, I don't usually 17 care what's the specific type for understanding 16 some lines of code I am reading. Consider 15 the .GetTableRectangle(e.Index), for which 14 you are not showing the code that operates 13 on it:

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

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

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

Score: 15

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

EDIT - interesting link discussing 4 the topic.

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

Score: 7

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

Score: 7

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

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

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

Score: 0

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

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

Score: 0

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

SuperDisconfibulator sd=new SuperDisconfibulator();

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

More Related questions