何时在方法上使用复数与集合词

发布于 2024-07-11 02:12:11 字数 439 浏览 5 评论 0原文

我正在为一个模块创建一个 API,在我的类中创建了几个方法之后,我问自己这个问题。

现在,作为一个例子,我正在这样做:

public Company GetMonitoredCompany( String companyName ) { ... }
public List<Company> GetMonitoredCompanies( ) { ... }

但我意识到,我多次使用其他 API/服务,有时它们的名称中包含 Collection ,可能是这样的:

public List<Company> GetMonitoredCompanyCollection( ) { ... }

是否有规则这? 一种模式? 或者任何一种方式都应该可以?

I'm creating an API for a module and after I created several methods inside my classes, I asked myself this question.

Right now, and as an example, I'm doing this:

public Company GetMonitoredCompany( String companyName ) { ... }
public List<Company> GetMonitoredCompanies( ) { ... }

But I realize that for several times that I use other API's / Services sometimes they have Collection in the name maybe like:

public List<Company> GetMonitoredCompanyCollection( ) { ... }

is there a rule for this? a pattern? or either way should be ok?

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

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

发布评论

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

评论(6

简美 2024-07-18 02:12:12

永远不要做收藏的事情——除非你觉得它增加了重要的意义。 现代工具能够轻松地告诉您预期的返回类型。 更改返回类型时会发生什么? 您要么将函数名称弄错,要么必须在整个代码中更改它(尽管您可能无论如何都需要这样做,但如果您进行子类化等,则可能不需要这样做)。

出于同样的原因,匈牙利表示法(有争议地)在高级语言中不再特别有用。

在所有情况下,您的代码都应保持一致。

哦,别傻了。 我有一位同事(我认为是在烦恼的时刻)按照 FunctionThatLoadsTheTotalFromFileNumberCount 的方式调用他的函数,试图传达函数返回类型(Number),意思是( Count),它是一个函数(Function),并且它实际上做了什么,相当冗长。

Never do the Collection thing - unless you feel it adds significant meaning. Modern tools are able to easily tell you at a glance what return type to expect. What happens when you change the return type? You either leave the function name wrong, or have to change it throughout your code (albeit you may need to do that anyway, but if you subclass, etc. you may not need to otherwise).

For the same reason as hungarian notation is (controversially) no longer particularly useful in high-level languages.

In all cases, be consistent across your code.

Oh, and don't be stupid. I had a colleague (in a moment of annoyance I think) call his functions along the lines of FunctionThatLoadsTheTotalFromFileNumberCount in an attempt to convey the function return type (Number), meaning (Count), that it was a function (Function) and what it did actually did, rather verbosely.

冰火雁神 2024-07-18 02:12:12

对于本例,我将复制 .NET Framework 命名。 例如 Directory.GetFilesType.GetMembers 并使用复数。 显然,除非这样做会使代码变得不清楚。

For this case, I would copy the .NET Framework naming. E.g. Directory.GetFiles and Type.GetMembers and go with the plural. Unless doing so makes the code unclear, obviously.

梦亿 2024-07-18 02:12:11

使用最短、最简单的名称来清楚地表明方法的用途。
并在您的项目中保持一致。

在这个例子中,如果没有其他考虑,我会使用这个名称:

获取受监控公司

因为它更短、更清晰。

(我还会返回只读的 ICollection 或 IEnumerable ,除非您有特定的理由不这样做。)

Use the shortest, simplest name that clearly shows the methods purpose.
And be consistent within your project.

In this example, if there are no other considerations, I would use the name:

GetMonitoredCompanies

Because it's shorter and clearer.

(I would also return a read-only ICollection or IEnumerable unless you've got some specific reason not to.)

稍尽春風 2024-07-18 02:12:11

由你决定。 但我建议您对整个项目采用相同的约定......
我更喜欢第一种方式(GetMonitoredCompanies)

It's up to you. But I recommend you to have the same convention for whole project...
I prefer the first way (GetMonitoredCompanies)

江湖彼岸 2024-07-18 02:12:11

如果它可能返回多个值(集合、数组等),请使用复数 (GetMonitoredCompanies),否则使用单数。 不要在名称中包含返回的数据类型。 这只是自找麻烦,您更改了返回数据类型,却忘记重命名该方法,从而造成了疯狂的混乱。

If it can possibly return multiple values (collection, array, etc), use a plural (GetMonitoredCompanies), else use a singular. Do not include the returned data type in the name. This is just asking for trouble where you change the return data type and forget to rename the method, sowing mad confusion.

紙鸢 2024-07-18 02:12:11

当仅使用复数来区分集合时,我发现它非常令人困惑。 返回集合时,我通常使用 List 而不是 Get,因此示例如下:

ListMonitoredCompanies

这使得使用智能感知变得非常容易,因为添加、更新、删除、列表和获取函数都有自己的起始字母。

I find it very confusing when only using a plural to distinguish a collection. I normally use List instead of Get when returning collections so the example would be

ListMonitoredCompanies

This makes it very easy to use intellisense as the add, update, delete, list and get functions all have their own starting letter.

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