The use of 1 or 0 for indexing

Not that I’ve ever seen? :wink:
The reasons I’ve ever seen 0-based is because of math, the reasons of 1 based have been purely for humans, which then tend to prefer 0 once they start dealing with the math of it. ^.^

How about one of the most basic examples, would you prefer to do (C/C++ syntax to keep it much shorter) arr[i + j + k], or would you prefer to do arr[i + j + k - 2], because the latter is what you have to do with 1-based indexing (and yes I know it’s actually an offset, but the term ‘index’ is the common term now, english changes over time annoyingly). Of course there is Djikstra’s paper on it from a mathematical perspective (ignoring the hardware performance aspects). Studies showing fenceposts errors are significantly more common with 1-based indexing. There is less calculations for the processor to do with 0-based indexing (which can be substantial on such architectures as you can index from memory to memory without needing to bring it to the CPU to sum a 1 to it first). Etc… etc… etc… I’ve never seen a good argument for 1-based indexing as of yet (like some people show things like for(int i=1;i<=len;++i)... when even the 0-based for(int i=0;i<len;++i)... is still shorter by a character and matches the math better). And somehow +1’s get scattered all over the place when I look at 1-based programming languages (Lua especially) that just wouldn’t happen with 0-based indexing. 0 is the natural base number.

So yes, 1-based array indexing is inherently broken by design and mathematically unnatural. :wink:

5 Likes