ReSharper 和 var

发布于 2024-07-16 07:07:40 字数 1029 浏览 5 评论 0原文

可能的重复:
为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夜巴黎 2024-07-23 07:07:40

Resharper 主要致力于帮助您重构代码,而 var 关键字通常使重构变得更加容易。 例如,如果任何这些函数的返回值更改为兼容类型,则您不必更改任何代码。 因此,现在重构 tabCaseNotes 类型变得更加容易。

就我个人而言,我通常倾向于保留前两行,因为我喜欢在声明变量的行的某个地方看到显式列出的变量的类型名称。 如果有的话,我可能会寻找一个接口来代替,这样我也可以获得与 var 关键字相同的“通用性”,而不会丢失任何重要的可读类型信息。 但是,我肯定会使用 var 来表示 fillBrushtextBrushsf

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.

渔村楼浪 2024-07-23 07:07:40

您不需要在行中添加类型来使其更具可读性,这取决于个人喜好。 我确实喜欢 var 变体:

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
   };

更新:我将添加一个有争议的观点。 除非我正在阅读书中的代码,否则我通常不关心理解我正在阅读的某些代码行的具体类型是什么。 考虑 .GetTableRectangle(e.Index),您没有显示对其进行操作的代码:

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

在阅读该特定代码时,我将通过对 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:

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

路还长,别太狂 2024-07-23 07:07:40

Resharper 不希望您使用var,它为您提供了选择。 如果您确实使用 var ,它将为您提供使用显式类型的选项,因此您无法获胜:-)。

编辑 - 有趣的链接讨论这个话题。

好像可以关掉,去Resharper -> 选项-> 代码检查 -> 检查严重性并向下滚动一点以查看与 var 相关的选项。

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.

故事还在继续 2024-07-23 07:07:40

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.

瀟灑尐姊 2024-07-23 07:07:40

在 C# 中,我更喜欢在任何地方使用 var。 为什么? 出于同样的原因,我使用 firstName 而不是 strFirstNameamount 而不是 intAmount。 当然,更详细的方式在一张纸上或(正如您所指出的)一本书上更具可读性,但我的代码还没有出现在书中。

现在,我不久前向我的一位同事询问了 intAmount 业务,他提出了一个非常好的观点。 他说,它在直接文本编辑器中很有用,但是当您拥有 Intellisense 时,只需将鼠标悬停在变量上即可获得相同的信息甚至更多信息。

最后,尽管我很欣赏其他人指出的折衷方案(即使用 varnew 语句),并且该论点有效且有力,但我倾向于引导远离这一点仅仅基于一致性和速记可读性。 我的一致性论点是,如果可读性对您来说如此重要,那么为什么不使用 intAmount 这样您就可以稍后在代码中告诉数据类型呢?

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?

糖粟与秋泊 2024-07-23 07:07:40

这个问题确实是引发一场口水战的好方法。 但是,您应该做您以及与您一起工作的人认为最可读的事情。 关于 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.

走野 2024-07-23 07:07:40

你确实可以把它关掉,我也这么做了。 我承认,在某些类类型名称很长的情况下,这很好,例如:

SuperDisconfibulator sd=new SuperDisconfibulator();

是缩短为 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文