5 ms·
I don't care if computers think in 0s and 1s or if arrays should start at 0 because it is the way computers think. Computers were made to serve us and as such
by TweedHeads 17y ago
I don't care if computers think in 0s and 1s or if arrays should start at 0 because it is the way computers think.
Computers were made to serve us and as such they should translate all their inner thoughts to more human consumable data.
Five is 5, not 101.
So no, numbering should start at ONE, even if most programmers have already been hardwired to start counting from zero.
- thunk 17y agoBut he was making the point that there are valid human reasons for zero-based indexing - e.g. keeping the lower bound within the natural numbers; having the upper bound be the number of elements in the preceding sequence, etc. The "way computers think" doesn't enter the discussion.
- TweedHeads 17y ago"keeping the lower bound within the natural numbers; having the upper bound be the number of elements in the preceding sequence, etc." Read your post and understand the same can be said of using one-based indexing.
- thunk 17y agoThat's false. You'd be forced to sacrifice the exclusive upper bound, which would then force you to sacrifice the difference between the upper and lower bounds being the number of elts in the collection. Unless the lower bound was exclusive, in which case it could be an unnatural number. No, the only way there's no lump in the carpet is his way.
- scott_s 17y agoWe have this convention for the benefit of humans. Indexing starting at 0 has the convenient property that when we modulus an arbitrary number by the size of a range, the result is a valid index into that range. This comes up when implementing things that map a larger set onto a smaller set, such as buffers or hash tables. We could still achieve the same effect if we started indexing at 1, but it would require more code, and it would be less clear.
- TweedHeads 17y ago"We could still achieve the same effect if we started indexing at 1, but it would require more code, and it would be less clear." Really? How about dealing with strings? "123456789" how many chars we have? 9 char[1] = 1 char[2] = 2 : char[9] = 9 Now, let's try the C approach: "123456789" char[0] = 1 char[1] = 2 : char[8] = 9 where is char "1"? at zero index! how many elements we have? last index plus one! see? there is already confusion in place, or as you say, more code and less clear...
- Locke1689 17y ago"0123456789" ch[0] = 0; ch[1] = 1; ch[2] = 2; : ch[9] = 9; ch[1] = 0; ch[2] = 1; ch[3] = 2; ....
- mononcqc 17y agoPlease re-read his question: "how many chars we have?" "how many elements we have?" ch[1] = 0; ch[2] = 1; ch[3] = 2; ... ch[10] = 9; This is about having the Nth element of the array with the index N rather than N-1.
- scott_s 17y agoThat's not code, that's English. When we start indexing from 0, the only time we need to do a +/- 1 is when we need to index the last element. All other times (iterating, mapping) we don't need to. Also, starting indices at 1 would change the idiomatic iteration to: for (int i = 1; i <= SIZE; ++i) I find that the <= and >= operators add more to my cognitive load than the strict relation operators < and > because there's an or in there. I don't find the alternative to that idiom any better: for (int i = 1; i < SIZE + 1; ++i) Your original argument was that we should make it easy for humans to understand, not computers. I think that starting indices from 0 is easier for humans to understand because it simplifies the code we must write and read.
- TweedHeads 17y agoNo, your language limitations don't have to force the rest of the world to accept them. As I alreay said, some languages use the simpler construct: For i=1 to N And C could easily use: for(i=1;i==n;i++) just by changing the way the loop condition works. So as you say, it is all about the language.
- TweedHeads 17y agoLook at your hand and start counting your fingers while naming them: [1] thumb [2] index [3] middle [4] ring [5] pinkie so we have a simple range [1..5] which can be represented in so many ways in computer programs: for i=1 to 5 print finger[i] for(i in [1..5]) print finger[i] my first finger[1] is my thumb my last finger[5] is my pinkie how many fingers we have? as many as the last index in the list: 5 - now, C programmers like to count this way: for(i=0;i<5;i++) print finger[i] where finger[0] is thumb and finger[4] is pinkie how many fingers we have? as many as the last index in the list plus one: 4+1 how human-like! And that's why you get so messy when trying to get the string position of a substring: if(pos>-1) exists, since pos=0 means the substring is in the starting position again, how human-like! But how dare I argue with C programmers without being burned at the stake?
- maweaver 17y agofor i=1 to 5 print finger[i] for(i in [1..5]) print finger[i] You're using an inclusive upper range here. Not that that's necessarily wrong, but it isn't standard for the reason Dijkstra mentioned (how many fingers do I have? 5 - 1 = 4. Oops.)
- TweedHeads 17y ago"how many fingers do I have? 5 - 1 = 4. Oops." Upper-Lower+1, easy. Whenever the lower is 1, just the upper is enough. In your case you go from 0 to 4, how's that different from how many fingers you have? 4-0=4 Oops.
- gloob 17y agoCongratulations; you have successfully introduced a whole new way to have off-by-one errors. People are used, due to forty years or more of tradition in the great majority of major languages, to zero-based indexing. Tossing that away Because It's Inelegant won't do much good to anyone, especially when you take into consideration the fact that an awful lot of math is more convenient with 0-based indexing.
- jongraehl 17y ago