28 ms·
> "This is the way we've always done it" is not a convincing argument. You're fighting a straw man; that isn't the argument. 1-based indexing is elegant for ma
by john_b 13y ago
> "This is the way we've always done it" is not a convincing argument.
You're fighting a straw man; that isn't the argument. 1-based indexing is elegant for many mathematical uses. Technical computing often implements algorithms that are best written down using mathematical notation. In such a case the largest conceptual difficulty is not the origin of the index but the successful translation of the algorithm. To minimize the possibility of error, the original indexing (especially when it involves nontrivial mathematical maps) is often preserved.
> Dijkstra makes all of the convincing arguments for why 0-based is better than 1-based.
He essentially makes only one point. Namely that, when using 0-based indexing we can easily determine the length of a sequence by only its upper index. Though it's not a profound observation since the index bias is zero, it often gets treated as such.
Either way, it's a convention. A competent programmer should be able to handle any well-specified indexing convention (including those starting at negative indices). Different circumstances will confer different benefits on different conventions, and you should use the one that's most appropriate for the task at hand rather than religiously promoting The One True Way (TM).
- bjourne 13y agoThe big drawback is that with 0-based indexing you can reference elements relative to the tail. If 0 is the first index and -1 the last index, then 1 is the second index, -2 the second last, 2 the third, -3 the third last and so on. 1..-1 would be all elements from the second up to including the second last. I don't see how you could have indexing from the tail being internally consistent if the arrays are 1-indexed.
- astrieanna 13y agoJulia uses the keyword `end` to indicate the last index. 1 and end for the first and last, 2 and end-1 for the second and second to last, and so on.