3 ms·
personally i think 0-indexing is weird. it makes sense as a pointer offset in lower-level languages. but in higher-level languages we have the opportunity to i
by tokipin 18y ago
personally i think 0-indexing is weird. it makes sense as a pointer offset in lower-level languages. but in higher-level languages we have the opportunity to index the first element by the number 1, the second by the number 2, and so on instead of having to do that little -1 every time which is something that has to be learned
- enqk 18y agoThere is a short note by Dijkstra about this http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EW...
- tokipin 18y agothat's really weird. was it written at a time when most programming was for scientific purposes? i don't know my comp history if i parsed it correctly, he is saying that things should be 0-indexed because a particular mathematical notation corresponding to it is more sensical, but i don't see what mathematical notation has to do with programming notation, both of which are man-made, arbitrary, and for different purposes, such that reasoning about either with respect to the other is imo fallacious he compares subscript ranges in a notation where 0-indexing appears to be better, but choosing a different notation (such as b in his examples) makes it look better for 1-indexing ( 0 < i <= N versus -1 < i <= N-1 )
- critic 18y agoHere is the important part: Extensive experience with Mesa has shown that the use of the other three conventions has been a constant source of clumsiness and mistakes 0-based indexing is not something you can rigorously prove to be better.
- silentbicycle 18y agoOne advantage of 0-based is that using array[mod(i, length)] works. It mostly seems to me like 0-based vs. 1-based is a relatively arbitrary decision, though. Each just leads to fencepost errors in different places.
- tokipin 18y agoif i read correctly, that's talking about the mathematical notations, not arrays or the indexing of such, of that programming language and while we're considering sources of "clumsiness and mistakes," 0-indexing is an exceptional candidate, from my experiences helping a friend learn C++ and my own memories treading through those early days i look at it from the perspective of usability
- enqk 18y agoIn C++ and C the iteration idiom almost always looks like: for (elementIndex = first-element; elementIndex < indexAfterLastElement; elementIndex++) { ... } That is, writing the equivalent of the mathematical notation: firstElementIndex <= elementIndex < indexAfterLastElement Two advantages of that idiom: The first one is that indexAfterLastElement - firstElementIndex equals the number of elements being scanned. The second advantage (which is the most debatable, and personal) is that the initial boundary condition is trivial (it's the first element) and the end boundary condition is natural i.e. just look past the element you are looking at whether there is a next one. That's what Dijkstra is talking about when telling the example of the student who would not look past the end of the page. I used the epithet natural because for most of the objects we interact with, the end is not part of the object, rather it is the transition in between the last element and nothingness. Now that said (in too many words) if you compare 1-based arrays and 0-based arrays you have: for (i = 0; i < N; i++) { ... } for (i = 1; i < N + 1; i++) { ... } Both would be acceptable. However 0 based arrays have the advantage of giving i two meanings: 1. the index of the current element 2. the number of elements scanned so far. At the end of the iteration, i also conveniently contains the number of elements having been scanned. Which can be useful in algorithms where there is an extra condition which might let you leave the iteration early. This to me is the most important part, zero-based indexing is more expressive.