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?

closed as too broad by gnat, Bart van Ingen Schenau, user22815 Nov 30 '15 at 19:15

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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 ArrayLists have size(). And while there may be some logic behind the distinction, it isn't immediately apparent. – biziclop Nov 30 '15 at 12:47
  • 3
    There's a good answer to this on SO. stackoverflow.com/questions/300522/… – JamesT Nov 30 '15 at 13:00
  • 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

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.

  • 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 - stackoverflow.com/questions/1651301/… ) – 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.