GetCountOfObjects、GetNumberOfObjects 和 GetObjectCount 哪个最好?

发布于 2024-10-06 18:36:43 字数 156 浏览 2 评论 0原文

我是一名来自非英语国家的 C++ 程序员。我总是对如何选择以下函数名称之一感到困惑:

GetCountOfObjects

GetNumberOfObjects

GetObjectCount

谁能告诉我它们之间的细微差别是什么?

I am a C++ programmer from a non-English country.I am always confused about how to choose one of the following function names:

GetCountOfObjects

GetNumberOfObjects

GetObjectCount

Who can tell me what the subtle differences are between them?

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

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

发布评论

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

评论(5

乙白 2024-10-13 18:36:43

我也是来自非英语国家的程序员,但我认为选择名称的最佳方法是

  1. 使用最清晰的名称
  2. 使用最短的名称足以轻松理解
  3. 另外,英语假设最好交换顺序而不是使用“Of”。

因此,恕我直言,最好的变体是这里的“GetObjectCount”,当然,如果它返回对象的数量。

I'm also a programmer from non-English country, but I think the best way to choose the name is

  1. use the name that is the most clear
  2. use the shortest name enough to understand easily
  3. Also, english language suppose that it's better to swap the order than use 'Of'.

So, IMHO the best variant is 'GetObjectCount' here, of course if it returns the quantity of object.

难理解 2024-10-13 18:36:43

GetNumberOfObjects 听起来可能最接近自然英语。 GetCountOfObjects 听起来有点尴尬。除此之外,几乎没有什么区别。

我个人的风格可能是对仅返回已知数字的方法使用 GetNumberOfObjects,而对实际执行计数的方法使用 CountObjects。

编辑:至少对我来说,造成这种差异的原因是“数字”一词更常用作名词,而“计数”更常用作动词。

确实,这是一种风格选择。坚持使用你选择的任何东西就可以了。

GetNumberOfObjects probably sounds closest to natural English. GetCountOfObjects sounds slightly awkward. Other than that, there is almost no difference.

My personal style would probably be to use GetNumberOfObjects for a method that just returns a known number, but CountObjects for a method that actually performs the counting.

EDIT: The reason for this difference, at least to me, is that the word 'number' is more commonly used as a noun while 'count' is more commonly used as a verb.

Really, this is a style choice. Use whatever you choose consistently and it will be fine.

时光沙漏 2024-10-13 18:36:43

使用任何你想要的东西,但要始终如一地使用它。

Use whatever you want, but use it consistently.

陌上青苔 2024-10-13 18:36:43

如果有意义的话,我会选择最短的:size()。也就是说,如果您尝试将成员函数添加到某种程度上类似于容器的类,则使用现有库中用于相同概念的相同名称将使代码更易于阅读。

即使这没有意义,虽然在 Java 中 getter 和 setter 很常见,但在许多 C++ 库中,相同的函数名称将删除 get 部分并提供更短的名称:GetNumberOfObjects => NumberOfObjects, GetObjectCount =>; ObjectCount...如果您想让您的对象与容器不同(因此您明确希望避免 size()),我可能会选择 objectCount< /code> 或 numObjects。虽然 numObjects 不是正确的英语,但它很容易阅读和解释,而且很短。

I would go for the shortest simplest: size() if it makes sense. That is, if you are trying to add a member function to a class that somehow resembles a container, using the same names that are used in existing libraries for the same concepts will make code simpler to read.

Even if that does not make sense, while in Java getters and setters are common, in many C++ libraries the same function names will drop the get part and provide a shorter name: GetNumberOfObjects => NumberOfObjects, GetObjectCount => ObjectCount... If you want to make your object different from containers (and thus you explicitly want to avoid size()) I would probably go for objectCount or numObjects. While numObjects is not proper english it is easy to read and interpret and it is short.

萌逼全场 2024-10-13 18:36:43

使用您觉得舒服的名称,但要保持一致。避免太长的名称,因为您可能会犯错。您还可以在名称中使用总和类型的区别2帮助您确定变量的类型,或者它是静态的,本地的还是公共的,或者私人的

use whichever u feel comfortable wid but be consistent wid it.avoid very long names as u can err.also u can use sum kind of distinction in d names 2 help u figure out type of variable or whether it is static,local or public or private

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