181

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?

molasses
  • 2,998
  • 5
  • 20
  • 22

10 Answers10

242

Length() tends to refer to contiguous elements - a string has a length for example.

Count() tends to refer to the number of elements in a looser collection.

Size() tends to refer to the size of the collection, often this can be different from the length in cases like vectors (or strings), there may be 10 characters in a string, but storage is reserved for 20. It also may refer to number of elements - check source/documentation.

Capacity() - used to specifically refer to allocated space in collection and not number of valid elements in it. If type has both "capacity" and "size" defined then "size" usually refers to number of actual elements.

I think the main point is down to human language and idioms, the size of a string doesn't seem very obvious, whilst the length of a set is equally confusing even though they might be used to refer to the same thing (number of elements) in a collection of data.

Alexei Levenkov
  • 94,391
  • 12
  • 114
  • 159
gbjbaanb
  • 49,287
  • 10
  • 99
  • 143
  • 5
    So what's a "looser collection"? I'm not seeing the difference between size and count here. – Sophie Alpert Oct 01 '09 at 23:47
  • 35
    @ben: size = available slots, count = actual elements. size == count when the collection is full. – Steven Evers Oct 02 '09 at 02:10
  • @SnOrfus, that's still true, but I meant for collections that didn't have any meaningful contiguous elements, eg a map or a dictionary. You wouldn't say that a set had a length of 10 elements, there'd be a count of 10 elements in that. – gbjbaanb Oct 04 '09 at 00:04
  • 9
    Downvoting because `size()` refers to the number of elements in the vector, *not* its `capacity()`… at least in C++, which I think is the originator of `vector`s with `size`s. – Dave Abrahams May 10 '13 at 17:05
  • 12
    @DaveAbrahams - I never said that was the case. Read it again. I said it "tends to refer", I never even tried to make a specific statement that applied equally to all permutations of all collection classes in all languages. – gbjbaanb May 11 '13 at 00:03
  • 1
    Completely unrelated yet interestingly, a "length" (or measure) of a mathematical set is definitely not the same as its "size" (or number of elements in the set). Example: the interval [0,1] has a "length" of 1 but has a "size" of [aleph_1](http://en.wikipedia.org/wiki/Aleph_1#Aleph-one). – chharvey Aug 15 '14 at 04:01
  • 2
    @SnOrfus I think you've gone into the realm of "capacity" there. [`std::vector`](http://www.cplusplus.com/reference/vector/vector/) (C++) for example uses "capacity" and "size" where you use "size" and "count", respectively. Actually, *everything* in `std::` uses "size" for the current element count, even `std::string` (which provides "size" for template compatibility and a completely identical "length" for... human convenience I guess). – Jason C Nov 23 '14 at 20:22
  • So to be more Cocoa I should make `.numberOfElements()`? ;P – Ben Leggiero Feb 12 '16 at 23:50
30

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.

Michael Burr
  • 311,791
  • 49
  • 497
  • 724
  • 19
    While Length and Size are nouns, Count is also a verb, thus it could be interpreted as counting at runtime (O(n)) vs lookup a value (O(1)). – mbx Mar 15 '12 at 14:34
  • 1
    Indeed, that's exactly how it's used in LINQ: [Enumerable.Count](https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.count) – Edward Brey Sep 07 '17 at 16:41
12

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.

Mecki
  • 106,869
  • 31
  • 201
  • 225
  • Following your string analogy a file must have a `length`, but different storages might use different `sizes` to store its data. Java also think so in _java.io.File#length()_, but it seems like the rest of the world disagrees. – Ivan Balashov Jan 26 '17 at 12:24
  • 1
    @IvanBalashov I never used "length of file" in daily talk, for me a file has no length but a size and that's also what I wrote in my reply. Whenever we are talking of raw bytes we are talking of size IMHO and a file with no closer specific content is just a bunch of bytes. Length is usually not used for expressing byte count but to express a accumulation of elements stringed together (bytes are not elements to me, more the building blocks to form elements and they are also not "stringed together"). – Mecki Jan 26 '17 at 13:34
4

Count I think is the most obvious term to use if you're looking for the number of items in a collection. That should even be obvious to new programmers who haven't become particularly attached to a given language yet.

And it should be a property as that's what it is: a description (aka property) of the collection. A method would imply that it has to do something to the collection to get the number of items and that just seems unintuitive.

Corin
  • 1,909
  • 2
  • 30
  • 41
3

Hmm...I would not use size. Because this might be confused with size in bytes. Length - could make some sense for arrays, as long as they are supposed to use consequent bytes of memory. Though...length...in what? Count is clear. How many elements. I would use count.

About property/method, I would use property to mark it's fast, and method to mark it's slow.

And, the most important - I would stick to the standards of the languages/libraries you are using.

Paul Kapustin
  • 3,167
  • 4
  • 32
  • 44
  • So what about a DataBlock, just a bunch of bytes. Does it have a length or does it have a size? – Mecki Jul 29 '11 at 11:53
2

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.

Ken Gentle
  • 13,047
  • 2
  • 39
  • 49
  • Why are you "stuck" if it is exposed as a property? Properties have an underlying implementation that can change just as easily without breaking the interface. In fact, most languages implement properties as compiler generated get/set methods anyway...you just can't call them directly. – Scott Dorman Nov 18 '08 at 23:35
  • Which "most languages" are you referring to? C, C++, Java (just to name a few) don't do this. Ruby and Groovy I know do. Please note how I started the answer, too: "If 'property' implies ..." Why stuck? If the interface to the class changes, clients have to change (generally speaking) – Ken Gentle Nov 19 '08 at 00:46
1

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).

brightball
  • 875
  • 12
  • 10
0

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

EBGreen
  • 33,707
  • 11
  • 58
  • 80
  • And, what does it matter? What changes? Are we all going to write angry emails to the Java folks for picking two and being inconsistent? – S.Lott Nov 18 '08 at 23:45
  • 1
    That is my point. Why wonder which is better. It is what it is. – EBGreen Nov 19 '08 at 00:37
0

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.

Alexandr
  • 4,604
  • 3
  • 37
  • 65
0

Kotlin answer

from _Collections.kt

/**
 * Returns the number of elements in this collection.
 */
@kotlin.internal.InlineOnly
public inline fun <T> Collection<T>.count(): Int {
    return size
}
Kochchy
  • 175
  • 1
  • 1
  • 11