4 ms·
One-based indexing is an example of this. It's always argued as being familiar, as if people learning to program couldn't deal with adding 1.
by IshKebab 2y ago
One-based indexing is an example of this. It's always argued as being familiar, as if people learning to program couldn't deal with adding 1.
- crowselect 2y agoMeanwhile off-by-one errors are ludicrously common… People can learn to deal with adding 1, but it’s still a cognitive encumbrance that loads us down and makes life harder. Not that I’m advocating for one-based indexing. At this point consistency is more important than whatever the easier solution is.
- littlestymaar 2y ago> Meanwhile off-by-one errors are ludicrously common… Off by one errors in general have nothing to do with being 1 or 0-based indexed. In French off-by-one error is called “the post and fence problem” because there's always one more post than piece of fence (unless your fence makes a loop). It arises because it's easy to confuse the number of intervals between elements and the number of elements (which is also: do I need to include the bounds or not). 1-based indexing isn't going to help at all in the general case (it will help for the most basic error which is getting the last element of an array with `a[a.length]`, but that's it)
- IshKebab 2y agoOff-by-one errors are more likely with 1-based indexing because they don't have the nice properties of 0-based/right open intervals that mean you don't have to add or subtract 1 all over the place. For example what is the index of pixel X,Y in an image buffer? X + Ystride right? Nope! It's X + (Y-1)stride. What's the range of the Ith block of N elements? [in, in + n)? Nope! It's [(i-1)n+1, in]. I have to deal with that nonsense in Matlab all the time. Nightmare.
- Sharlin 2y agoOne-based indexing simply has its own set of off-by-one bugs.
- aqueueaqueue 2y agoI imagine zero based indexing is because resources were scarce and number of bits low in the bad old days. Why waste that zero index. From there is was backwards compatibility. If machine code uses zero then assembly inherits that, them C and everything else. Even JS months!
- tcoff91 2y agoNo, zero based indexing just is based on how arrays actually work. It has nothing to do with resources being scarce. You have an address for the beginning of the array and an offset. The first element has no offset so you use zero. Therefore you naturally end up with zero indexing.
- aqueueaqueue 2y agoYeah but why wasn't that converted to human 1 based indexing at the machine code level or assembly in early machines? Maybe because they didn't want to "hide the bicycle gears"?
- IshKebab 2y agoNo, it's because it doesn't make any sense to do that. It would add a ton of complexity for no benefit.
- robertlagrant 2y agoIt does make some sense: if I have some apples, and I ask a child to count them, they'll address each one starting with the number 1, not the number 0.
- IshKebab 2y agoIndexing/addressing is not counting though. Look at a ruler. It doesn't start at 1.