If it's a text it generally doesn't matter, if it's a deck of cards, well, should it matter?

I am talking about indexes. 0-51 for cards, 0-6 for days in a week, 0-364 for days in a year if you want to store something at that spot in your array.

The human thinks that the first ( 1 ) day of the year is 1. Computers with their 0-indexes start at 0 and we adjust accordingly.

How do you do it? Burn the first byte? dword? whatever? so that your indexes can be 1-52, 1-7, 1-365 organized or the other way around? Anyway - you have to adjust the length somewhere. Do you get confused thinking that your array has in fact 8 days in a week if you burn the first? How about loops ? Does it annoy you / confuse you to start looping at 1? Does it annoy you if you read someone else code who does the opposite of how you would have done it?

Pick a side and stick to it? What's your input?

Posted on 2010-12-30 12:44:19 by JimmyClif
Aside from a few languages where 1-indexing is supported natively (eg Pascal/Delphi, VB), I would say that 0-based indexing is the convention.
I always use 0-based indexing, and virtually all of the code that I've worked with over the years (mostly asm, C/C++, Java and C#) does the same.
I think 0-based indexing is very intuitive if you understand WHY it works that way.
And that is because an array is stored as a pointer to the first element.
The array indexing is an offset to the pointer.
So array comes down to this:
- array is the pointer
- array is shorthand for: array + n*sizeof(element)
Hence the first element is the element at n=0.
Posted on 2010-12-30 14:14:43 by Scali
Although I tend to agree with Scali, I think we humans bend too much to try to think like the computer instead of the other way around.  There is quite a bit of literature on the net that supports the side of making computers understand our natural language better.  All natural languages begin with 1 as the first in the sequence.  When I'm programming I have to make the mental adjustment that the first position of an array is at offset zero of an address.  Programming occasionally tends to  be counter-intuitive to what we've been taught since grade school.  If I have 1 apple and you have one apple how many apples do we have? 1 + 1 = 2.  Let's see how this incredibly simply concept translates into computer code :


void main(int argc, char *argv[])
{
   int ctr, total_apples;
   int apples_array[2];

   apples_array[0] = 1;
   apples_array[1] = 1;

   for ( ctr=0, total_apples=0; ctr < sizeof(apples_array); ctr++)
      total_apples += apples_array;

   printf("there are %d apples available to eat\n", total_apples);
}


Why does the first index in the array start at 0 rather than 1?  Because C as a language is designed to be "close" to machine code but yet also "portable".  Thus, we talk of array offsets.  Offsets from what?  The address location designed to hold the first object - an integer number in this case - the address in memory that is represented by the variable name apples_array.   For those of us that having been programming our entire lives we understand this.  But this concept is a major hurdle for newbie programmers.  It is counter-productive to the way we think.  How many bugs and buffer over-runs have been caused by this - and made by professionals even?  Quite a lot.
Had Pascal gained widespread acceptance rather than C we'd be indexing array positions, not offsets.
And starting with the number 1.
Just my 1 + 1 cents ...


Edit: Speaking of bugs made by professionals, the above hastily posted code contains one which I'm purposely leaving in for this discussion.  :lol:

Posted on 2010-12-30 22:36:07 by p1ranha

Had Pascal gained widespread acceptance rather than C we'd be indexing array positions, not offsets.
And starting with the number 1.
Just my 1 + 1 cents ...


Well, to clarify my earlier post, I'd like to say that you should stick to the 'native' representation that your language uses, to avoid problems.
If you want 1-based indexing in C/C++, not only is that counter-intuitive to most C/C++ programmers, so that they may misread your code, but you also need to manage this translation manually, which means there's more code to write, and as such there is room for error. If you just stick with 0-based all the time, you'll get used to it soon, and rarely make mistakes (and if you do, you'll catch them quickly).
With Pascal the feature is built into the language, so the compiler handles it for you behind the scenes (which may result in reduced performance and larger code, which is probably one of the reasons why Pascal never became as widespread as C/C++). The actual range is also evident from the syntax that you use, so other Pascal programmers will not have problems understanding your code or making wrong assumptions.

For me personally the worst part of working with Pascal and VB is that they use 0-based and 1-based indexing at the same time. Namely, although the language itself might support 1-based indexing, the underlying Windows API or other third-party libraries do not. So you will still be using 0-based indexing in a lot of code.
For me personally that is a good reason not to use 1-based indexing at all, if your language can handle 0-based indexing equally well.

I also don't think that 0-based is all that 'unhuman'. In essence, programming is just a form of applied mathematics. And in mathematics, using 0 is quite common practice as well. Like for example how x^0 is defined, and in general, series will be started at 0. This practice has existed centuries before the computer was even invented.
Mathematics is the language of counting, not 'natural language'.
Likewise, using 'natural language' to define logical statements is very error-prone, since logic in natural language often depends on context and other strange intricacies. Saying the exact same sentence word-for-word in a different language can mean something completely different.
Hence humans have invented languages to express logical statements in unambiguous ways.
Trying to make the computer understand natural language is nice at a high level (eg Google), but I don't think it has a place in programming because of the inherent ambiguous nature of natural language. The fact that we have introduced 1-based indexing in the first place (where 0-based indexing has been around even before programming languages were invented, as that is just how the machine works), introduces such ambiguities.


Edit: Speaking of bugs made by professionals, the above hastily posted code contains one which I'm purposely leaving in for this discussion.  :lol:


Yea, that sizeof(apples_array) statement is rather interesting in combination with the ctr++ in that for-loop :)
Posted on 2010-12-31 07:13:01 by Scali
It's what I though you guys would write. I never even considered 'burning' a slot to align my array's on a 1 index until I came along on a C# book by Jack Purdum (wrox.com) and he suggests strongly to align arrays on a 1 index. His code is riddled with static string [] = { "cabbage", "Monday", Tuesday", Wednesday"...etc} - He accesses them using for (index = 1 to end) and ArraySize + 1 all over.

Reading the code confuses the living hell out of me. As this book was given to me I am reluctant to not continue reading but I wonder if I should bother scratching my head over it or not.

I'm pleased to know that 0-based is the standard. :thumbsup:

Thanks for the input.
Posted on 2011-01-01 19:05:42 by JimmyClif
"void main"? Where'd you learn talk like that? :)

Best,
Frank

Posted on 2011-01-01 22:30:35 by fbkotler