ReSharper 和 var
可能的重复:
为什么 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 keywords in C#,但是我想从 ReSharper 的角度了解情况。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Resharper 主要致力于帮助您重构代码,而
var
关键字通常使重构变得更加容易。 例如,如果任何这些函数的返回值更改为兼容类型,则您不必更改任何代码。 因此,现在重构tabCaseNotes
类型变得更加容易。就我个人而言,我通常倾向于保留前两行,因为我喜欢在声明变量的行的某个地方看到显式列出的变量的类型名称。 如果有的话,我可能会寻找一个接口来代替,这样我也可以获得与
var
关键字相同的“通用性”,而不会丢失任何重要的可读类型信息。 但是,我肯定会使用var
来表示fillBrush
、textBrush
和sf
。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 yourtabCaseNotes
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 usevar
forfillBrush
,textBrush
, andsf
.您不需要在行中添加类型来使其更具可读性,这取决于个人喜好。 我确实喜欢 var 变体:
更新:我将添加一个有争议的观点。 除非我正在阅读书中的代码,否则我通常不关心理解我正在阅读的某些代码行的具体类型是什么。 考虑 .GetTableRectangle(e.Index),您没有显示对其进行操作的代码:
在阅读该特定代码时,我将通过对 itemRect 的操作而不是其类型来更多地了解它。 它可以是 I矩形、矩形、自定义矩形,但仍然不会过多说明代码如何使用它。 相反,我更关心 itemRect.Height、itemRect.Width 或 itemRect.GetArea() 以及所涉及的逻辑。
更新2:正如其他人指出的那样,您可以将其关闭。 确保团队保持相同的实践,否则每次不同的人接触代码时,您可能最终都会以一种或另一种方式进行更改。 请参阅:http://www.jetbrains.com/resharper/features/codeTemplate.html
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:
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:
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
Resharper 不希望您使用
var
,它为您提供了选择。 如果您确实使用var
,它将为您提供使用显式类型的选项,因此您无法获胜:-)。编辑 - 有趣的链接讨论这个话题。
好像可以关掉,去Resharper -> 选项-> 代码检查 -> 检查严重性并向下滚动一点以查看与
var
相关的选项。Resharper doesn't want you to use
var
, it is giving you the option. If you do usevar
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
.Resharper 认为这是最佳实践,但正如您在链接文章中所读到的那样,有些人不同意。 我喜欢使用显式声明来提高可读性,但每个声明都有自己的特点。 如果要使用显式声明,可以在 Resharper 中禁用该规则。
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.
在 C# 中,我更喜欢在任何地方使用
var
。 为什么? 出于同样的原因,我使用firstName
而不是strFirstName
或amount
而不是intAmount
。 当然,更详细的方式在一张纸上或(正如您所指出的)一本书上更具可读性,但我的代码还没有出现在书中。现在,我不久前向我的一位同事询问了
intAmount
业务,他提出了一个非常好的观点。 他说,它在直接文本编辑器中很有用,但是当您拥有 Intellisense 时,只需将鼠标悬停在变量上即可获得相同的信息甚至更多信息。最后,尽管我很欣赏其他人指出的折衷方案(即使用
var
与new
语句),并且该论点有效且有力,但我倾向于引导远离这一点仅仅基于一致性和速记可读性。 我的一致性论点是,如果可读性对您来说如此重要,那么为什么不使用intAmount
这样您就可以稍后在代码中告诉数据类型呢?In C#, I prefer to use
var
everywhere. Why? For the same reason that I usefirstName
instead ofstrFirstName
oramount
rather thanintAmount
. 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 thenew
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 useintAmount
so you can tell the data type later on in the code?这个问题确实是引发一场口水战的好方法。 但是,您应该做您以及与您一起工作的人认为最可读的事情。 关于 var 的争论双方都有很好的论据。
也就是说,如果您认为显式声明类型更具可读性,那是您的事。 您不必执行 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.
你确实可以把它关掉,我也这么做了。 我承认,在某些类类型名称很长的情况下,这很好,例如:
是缩短为 var 的良好候选者,但就我个人而言,这是我唯一希望更改它的时候。 我认为当从方法的返回值(如第二行中)分配变量时使用它不是一个好主意,因为可能无法立即清楚它返回的变量类型到底是什么。
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:
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.