Code and comments

Practical and theoretical aspects of software development

Why programmers count from zero

with 3 comments

If you are a programmer, you know that zero-indexed arrays and lists are just the right thing. Perhaps you read Dijkstra’s paper, or maybe you’ve encountered enough practical benefits to be convinced. But to non-programmers and students, zero-indexed lists seem extremely odd, and needs explanation. After all, we all know how to count, and it seems like it works OK when we start at one. What’s your good reason for starting at zero?

We have made this too complicated. While it is appropriate to have a careful answer for academic settings, we should have a casual illustration that is useful for casual inquiries. Here are my two suggestions:

Grouping by tens:

Suppose you want to write the first thirty numbers in rows of ten. Your choices are:

 1  2  3  4  5  6  7  8  9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30

 
or

 0  1  2  3  4  5  6  7  8  9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29

 
It’s pretty evident which of these follows a convenient pattern. Look, when we count from zero, our rows always have the same first digit. And when we count from one, the last item in each row is a special case.

A month-to-quarter method:

Suppose you want a method that takes a month, and returns the quarter of the year in which it occurs. If our months and quarters are indexed from one, we have:

int monthToQuarter(int month) {
    return 1+((month - 1)/3);
}

 
but with months and quarters zero-indexed, we have:

int monthToQuarter(int month) {
    return month/3;
}

 
Hmm … which seems better? Which is easier to read?

Of course, both of my illustrations make the same point, that grouping works better with zero-indices. Does anyone have a simpler, quicker explanation?

Advertisements

Written by Eric Wilson

September 21, 2011 at 4:22 am

Posted in didactics

Tagged with ,

3 Responses

Subscribe to comments with RSS.

  1. Conversely, if you store 30 items in a zero-indexed array, and you want to retrieve the 10th item you need to do array(9) i.e. array(n-1). Equally daft, therefore I see no compelling reason for using zero-indexed. Then you get people starting to use terms like ‘the 0th item’. Bad code, and even worse if it is exposed to users!

    Chalky

    September 15, 2012 at 11:30 pm

    • Chalky,

      It’s a given that there is awkwardness to zero-indexing, that was the premise to the post. My point was that that one-indexing also has inherent awkwardness. Sorry I failed to compel you.

      Eric Wilson

      September 25, 2012 at 5:47 am

  2. I completely agree that zero indexing is more logical. The habit has become so ingrained that when I’m writing a list on a whiteboard I involuntarily start with zero.

    Kaj

    September 2, 2013 at 11:41 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: