count vs length vs size in a collection

2019-01-01 05:11发布

From using a number of programming languages and libraries I have noticed various terms used for the total number of elements in a collection.

The most common seem to be length, count, and size.

eg.

array.length
vector.size()
collection.count

Is there any preferred term to be used? Does it depend on what type of collection it is? ie. mutable/immutable

Is there a preference for it being a property instead of a method?

9条回答
大哥的爱人
2楼-- · 2019-01-01 05:26

I would say that it depends on particular language that you are using and classes. For example in c# if you are using Array you have Property Length, if you have something that inherits from IEnumerable you have extension Method Count(), but it is not fast. And if you inherited from ICollection you have Property Count.

查看更多
梦醉为红颜
3楼-- · 2019-01-01 05:27

In Elixir there is actually a clear naming scheme associated with it across types in the language.

When “counting” the number of elements in a data structure, Elixir also abides by a simple rule: the function is named size if the operation is in constant time (i.e. the value is pre-calculated) or length if the operation is linear (i.e. calculating the length gets slower as the input grows).

查看更多
姐姐魅力值爆表
4楼-- · 2019-01-01 05:36

The terms are somewhat interchangeably, though in some situations I would prefer one over another. Usually you can get the best usage if you think about How would you describe the length/size/count of this element verbally to another person?.

length() implies that the element has a length. A string has a length. You say "a string is 20 characters long", right? So it has a length.

size() implies that the element has a size. E.g. a file has a size. You say "this file has a size of 2 MB", right? So it has a size.

That said, a string can also have a size, but I'd expect something else here. E.g. a UTF-16 string may have a length of 100 characters, but as every character is composed out of two byte, I'd expect size to be 200.

count() is very unusual. Objective-C uses count for the number of elements in an array. One might argue if an array has a length (as in Java), has a size (as in most other languages) or has a count. However, size might again be the size in byte (if the array items are 32 bit int, each item is 4 byte) and length... I wouldn't say "an array is 20 elements long", that sounds rather odd to me. I'd say "an array has 20 elements". I'm not sure if count expresses that very well, but I think count is here a short form for elementCount() and that again makes much more sense for an array than length() or size().

If you create own objects/elements in a programming language, it's best to use whatever other similar elements use, since programmers are used to accessing the desired property using that term.

查看更多
萌妹纸的霸气范
5楼-- · 2019-01-01 05:37

To me, this is a little like asking whether "foreach" is better than "for each". It just depends on the language/framework.

查看更多
裙下三千臣
6楼-- · 2019-01-01 05:38

FWIW (and that's vanishingly close to nothing), I prefer 'Count' because it seems to indicate that it's going to return the number of elements/items in the collection pretty unambigously.

When faced with the terms 'Length' or 'Size' I'm often left wondering for a moment (or even being forced to re-read documentation) whether the damn thing is going to tell me how many elements are in the colection or how many bytes the collection is consuming. This is particularly true for collections that are intended to be contingous like arrays or strings.

But no one who was responsible for the naming conventions used by the Java, BCL/.Net, or C/C++ standard frameworks/libraries bothered to ask me, so you're all stuck with whatever they came up with.

If only I were much smarter than I am and was named Bjarne, all of you might be spared the misery...

Of course, back in the real world, you should try to stick with whatever naming convention is used by the language/platform you're using (eg., size() in C++). Not that this seems to help you with your Array.Length dilemma.

查看更多
千与千寻千般痛.
7楼-- · 2019-01-01 05:39

Adding to @gbjbaanb's answer...

If "property" implies public access to the value, I would say that "method" is preferred simply to provide encapsulation and to hide the implementation.

You might change you mind about how to count elements or how you maintain that count. If it is a property, you're stuck - if it is acessed via a method, you can change the underlying implementation without impacting users of the collection.

查看更多
登录 后发表回答