Live data from Hacker News

Why We Start Indexing from 0 in Computer Science

cs.utexas.edu

1–10 of 38 posts

Re: Why We Start Indexing from 0 in Computer Science

#5
post #3
post #2

Easier to read: http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EW...

Although this is indeed much easier to read, the handwriting in the original is beautiful. Love the 'f'.

You are not the only person to think so, it has been turned into a font multiple times (for example, here: http://www.fonts101.com/fonts/view/Uncategorized/34398/Dijks...).

Re: Why We Start Indexing from 0 in Computer Science

#6
"an element's ordinal (subscript) equals the number of elements preceding it in the sequence."

That is going to make the "the 2nd item is number 1" thing SO much easier to explain when teaching. Thanks! (And it's good to have the rest of the reasoning there to answer the particularly curious students...)

Definitely falls into the "wish I'd thought of that" and "wish I'd heard someone say it that way 5 years ago" camps.

Re: Why We Start Indexing from 0 in Computer Science

#7
I suspect the real reason has more to do with the pragmatics of machine code.

It's extremely common to want to refer to some dynamic offset of a fixed location in memory, like if you have an array of equal-length items stored consecutively. If you have two literal addresses called Start and Index, you'd like to be able to say something like "Start[Index]" and have it mean "Read from Index, and whatever number is there, move that many spaces forward from Start, and give me that". It's handy that if Index is a cleared region of memory, you can say "Start[Index]" and have it mean the same thing as just "Start". More importantly, if "Start[Index]" only meant "Start" when Index was 1, that would imply that if Index were 0 that Start[Index] would refer to the spot right before Start, which is not only useless but outright dangerous.

Re: Why We Start Indexing from 0 in Computer Science

#9
I'll repeat my comment from a different thread:

"I've never really agreed that numbering should start at zero (or at one), I think people use indices way too much and it gets in the way of clarity. I much prefer whole array or list operations with no fiddling with indices and off by one errors. I like Haskell's array API, for example. You can index by whatever is natural in each case and you can always get the whole list of valid indices for an array x by using "indices x". I think that's much nicer, and it's also what D is doing: instead of having a(n implicitly paired) starting and ending iterator to indicate a range of positions like the C++ library does, D's library uses a range object. Basically I'm saying you can represent a range of indices as a pair of indices, one pointing to the first valid position and the other pointing past the last valid one, but that's an implementation detail you don't need to expose, make a range abstraction and you'll be happier."

"I should also point out that mathematicians don't number at zero unless there is some advantage (the default is to start at one), but more importantly, the preferred style in mathematical arguments is to avoiding fiddling with indices as much as possible (since it's so easy to mess something up working at such a low level of abstraction)."

Re: Why We Start Indexing from 0 in Computer Science

#10
post #7

I suspect the real reason has more to do with the pragmatics of machine code. It's extremely common to want to refer to some dynamic offset of a fixed location in memory, like if you have an array of equal-length items stored consecutively. If you have two literal addresses called Start and Index, you'd like to be able to say something like "Start[Index]" and have it mean "Read from Index, and whatever number is ther…

Right, it makes sense when dealing with machine code and when making the compiler do extra work for you (converting one-based to zero-based indexing) would be too much work for the compiler implementer or is too slow (like on machines back in the 70s and 80s) or it would abstract too far from what is happening underneath the hood, possibly leading to errors.

But like many features of programming languages and operating systems (case-sensitivity, global menu bar, python's colon...), zero-based indexing is something that made perfect sense in the context and time in which it was originally designed and implemented, but it's really no longer necessary in most cases today, and only survives because of tradition and blind cargo cult-like copying of what others did before.

Post reply on HN