.NET 中方法名称可以以 Get 开头吗

发布于 2024-07-14 14:27:34 字数 507 浏览 7 评论 0原文

我最近在我们公司看到了一份针对我们维护的代码的审计报告,其中指出我们不应该在方法(而不是属性)命名中使用 Get,例如 GetSearchResults 或 GetXyzInformation。 我查找了 MS 指南 (http://msdn.microsoft.com/en-us/library /4df752aw(VS.71).aspx) 用于方法命名,并且允许 Get,那么您对此有何想法,从标准角度来看我们是否可以拥有它,如果不是为什么?

附加信息
我在第一组答案之后添加了更多信息,我所指的方法是涉及数据库交互的方法,因此显然属性不是理想的选择。

I came across a recent Audit report in our company for the code we maintain which says that we should not use Get in the method (not properties) naming like in GetSearchResults or GetXyzInformation. I looked up the MS guidelines (http://msdn.microsoft.com/en-us/library/4df752aw(VS.71).aspx) for method naming and as per that Get is allowed, so what are your thoughts on this, Can we have it or not from a standard perspective, if not why?

Additional Info

I am adding more information after the first set of answers, the Methods that i am referring to are the kind which involves a database interaction, so clearly properties are not ideal options.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

猛虎独行 2024-07-21 14:27:34

是的,他们当然可以 - 标准 API 中有很多示例(例如

但是,通常在创建 GetFoo 方法时值得考虑您是否实际上需要一个属性。

Yes, they certainly can - there are plenty of examples in the standard API (e.g. Delegate.GetInvocationList).

However, generally it's worth considering whether you actually want a property when you create a GetFoo method.

不知在何时 2024-07-21 14:27:34

它是完全合法的(甚至可以合法地编写“get_Foo”形式的方法,如果您要使用 getter 声明属性 Foo,它会与编译器合成的 get 方法发生冲突)。

至于它是否是一件事情:

  1. 如果该方法做了认真的工作,有副作用或可能抛出异常,那么它可能不是属性的良好候选者,并且名称是描述性的,所以不应进行不必要的更改
  2. 如果该方法没有做任何认真的工作,则将其作为属性可能是一件好事,因为它将使调用代码更容易使用。
  3. 如果该方法实际上没有“获取”任何东西,则该名称是错误的,应该更改

以上几点是主观的,但像“从不”“总是”这样的硬规则可能是一个坏主意的原因是平衡多个主观和竞争因素是成为优秀开发人员的一部分。

It is perfectly legal (it is even legal to write methods of the form "get_Foo" which would collide with the compiler synthesized get method if you were to declare a property Foo with a getter).

As to Whether it is a good thing:

  1. If the method does serious work, has side effect or may throw an exception is it probable that it is not a good candidate for a property and the name is descriptive so should not be changed needlessly
  2. If the method does no serious work making it a property is probably a good thing since it will make it easier to work with in calling code.
  3. if the method does not actually 'Get' anything the name is bad and should be changed

The above points are subjective but the reason that hard rules like 'never' 'always' can be a bad idea is that the balancing of multiple subjective and competing factors is part of what being a good developer is all about.

月下凄凉 2024-07-21 14:27:34

是的,您可以做到这一点,但这通常强烈表明您应该真正创建一个属性(特别是如果您的方法没有参数)。

来自 框架设计指南

不要具有与“获取方法”名称匹配的属性,如下例所示:

public string TextWriter { get {...} set {...}}
public string GetTextWriter(int value) {...}

You can do it, yes, but it is often a strong indication that you should really be creating a property (especially if your method has no parameters).

From the Framework Design Guidelines:

DO NOT have properties that match the name of "Get methods" as in the following example:

public string TextWriter { get {...} set {...}}
public string GetTextWriter(int value) {...}
荒芜了季节 2024-07-21 14:27:34

如果您的 Get 方法接受参数,那么相应地命名它是绝对可以的。

如果它不接受参数,请将其重命名为 SearchResults { get; 它

比在 VB.NET 中使用参数属性要好得多:

ReadOnly Property Foo(ByVal i As Integer, ByVal j As Integer) As String
  Get
    Return ""
  End Get
End Property

事实上,在 IL 中它将是方法 GetFoo(int, int) 但 VB.NET 是很棒的语言!

If your Get-method accepts parameters, it's absolutely OK to name it accordingly.

If it doesn't accepts parameters, rename it to SearchResults { get; }

And it's much-much better then use parametric properties like in VB.NET:

ReadOnly Property Foo(ByVal i As Integer, ByVal j As Integer) As String
  Get
    Return ""
  End Get
End Property

In fact, in IL it will be method GetFoo(int, int) but VB.NET is wonderful language!

一个人的旅程 2024-07-21 14:27:34

是的,如果该方法有一些副作用、执行速度慢或需要参数(在所有情况下,您绝对不应该使用 Xyz 属性),我确实会使用 GetXyz

就个人而言,我会混合搭配 GetXyzFindXyz,以防返回 null 可能不是 get< 的正确做法。 /em>. 例如,如果 get 没有找到它要查找的内容,则它可以分配一个新对象,而 find 将返回 null

Yes, I'd indeed use GetXyz if the method has some side effects, is slow to execute or requires arguments (all cases where you definitely should not use an Xyz property).

Personally, I mix and match GetXyz and FindXyz in cases where returning null might not be the right thing to do for a get. A get could allocate a new object if it does not find what it is looking for, whereas a find would return null, for instance.

傲性难收 2024-07-21 14:27:34

当然,您可以在方法名称中使用 Get。 就我个人而言,我用它来进行区分:

不称为 Get:

public string GetSomeString()
{
     return _someMemberVariable;
}

编辑: 看来我在这里不清楚,我的意思是表明名称中带有 Get 的属性是如果他们不进行处理,就会产生误导。

方法名称中的Get一词意味着需要付出努力。

public string GetSomeString()
{
     //Some code that does processing or includes logic.
}

我相信《清洁代码》中提到这是一个很好的实践(以明确它不仅仅是交回一个值)。

Absolutely you can use Get in the method name. Personally I use it to make this distinction:

Not called Get:

public string GetSomeString()
{
     return _someMemberVariable;
}

Edit: It seems I wasn't clear here, I meant to show that properties with Get in the name are misleading if they do no processing.

The word Get in a method name implies effort required.

public string GetSomeString()
{
     //Some code that does processing or includes logic.
}

I believe this is mentioned in Clean Code as a good practice (to make clear that it's not simply handing back a value).

清风挽心 2024-07-21 14:27:34

我使用的技术与上面的一些示例类似 - 我的标准是,如果属性/方法公开类中已保存的数据,则使用属性。 如果我们触发某种操作来获取请求的值(例如从数据库或尚未存储在类中的其他源),请使用 Get。

过于简单化,属性是名词,方法是动词。

I use a technique with similarities to some of the above examples - my criteria is that if the property/method is exposing data that already is held in the class, use a property. If we are triggering some kind of action to go obtain the requested value, say from a database, or some other source not already stored inside the class, use a Get.

Oversimplified, properties are nouns and methods are verbs.

瑾兮 2024-07-21 14:27:34

在我看来,使用 Get 作为方法名称的开头是很自然的。

It is quite natural to use Get as the begin of method name in my opinion.

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