GetCountOfObjects、GetNumberOfObjects 和 GetObjectCount 哪个最好?
我是一名来自非英语国家的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我也是来自非英语国家的程序员,但我认为选择名称的最佳方法是
因此,恕我直言,最好的变体是这里的“GetObjectCount”,当然,如果它返回对象的数量。
I'm also a programmer from non-English country, but I think the best way to choose the name is
So, IMHO the best variant is 'GetObjectCount' here, of course if it returns the quantity of object.
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.
使用任何你想要的东西,但要始终如一地使用它。
Use whatever you want, but use it consistently.
如果有意义的话,我会选择最短的:
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 avoidsize()
) I would probably go forobjectCount
ornumObjects
. WhilenumObjects
is not proper english it is easy to read and interpret and it is short.使用您觉得舒服的名称,但要保持一致。避免太长的名称,因为您可能会犯错。您还可以在名称中使用总和类型的区别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