Hacker News new | ask | show | jobs
by jacknews 1061 days ago
So the first item in my array of 13 items is at position '0'. The 3rd item is at position '2'.

And if I want to count the items in my array I start at '0' and keep going until I get to '12'. Even though there are 13 items in my array?

I see.

zero-indexing is good for system-level stuff, for dealing with actual memory and so on, but I'm still not convinced it's the right way. It's certainly not the one true way, 1-based indexing is completely valid for some use-cases like maths.

Either way, there will be off-by-one difficulties, either in ordinals or count or whatever, so it's a trade-off.

1 comments

By now we've been doing

    for (int i=0; i<13; i++) {
       whatever(array[i]);
    }

    and

    def printHello():
       print("hello world"[0:5])

for decades. No point vastly messing up consistency for minimal gain. BTW notice how in both cases we count to n elements despite using 0..n-1 indexing. You don't, in fact - need to put 12 anywhere if you mean 13 elements.
But

  whatever[1]
gives the second item, and the last item, of 13, is

  whatever[12]
fortran has been 1-based since well before these examples.

The point is there is no one true way, it's a trade-off.