4

It seems that the terms size and length are used interchangeably to describe how many bits, bytes or octets some data occupies, i.e. a length field in a data header is said to indicate the size of the data. Did I see this correctly? If not, how do the terms differ? Which term is most common for which usage?

user3998276
  • 187
  • 1
  • 2
  • 7
  • 1
    I suppose that `size` is definitely more appropiate if the data is not continuous. Also note that sometimes `count` is used to denote exactly the same. In many cases the usage is subjective. – Sulthan Nov 30 '15 at 12:46
  • I don't think there's any consistent use. Certainly in Java arrays have `length`, while `ArrayList`s have `size()`. And while there may be some logic behind the distinction, it isn't immediately apparent. – biziclop Nov 30 '15 at 12:47
  • @Sulthan That is certainly one crucial difference: a graph for example may have a size but it definitely doesn't have a length. – biziclop Nov 30 '15 at 12:48
  • 3
    There's a good answer to this on SO. http://stackoverflow.com/questions/300522/count-vs-length-vs-size-in-a-collection – JamesT Nov 30 '15 at 13:00
  • @JTolley that's not a very good answer in my opinion. It is making it sound as if there is some logic behind it, while in fact there isn't any. The use of the term `size()` for java collections was a bad choice. – Mike Nakis Nov 30 '15 at 13:04
  • I get that. But I think it's good to have an idea of what to expect from each term in advance. Particularly if you have two available. As a general rule I'd expect to get a byte count from size. – JamesT Nov 30 '15 at 13:08
  • @JTolley back in the days when I used to program in C there was a convention which said that "size" is the fixed amount of bytes allocated for something, while "length" is the (smaller) number of bytes actually used. So, go figure. – Mike Nakis Nov 30 '15 at 13:11
  • 1
    @MikeNakis "Size" for Java collections certainly makes sense, at least from a more formal perspective, length is something associated with ordered structures, not all collections have such a structure. Speaking the "length" of a set doesnt make much sense. – Hirle Nov 30 '15 at 14:09
  • 1
    @Hirle I see your point. That's reasonable. – Mike Nakis Nov 30 '15 at 14:56
  • You know what will really bake your noodle? Throwing "width" or "height" into the mix. How do you measure a two-dimensional array? A binary tree? –  Nov 30 '15 at 19:17

1 Answers1

6

This question is a duplicate of https://stackoverflow.com/q/300522/773113, but since that question is on stackoverflow, technically it is not a duplicate. (I tried to mark it as a duplicate, but I was prevented, because the other question is not on Programmers SE.)

So, here is what is happening: it is all a matter of convention, and it is all arbitrary. Different languages and environments have their own conventions, (sometimes even self-contradictory,) and you need to learn the conventions of the language you are using, and follow it.

In the old times when C ruled, "size" was the fixed number of bytes allocated for something, while "length" was the smaller, variable number of bytes actually in use. Generally, "size" stood for something fixed, while "length" stood for something variable. But it was still not uncommon for someone to say that "a machine word is 32 bits long" instead of "the size of a machine word is 32 bits", despite the fact that the number of bits in a machine word is, of course, very fixed.

And then comes java, which has arrays of fixed size, but their size is returned via a length property, and strings of fixed size, but their size is returned via a length() method, and collections of variable size, but their length is returned via a size() method. So, java decided to turn things around.

Then came C#, which keeps the term "length" for stuff of fixed size, but for variable size stuff it uses the term "count", which would be perfect, if it was not for the unfortunate fact that besides being a noun it is also a verb, which can be taken to mean that when you get the "count" of a collection, the items in the collection will be counted one by one. (O(N) instead of O(1).)

So, go figure. There is no definitive answer, be sure to carefully study the documentation of the system that you are dealing with, and to understand the precise definition of the terms "length" and "size" within the context of that system, and be even prepared that there may be no precise definition of these terms, and they may be used interchangeably and arbitrarily.

Mike Nakis
  • 32,003
  • 7
  • 76
  • 111
  • 2
    Specifically, in C#, `foo.Count` should be the noun with O(1) but `foo.Count()` can be the verb with O(N) ( unless the receiver implements an interface with a `Count` property - http://stackoverflow.com/questions/1651301/c-sharp-count-extension-method-performance ) – Pete Kirkham Nov 30 '15 at 14:07
  • 1
    This is nitpicking, but `size` is a verb too, albeit one used far less often. – biziclop Nov 30 '15 at 14:37