Peter said:
That depends on how you are interpreting the index.
You're not talking about indices there. You're describing ordinals.
Did you mean cardinals?
The FIRST element of a zero-based array is at INDEX 0. But it's still
the first element, and if I'm counting elements, I'm still going to
start at one. It's ORDINAL is 1.
"The finite ordinals (and the finite cardinals) are the natural numbers:
0, 1, 2" - Wikipedia.
Curious but I'm not sure I care who is right, To me, *ordinals* are
numbers used to define the *order* of a bunch of things.
If I had a dog a cat and a monkey, I could presumably assign them the
respective ordinals 327, 77 and 101. I do feel I could use those
ordinals to order those animals.
In case you take me literally: I am not advocating that Java should be
changed to use those numbers as the first three ordinals (or indices)
On the other hand, if I've got three rabbit hutches, and someone asks me
how far from the first rabbit hutch the hutch for rabbit #3 is, the
answer is "two rabbit hutches". That is, the index of the hutch is 2,
not three.
Hang on, you can't use distances a-priori to decide a one true system of
indexing. I'm pretty sure that people had multiple rabbit hutches prior
to the widespread use of the concept of zero. The absence of a formal
zero in their counting system presumably didn't prevent them from
working out which hutch was (say) three hutches from the second.
Let me try substituting 5,9 and 4 for your 1, 3 and 2:
On the other hand, if I've got nine rabbit hutches, and someone
asks me how far from the fifth rabbit hutch the hutch for rabbit #9
is, the answer is "four rabbit hutches". That is, the index of the
hutch is 4, not nine.
No, your logic is unpersuasive to me.
For some people, perhaps. But only if they insist on continuing to use
ordinals as if they were indices.
People make mistakes. That's what I'm talking about. People have habits,
sometimes those habits are suboptimal for a new situation. It takes time
to change them. It isn't a question of anyone *insisting* on doing
something inappropriate.
In any case, once a programmer has
developed the proper habits for a zero-based language, it's every bit as
problematic for them to switch to a one-based language as it might have
been for them to learn the zero-based language in the first place.
I'm happy for us to disagree on this subjective point. At least I don't
have any firm evidence, do you know of any?
And really, doesn't it make more sense for most languages to cater to
the _experienced_ programmer?
You seem to be broadening the discussion into a new area, I wasn't
advocating that Java should be any different than it is.
I mean, sure...sometimes you want a
language that is tailored for beginners. But most of the time, you want
a language that is designed for reasons other than a need to hold a
beginner's hand. Designing a language to fit the often-awkward and
always-inconsistent human experience seems like a sure-fire recipe for a
language poorly suited to instructing computers.
I was under the impression that all designers of high-level programming
languages do indeed try to do the latter to a large extent.
I'm glad I can write my numeric literals in decimal.
The mature programmer, with enough experience in zero-based arrays,
never writes "n - 1" as the end condition. Instead, they iterate from 0
inclusive to n exclusive (i.e. "for (int i = 0; i < n; i++)"). It's
quite natural, especially once you understand the difference between an
ordinal and an index.
You make a good point. I do that myself. The OP didn't. I thought I was
discussing what novices do, not what experienced programmers do.
Not bad advice. But off-by-one errors can be present whether one's
using zero-based or one-based indices.
True.
Frankly, it's been quite awhile
since I've written an off-by-one error myself,
Frankly, when I wrote "new programmer" and "trainee programmer" I wasn't
thinking of you Pete.
but when they do happen,
it's not because of the index base. It's for other less general
reasons. In fact, I'd say the most common off-by-one error I've seen in
code (my own and others) are with respect to calculations like that done
for a binary search, where it's not the beginning or end of the array
that's of interest, but some element in between. Runner-up is in
calculations for graphics code, mostly with respect to rounding errors
that result in off-by-one.
You are talking of the kind of off-by-one errors made by an experienced
programmer. I was talking of those made by a novice programmer.
Incorrect iteration termination is _way_ down on the list, and basically
never happens for experienced, competent programmers.
But does for novice programmers.
Arguing in favor of one indexing method or another as a preventative for
off-by-one errors is not, I feel, something supported by experience or
logical analysis of the situation.
You make it sound like I am advocating such a thing. I'm not. There are
many things in Java that are relatively hard to learn, that doesn't mean
I automatically want to change Java.
Ditto.