5

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?

1 Answers1

7

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,803