4 ms·
In contrast, indices starting at 0 always baffled me in other languages. In pretty much any real-world scenario, you always start at 1 (it's literally "the fir
by mdorazio 6y ago
In contrast, indices starting at 0 always baffled me in other languages. In pretty much any real-world scenario, you always start at 1 (it's literally "the first element" as in "1st"). It always seemed to me like language developers for some odd reason decided to apply spatial coordinates to arrays.
- bo1024 6y agoIt's not surprising: in these languages like C, arrays were essentially just pointers (i.e. spatial coordinates).
- Pelic4n 6y agohttps://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- ufo 6y agoThis always seems to pop up in these discussions but I don't think I am terribly convinced by his argument. For example, he doesn't mention that option (c) is better than option (a) if you need to iterate backwards. In that case option (a) becomes option (b), which everyone can agree objectively sucks :)
- pansa2 6y agoYes! Reverse ranges in Python are awkward for this reason. The reverse of `range(0, 5)` (i.e. [0, 1, 2, 3, 4]) isn’t `range(5, 0, -1)`, it’s `range(4, -1, -1)`. I think it would be better if the end-points were “low” and “high” rather than “start” and “stop”, so the reverse of `range(0, 5)` would simply be `range(0, 5, -1)`.
- bad-apple 6y agoI think Rust has another good alternative to this, although it might be cheating a little: you can just write (lo..hi).rev(). That way, the common case of going forwards is nice, and the case of going backward isn't much worse.
- bspammer 6y agoLet's say you want every 5th element of an array, with zero indexing your code can be very elegant. Your indexes are 5 * 0, 5 * 1, 5 * 2. With 1-indexing you have to do (5 * 0)+1, (5 * 1)+1, (5 * 3)+1 etc.
- dnautics 6y ago0-based array indexes make sense when what you are working with is laid out in memory from an offset. Honestly, if we cared about purity literally everything else (linked lists, etc) should be 1-indexed, but, stuff like composition and abstraction turned out to be more important, so we're stuck with 0-based indexes for everything.