54

可能重复:
为什么 ReSharper 想要对所有内容都使用“var”?

我有 ReSharper 4.5,到目前为止发现它非常宝贵,但我有一个担忧;
似乎想让每个变量声明都是隐式的(var)。
作为一个相对较的开发人员,在这方面我应该信任 ReSharper 多少?

从绘制选项卡标题的方法中获取以下代码片段。

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 希望我将所有 5 个更改为var。我已阅读以下类似的帖子Use of var keyword in C#,但我想从 ReSharper 的角度了解。

4

7 回答 7

45

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.

于 2009-04-10T14:49:24.517 回答
22

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

于 2009-04-10T14:59:08.427 回答
15

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.

于 2009-04-10T14:57:50.300 回答
7

Resharper 认为这是最佳实践,但正如您在链接帖子中所读到的,有些人不同意。我喜欢使用显式声明来提高可读性,但对每个人来说都是如此。如果要使用显式声明,可以在 Resharper 中禁用该规则。

于 2009-04-10T14:47:18.797 回答
7

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?

于 2012-01-09T23:26:13.863 回答
0

这个问题是开始火焰战争的好方法。然而,你应该做任何你和你一起工作的人认为最易读的事情。关于 var 的争论双方都有很好的论据。

也就是说,如果您认为显式声明类型更具可读性,那是您的事。您不必做 Resharper 告诉您的所有事情。如果您愿意,您甚至可以禁用该规则。

于 2009-04-10T14:48:08.907 回答
0

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.

于 2009-04-10T15:03:05.953 回答