3 ms·
Not convinced that is the case. I do not pepper my code with +1 and -1 while using 0-based indexing. I can think of a lot of cases where I would do it for 1-ba
by user-the-name 6y ago
Not convinced that is the case. I do not pepper my code with +1 and -1 while using 0-based indexing.
I can think of a lot of cases where I would do it for 1-based indexing, but very few for 0-based indexing. If you want to make the claim that there similarly many, you're going to have to come up with some substantial examples.
- tsegers 6y agoThe -1 with 0-based indexing occurs very frequently when using the amount of elements in some collection to derive boundaries of the indexes, which will range from 0 up to and including n-1. Grepping for "- 1" in my code folder returns mostly expressions that look like 'length/len/size - 1'.
- user-the-name 6y agoThis can happen, but it is fairly rare, especially if you favour handling ranges as either (start, length) tuples, or as half-open intervals, with the end index being one past the highest index. This representation simplifies many things, many of them unrelated to 0- or 1-based indexing.
- tsegers 6y agoWhile that is true, ranges and intervals are not native constructs in quite a few languages.
- ufo 6y agoOne of the more common examples is if you need to iterate backwards. It's awkward because the [0,N) interval becomes (N,0]. for(i=N-1; i>=0; i--)
- naniwaduni 6y agoNotably, this requires you to use a signed index type, which is absurd. The alternative is to use the so-called goes-to operator: for (i=N; i --> 0;). But this actually relies on the postincrement semantics, which is a huge wart.