什么名称应该具有返回嵌套对象数量的方法?
假设我们有 Gallery 和 Image 类。一个图库中可以有许多图像。
Gallery 应该有一些返回嵌套图像数量的方法。我的建议:
int getImagesCount ();
int countImages ();
int imagesCount ();
我在不同的 API 中看到了这 3 个建议的示例(当然有另一个名词,甚至没有它,比如 Collections API 中的方法 size ()
)。
您更喜欢什么?为什么? (我反对 countImages () 的想法之一是这个名称会让用户认为这个方法做了一些复杂的计算。)
Suppose we have classes Gallery and Image. There can be many images in one gallery.
Gallery should have some method which returns number of nested images. My suggestions:
int getImagesCount ();
int countImages ();
int imagesCount ();
I saw examples of each of these 3 suggestions in different APIs (of course with another noun or even without it, like method size ()
in Collections API).
What would you prefer and why? (One of my thoughts against countImages ()
is that this name can make user think that this method does some complex calculations.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的建议:
我同意,
countXX()
给人的印象是调用此方法会触发某种计算。所以我也不会在这里使用它。My suggestion:
I agree, that
countXX()
leads to the impression, that calling this method triggers some sort of calculation. So I wouldn't use it here as well.出于同样的原因,我也会排除
countImages ()
。除此之外,如果
Gallery
所属的代码库中有或多或少一致的命名约定,我建议遵守它。在没有明确的项目约定的情况下,我个人会倾向于size()
。I would rule out
countImages ()
too, for the same reason.Other than that, if there is a more or less consistent naming convention in the codebase
Gallery
belongs to, I would recommend adhering to it. In absence of a clear project convention, I personally would lean towardssize()
.如果您正在考虑使用一种返回大小的方法和另一种返回第 n 个元素的方法,我的一般建议是:不要这样做!使用集合 API 的强大功能!:创建一个返回
List
的方法。这样,API 的用户可以更轻松地执行诸如迭代图像(使用增强的 for 循环而不是必须处理索引)、对它们进行排序、将它们组合到其他集合中等操作,
这真的很容易实现:
List
就像通过重写编写
和AbstractList
的小子类一样简单size()get(int)
,这两个方法是您要实现的。If you are thinking of having one method that returns the size, and another one that returns the nth element, my recommendation in general: Don't do it! Use the power of the collections api!: create a method that returns a
List<Image>
.That way the user of the API can more easily do things like iterating over the images (using the enhanced for-loop instead of having to juggle with indices), sorting them, combining them in other collections,...
It is really easy to implement:
List<Image>
is as easy as writing a small subclass ofAbstractList
by just overridingsize()
andget(int)
, the two methods you were going to implement anyways.size()
的问题在于,您不一定清楚返回的是元素数量(本例中为图像)还是集合的大小(以字节/千字节/兆字节为单位)。出于同样的原因,我更喜欢getImageDimensions()
而不是getImageSize()
。就您而言,我喜欢countImages()
因为它清晰简洁,但它不遵循命名约定,例如,有像getImage(int)
这样的方法。因此,我必须使用int getImagesCount()
或更好的int getNumberOfImages()
或int getNumImages()
The problem with
size()
is that it's not necessarily clear whether you're returning the number of elements (images in this case) or the size in bytes/kilobytes/megabytes of the collection. For the same reason, I tend to prefergetImageDimensions()
togetImageSize()
. In your case, I likecountImages()
because it's clear and concise, but it doesn't follow naming conventions if for example, there are methods likegetImage(int)
. Therefore, I will have to go withint getImagesCount()
or better,int getNumberOfImages()
orint getNumImages()