什么名称应该具有返回嵌套对象数量的方法?

发布于 2024-08-23 11:15:53 字数 342 浏览 4 评论 0原文

假设我们有 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 技术交流群。

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

发布评论

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

评论(4

牛↙奶布丁 2024-08-30 11:15:53

我的建议:

public interface Gallery {

    int getNumberOfImages();

    //...
}

我同意, countXX() 给人的印象是调用此方法会触发某种计算。所以我也不会在这里使用它。

My suggestion:

public interface Gallery {

    int getNumberOfImages();

    //...
}

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.

烟燃烟灭 2024-08-30 11:15:53

出于同样的原因,我也会排除 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 towards size().

潜移默化 2024-08-30 11:15:53

如果您正在考虑使用一种返回大小的方法和另一种返回第 n 个元素的方法,我的一般建议是:不要这样做!使用集合 API 的强大功能!:创建一个返回 List 的方法。
这样,API 的用户可以更轻松地执行诸如迭代图像(使用增强的 for 循环而不是必须处理索引)、对它们进行排序、将它们组合到其他集合中等操作,

这真的很容易实现:

  • 如果图像在内部已经表示为列表,您可以直接返回该列表(可以选择用 Collections.unmodifyingList 包装)。
  • 即使您不将它们存储为列表,将它们公开为 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:

  • If the images are internally already represented as a list, you can just return that (optionally wrapped with Collections.unmodifiableList).
  • And even if you don't store them as a list, exposing them as List<Image> is as easy as writing a small subclass of AbstractList by just overriding size() and get(int), the two methods you were going to implement anyways.
居里长安 2024-08-30 11:15:53

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 prefer getImageDimensions() to getImageSize(). In your case, I like countImages() because it's clear and concise, but it doesn't follow naming conventions if for example, there are methods like getImage(int). Therefore, I will have to go with int getImagesCount() or better, int getNumberOfImages() or int getNumImages()

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