0-based vs 1-based indexing

New programmers find it hard to understand what good comes from counting from zero instead of one. In our everyday life we count from 1, why not do the same in computer programming?

The answer is very simple for those who started their computer programming life from low level languages (Assembly, C, C++). It has to do with memory management something very common for them.

In high level languages (PHP, JavaScript, Java, C#) you almost never need to access memory directly. You use variables and the system does the rest. In low level programming you need to use pointers, a special type of variables, to get access to memory. Pointers are plain integers that represent a precise point in memory. Memory is massive so you need those pointers to find your way. Low level programming uses pointers to point to one specific byte in memory that usually denotes the start of a memory block you are interested in. If you want to access the rest of it, you make progressive steps from the pointer.

Now that you have the background, imagine that p is a variable that points to the memory block you are interested in. p points to the first byte of the block. If you want access to the second byte you use p + 1, if you want access to the third byte you use p + 2, and so on. The big picture p + 0, p + 1, p + 2. That is how 0 is so special in arrays.

You may still wonder why not change that system, since its roots are only in low level programming languages. Well, you are free to do as you wish in your code. However, like it or not, the 0-based system is commonly used and that is not easy to change because you want it changed.

If you're planning to use a 1-based system instead, a piece of advice is to be consistent. Complicated programs can run into all kinds of trouble if your programming habits change.

One more advice is to leave non-programmers out of this 0-based habit. 0-based is good for us, programmers. It makes absolutely no sense for everyone else and it can only complicate their work. So keep 0-base for your programs, use a 1-based system for anything else.