Every now and then I end up in arguments with people at what bound an array index should start. The usual options are one-based or zero-based indexing, but there are enough languages that allow defining a different starting index of an array.
In my opinion, in language where you can define the starting index, it should depend on the problem to be solved. I prefer languages that allowing for an arbitrary starting index as they allow specifying the problem domain best.
Often the argument used by others is “zero” as starting index is that it is more efficient. My counter arguments usually are that
- with compiler optimised iterators nowaways, indexing often is moot
- people learn to count starting with one for various reasons (for one because they use their digits to count) which makes it hard for them to unlearn and always count from zero, causing off-by-one mistakes doing so
- counting from 1 to 10 is acquired at age 2 to 3 years old, the concept of zero or empty about a year or two later
- the concept of zero is way younger than the concept of one, see 0: history and 1: history on Wikipedia
- zero based indexing is only more efficient in languages supporting multidimensional indices that can be folded in a single dimension and that the transofmration between the single folded dimension and the equivalent multidimensional index is easier when both are zero based
- for single dimensional arrays, there is no efficiency argument as most compilers and interpreters will optimise that away
- there are plenty of programming languages not having a default starting index of zero
I usually forget the last list, so below here are a few links with sources and discussions.
Before reading the list, always remember: code is about representing problem domains and problem domains are not about math, compilers are.
Oh, and on the concept of zero, this is a good read: [Wayback/Archive] What Is the Origin of Zero? | Scientific American
Read the rest of this entry »