Hacker News new | ask | show | jobs
by bsder 3794 days ago
> Why should we use from-0 indexing for all eternity? Just because it's always been that way? This is a real question if you have the answer.

Well, we obviously can use 1-based indexing. Ada/VHDL allows you to define the ranges of your indicies even beyond 0 or 1 based (very useful in fixed point mathematics where you sometimes want to index your bits by a negative power of 2).

The big disadvantage is that you lose the algebraic properties of mapping to the ring of integers modulo N when your indicies are 1-based.

This leads to a bunch of off-by-one bugs in various places. One of the most pernicious is that while you can get to the next index with "next = prev % N + 1" you actually have to do "prev = (next - 1) % N". Note that the previous REQUIRES the parentheses due to precedence of operations.

0-based may be convention, but it isn't ill thought out. FORTRAN(1-based) and C(0-based) coexisted a LONG time. Pascal was(is?) 1-based. Most people had experience with both.

People who designed programming languages CHOSE the 0 convention. Overwhelmingly.

1 comments

After seeing the light with Ada I never want to go back - indexing based on enums, modulos, etc. just makes so much more sense (as long as you're using something strongly typed)