3 ms·
Not that I particularly care to start yet another meaningless bikeshedding argument about 0 vs 1 indexing but... I think that a proper high level language shoul
by newen 8y ago
Not that I particularly care to start yet another meaningless bikeshedding argument about 0 vs 1 indexing but... I think that a proper high level language should have arrays that start at 1.
It's only because of C and many programmers' weird tendency to take to heart statements from authority (Dijkstra) without any criticism that the 0-index meme is so popular now. 0-index is useful because it makes pointer arithmetic useful while pointer arithmetic is not often used in high level languages. 0-index is for offsets not counting. Vast majority of people use 1-index for counting. People who say everyone should use 0-index for counting sound really really silly to me (and frankly the vast majority of people; it's only because this meme is so popular in the programming world that they can somewhat get away with making this statement).
- erezyehuda 8y agoDijkstra gave pretty concise justification for 0-indexing, so I wouldn't reduce it down to "authority" for why people use it. While I'm personally comfortable hopping back and forth between conventions, is there any deeper justification for using 1-indexing? "Vast majority of people" seems like the same meme you're rejecting for 0-base.
- newen 8y agoI wouldn't say there's any deeper justification for 1-based indexing but neither is there for 0-based indexing. But convention is a pretty good reason for 1-based indexing. Regarding Dijkstra, I don't buy the specific argument he originally made about sequences much less the generalized argument that has become a meme now. His main argument is that a <= i < b (everything integers) is less "ugly" because b-a is the length of the sequence and a <= i < a is an empty sequence. And because you must use the above convention, 0 <= i < N is "nicer" than 1<= i < N+1. I don't buy his a <= i < b premise (b-a+1 is not exactly hard to write and similarly a <= i <= a-1 for an empty sequence is not impossible to write). There is nothing wrong with a <= i <= b. It is inclusive on both sides which is much less confusing than inclusive on one side and exclusive on the other (just look at python's ranges for how it can get confusing; range(5) means 0,1,2,3,4 ...seriously? Where did my 5 go? And range(a,b) meaning a <= i < b is so much more confusing than inclusive ranges). When you use inclusive ranges, you can actually see the numbers that are on both sides of the sequence. So, if we use this convention, then 1 <= i <= N is much "nicer" than 0 <= i <= N-1. Aside from my argument about inclusive ranges, the fact is that both inclusive ranges and 1-indexing have thousands of years of convention behind it's back. This should not be discounted since math is a language, language is for communication, and communication depends on conventions.