Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Has this argument been rehashed much:

If you use 1-based indexing, you can't iterate over a list of maximum length with a simple loop. The normal loop condition is `while i < len` (0 based) or `while i <= len` (1 based). If len is maximal, the second one probably has an overflow bug in the loop body that turns it into an infinite loop or a crash.



This is actually symmetrical. When you use 0-based indexing, you can't count down with a simple loop: you need to either extend your index type to include an invalid -1 or apply the infamous evaluate-then-decrement semantic. In practice, this means people often index with a signed integer, which is absurd.

And lest you think that counting down is unnatural—there are quite a few advantages in comparing to zero on every iteration instead of a computed upper bound, like that you can often roll the condition check in with the decrement instead of having to do a separate comparison.


I'd also argue that counting down is more common than having a list with UINT_MAX elements.


Ah that's a very good point.


A list of length equal to whatever maxint is is something you would have to set up on purpose. (If it were naturally occurring and unpredictable, then you would need to handle the much more common case where the length of the list is greater than maxint, too.)

The fact that a weird situation you set up intentionally needs special-purpose handling doesn't seem odd or unreasonable to me.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: