News for hackers. Stuff that matters.
Re:The Nightmare Bicycle by crowselect on Wednesday March 05, 2025 @02:43AM (#43263935)
Meanwhile 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.

Re:The Nightmare Bicycle by IshKebab on Wednesday March 05, 2025 @04:09AM (#43264515)
Off-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.

Re:The Nightmare Bicycle by Sharlin on Wednesday March 05, 2025 @04:10AM (#43264522)
One-based indexing simply has its own set of off-by-one bugs.
Re:The Nightmare Bicycle by littlestymaar on Wednesday March 05, 2025 @03:17AM (#43264147)
> 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)