Why 1-based indexing is *OK*.

Some programmers markedly object to the presence of 1-based array indexing in programming languages. This happens every time a new language appears which uses it, the latest being Julia. But why?One of the arguments is because 0-based indexing is more natural. Really? Ever start counting anything at 0? How many eggs are in an egg carton – ahhh 12?  But when I take out the first, is it egg number 0?

Starting array indexing at 1 *is* natural for counting. So an array with 100 elements will be indexed from 1 to 100, not 0 to 99. Zero-based indexing is more of an artifact of C than anything else. Languages like C and C++ use pointers to store data, so it makes more sense to have their indices start at zero. Finding an array element in contiguous memory is then easy.

Element address = address of array + index × (size of type)

Arrays basically simulate matrices and vectors in mathematics, which used 1-based indices before the computer era, and still do in many cases. Both Fortran and Matlab use 1-based indexing, as do most matrices and vectors. Pascal and Ada allow for indexing to be generic. You want array indexing from -7 to 7… no problem – maybe generic indexing is the best approach? Some types of array manipulation are easier using zero-based indexing, and in certain circumstances zero-based indexing produces cleaner code.  Some of the reason why many people like 0-based indexing is the same reason they get nauseous looking at a Cobol program – they are use to programming predominantly in C-like languages, where 0-based indexing is the norm.

Dijkstra in his article “Why numbering should start at zero“, argues 0 ≤ i < N is a “nicer” range than 1 ≤ i < N+1 and doesn’t really care for 1 ≤ i ≤ N. This really doesn’t make much sense – there are no aesthetic qualities associated with conditional statements. Guido van Rossum  gives a rationale for why Python uses 0-based indexing, which boils down to being “swayed by the elegance of half-open intervals“.  He does make a good point that splitting a string into three components using a[:i], a[i:j], and a[j:] is nice.  But one also has to like the fact that i:j implies from i to <j.

Sure, for veteran programmers, 0-based indexing is no big issue, but for novice programmers, 0-based indexing is a huge usability issue. An array has 10 elements, but the 1st is actually the 0th, and the 10th is actually the 9th. Mathematically-focused languages like Matlab, Fortran, and Julia are okay having 1-based indexing. To each their own. Look some people like 0-based indexing and others like 1-based. There may be no right answer… and maybe that little bit of extra brain work in 1-based languages isn’t such a bad thing.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.