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

Re:The Nightmare Bicycle by aqueueaqueue on Wednesday March 05, 2025 @03:13AM (#43264116)
I 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!

Re:The Nightmare Bicycle by dsego on Wednesday March 05, 2025 @03:48AM (#43264395)
It's pointer arithmetic I presume, a memory address + zero is still the same memory address, and then you increment to move forward.
Re:The Nightmare Bicycle by leni536 on Wednesday March 05, 2025 @03:39AM (#43264318)
Assembly doesn't really have indexes, as it doesn't have arrays. It's all addresses and offsets. Offsets are naturally 0-based.

Everywhere else, indexing is just a convention, even in natural language and math.

Re:The Nightmare Bicycle by tcoff91 on Wednesday March 05, 2025 @03:28AM (#43264224)
No, 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.
Re:The Nightmare Bicycle by aqueueaqueue on Wednesday March 05, 2025 @03:32AM (#43264257)
Yeah 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"?

Re:The Nightmare Bicycle by IshKebab on Wednesday March 05, 2025 @04:10AM (#43264520)
No, it's because it doesn't make any sense to do that. It would add a ton of complexity for no benefit.
Re:The Nightmare Bicycle by robertlagrant on Wednesday March 05, 2025 @05:51AM (#43265106)
It 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.
Re:The Nightmare Bicycle by IshKebab on Wednesday March 05, 2025 @09:13AM (#43266636)
Indexing/addressing is not counting though. Look at a ruler. It doesn't start at 1.
Re:The Nightmare Bicycle by robertlagrant on Wednesday March 05, 2025 @09:49AM (#43267199)
Yes, indeed. But when indexing to an element in an array you aren't measuring from a zero (well, in memory you are, but not conceptually). You're conceptually pointing at an apple.
Re:The Nightmare Bicycle by IshKebab on Wednesday March 05, 2025 @12:50PM (#43269867)
> But when indexing to an element in an array you aren't measuring from a zero (well, in memory you are, but not conceptually).

Yes you are!! In memory, and conceptually. You're conceptually moving your finger 0 spaces from the start of the apple array.

Re:The Nightmare Bicycle by robertlagrant on Wednesday March 05, 2025 @02:54PM (#43271450)
I think maybe it's just hard to unlearn the offset mentality. Of course you can convert a direct index mentality to an offset mentality, but I don't think anyone is pointing at the first item in a row of items and thinking "that's a zero offset from the start of the number of items". They're thinking, "That's item number 1".
Re:The Nightmare Bicycle by IshKebab on Thursday March 06, 2025 @03:07PM (#43284574)
> They're thinking, "That's item number 1".

Yeah that's because the distinction between indexing and counting is pretty much irrelevant to every day use, and to maths where you can just hand-wave syntax. So people are very used to doing it "wrong".

That's what most of the issue with this debate is. Indexing from 1 is wrong, but people are soooo used to it they just can't get over what they think is the "normal" way.

Re:The Nightmare Bicycle by robertlagrant on Monday March 10, 2025 @08:57AM (#43320102)
> Yeah that's because the distinction between indexing and counting is pretty much irrelevant to every day use

No it's not. Telling the time or measuring something is 0-indexed. But identifying a thing in a list is 1-indexed.

Re:The Nightmare Bicycle by aqueueaqueue on Wednesday March 05, 2025 @03:53PM (#43272221)
Thanks that made it click.

While I understand the CS concepts, I have never really thought about the developer UX before.

Re:The Nightmare Bicycle by mkl on Wednesday March 05, 2025 @04:26AM (#43264605)
No. Fortran (1957) uses 1-based indexing by default. Probably because matrix and vector indexing in maths is usually 1-based.