Article:
  What I Hate About Your Programming Language
Subject:   C, C++, Java, maybe others - zeroth element arrays
Date:   2003-05-15 17:24:19
From:   anonymous2
Response to: C, C++, Java, maybe others - zeroth element arrays

Short version:
1. Ask not whether to start at zero or one. Ask whether to place zero at the start of the array or the end.


2. Ask not whether to use zero or one based indexing. Ask whether to use [0, 1) or (0, 1] based indexing.


Long version:
Think of the indexes as *between* the elements. Like inches on a ruler. The first inch is between the zero mark and the one mark. So we call that inch [0, 1). If we want the second and third inches together we want [1, 3). That is starting at the 1 mark and ending at the 3 mark. (Note that 3 minus 1 is the number of inches as nice side effect.) You also see this effect in birthdays. Your first year of live is between your zero'th birthday and your first birthday and so on.


Now programmer hate to have to write [n, n+1) (or [n - 1, n)) all the time so we need a shorthand. We can take either the first number (zero based) or the second number (one based). It usually wont matter unless your array can use negative numbers. The most common (but not only) case of this is a circular array where negative numbers index from the end of the array. Zero will have to be placed either at the end or the begining of the array (we're *not* going to number them like we do our years). So the real question is whether zero goes at the begining or the end of the array. (Keep in mind where talking about shorthand notation. They are really elements [0, 1) and [-1, 0).)


Note that 0 is an element of [0, 1) but 1 isn't so we should prefer calling it 0 instead of 1. This may be a bias because we could also use [0, 1], (0, 1), or (0, 1]. Let's examine these. I shouldn't have to explain why [0, 1] and (0, 1) are completely out of the question (ask any Math professor). So that brings it down to [0, 1) vs. (0, 1]. In a certain sence which we choose is arbitrary, but Mathematicians have generally settled on [0, 1). Maybe it's because they are more intrested in the set of non-negative numbers, [0, INF), than the set of non-positive numbers, (-INF, 0]. I don't know why but it *is* the convention to prefer [x, y) (read "x upto but not including y") over (x, y] (I have no idea how to read that).

Main Topics Oldest First

Showing messages 1 through 1 of 1.

  • C, C++, Java, maybe others - zeroth element arrays
    2003-10-24 13:24:10  anonymous2 [View]

    "Think of the indexes as *between* the elements..."

    What an absurd way to think of an array index. An array isn't a continuum, it's a numbered sequence of discrete items, so the rest of your "(0,1] [0,1]..." argument is irrelevant.

    How do you number three items in every other domain in life? 1, 2, and 3. The first element of an array of items should be array[1], the the Nth would be array[N]. An array whose highest legal subscript was N would have N elements.

    Easy to count, easy to refer to, the least suceptible of all systems to off-by-one bugs.

    this confusion comes from the fact that array subscripts in close-to-the-metal languages like C are just syntactic sugar for memory offsets. Naturally, the first element of an array starts where the array itself starts, so its offset is zero. my_array(N) is just my_array + N*sizeof(element). It's a pointer to a memory address.

    Unfortunately, from the very start, the most used languages have been low level, like C, so programmers have just gotten it in their heads that arrays *ought* to be zero-based. Perhaps they should be in such languages, but they carry that legacy mindset with them even when they move on to high-level languages where memory offsets are irrelevant -- to where you are simply labeling a sequence of items. If language designers now try to label array elements 1, 2, and 3 as 1, 2, and 3 (instead of 0, 1, and 2), though, the experienced (read: "those with the most legacy baggage between their ears") scream about it, coming up with preposterous, strained arguments such as the one above, appealing to "mathematics" to explain to gullible people why the ordinary counting everyone uses for everything else in life is somehow not acceptible for counting elements in an array.

    Nonsense.