The Green Stripe

Park-Hagiwara stuff.

Tuesday, May 05, 2009

Zero worship

I recently read a design document which stated that since an ID value was to be viewable by a user, it should start from 1, rather than 0. Counting from 1 is apparently easier for humans to understand.

This offends me. It has offended me for many years. Fortunately in software engineering, at least my speciality which is C programming, arrays start at 0. Unfortunately, in recent years some software engineers have used the relatively vast amount of memory available as an excuse to 1 base their arrays, and not caring about the waste of the 0th memory element - or apparently about the elegance of their code or the fact that they will be damned to eternity in hell for their nefarious crime.

So, why does this issue irritate me? I didn't know until now, but I have figured it out. The simple fact is that no-one counts from 1. We all count from 0. My tiny daughter, less than 2 years old, only counts shoes, and as such never gets past two. But she always starts with no shoes.

It is true that the zero we start counting from is rarely explicitly stated, which is why it isn't immediately obvious that we count from zero. But we must. Imagine counting from 1 to 5. One, two, three, four, five. Start at one, end at five. If we counted to five, how come we're only 4 higher than where we started? The answer is we didn't start at 1. We started at zero.

Everyone knows this. Even my toddler. But to help everyone actually understand, I shall continue, with renewed vigour, to pursue my quest to keep arrays zero based. Even in userland.

Disclaimer: the opinions stated in this article are the author's own, and not those of his employer, or indeed anyone else he's ever met.

6 Comments:

  • At 8:58 pm, Blogger RobZed said…

    I think you know this, but you are confusing counting the number of something (to find the number in the set) with enumerating each item, thereby giving each one a numerical reference. For enumeration, there is no particular reason why the first reference has to be '0' or '1' or any other number - or in fact a number at all ... think Javascript arrays where the index is a string - this associative array is like a c++ map.

    I'm not convinced that losing the memory for element 0 is, in most systems, critical. Except perhaps where you have constructors / destructors - then you are running code on element zero.

    However, I do think there is a deeper problem in C with using 1 as the first element 'because it's seen by the user therefore the conceptual gap between problem domain and solution domain is smaller'. The first that comes to mind is that (a) the user display is tied to this ascending number sequence. We don't, I believe, naturally think of things as numbers - or named as numbered. Therefore there is a lost of abstraction that really could do with being hidden. Objects (e.g. with a C++ class) could sort this. Secondly, or (b), in C, at least, where I've seen this, there tend to be -1 and +1 sprinkled randomly around the code (without the help of object oriented design). Thirdly, or (c), (what do you mean I've run out of enumerations?!) if you have to remove an element or make the list bigger in the middle, or do any sorting, then the index (which is what we are talking about) becomes invalid. But the user numbering, apparently, does not. Doesn't that mean that the whole 'fixed sequence' is an artificial construct of the specifier or developer? Where does this leave the user?

    I'm sure there are more ...

    One last thing: I can hear a bunch of programmers saying "but on small systems"/"my system" it's the most efficient way of showing something. But that's not the argument here. Either: (a) you bit the bullet and go for efficiency and ignore usability or (b) you abstract the user from the internals of the program but make the internal of the program easy to maintain. The third option making it easy by removing abstraction only works in very narrow cases and tends to be fragile to change.

     
  • At 9:06 pm, Blogger Tony said…

    I'm not so much confusing enumeration with counting with lamenting that in general we don't enumerate from zero. Andrew Koenig and Barbara Moo must agree with me. The first chapter in their book 'Accelerated C++' is chapter zero.

     
  • At 9:38 pm, Blogger RobZed said…

    Would it be a problem for you in some languages like some BASICs that start their array indexes at 1 not zero?

     
  • At 9:44 pm, Blogger RobZed said…

  • At 7:24 pm, Anonymous Anonymous said…

    as an atheist how can you dam someone to hell

     
  • At 9:44 am, Blogger Tony said…

    'as an atheist how can you damn someone to hell?'

    Metaphorically.

     

Post a Comment

<< Home