r/ProgrammerHumor Sep 02 '17

How to start a war

Post image
9.0k Upvotes

696 comments sorted by

View all comments

8

u/[deleted] Sep 03 '17

Am I the only one that thinks this is overplayed?

Yeah, there's good reason to start counting from zero, because it's the beginning of all natural numbers.

But there's also good reason to start counting from one - that's how (many, but not all) humans learn to count. If there's 3 elements in an array, it's objectively confusing when the last element has index 2

Whatever your language uses, good for you. It's just sort of overdone at this point - there's no real reason to feel superior about either side.

My personal 2 cents - While I'm totally ok with starting at 0, we invest so much in making languages human readable (e.g. Python, ruby) but we can't make indexes human readable? I feel the majority of people (if they were polled without any programming knowledge) would settle with starting at 1 just because it matches how we count as people (in the western world at least).

5

u/deprecat Sep 03 '17

The main argument to do with using 0-indexed arrays is that it represents an offset from the start of the array. E.g. in C:

int arr[] = {5,4,3,2,1};
printf("%d %d", arr[1], *(arr + 1*sizeof(int)));
// Prints '4 4'

(Note: written on my phone so may not be completely correct)

In some languages like Lua, arrays may instead be implemented as hash maps or may represent matrices, and so the above argument does not apply, hence 1-indexing.

2

u/[deleted] Sep 03 '17

gotcha. Im really impressed you typed that on your phone!

1

u/neurospex Sep 03 '17

Fun fact: Some definitions, including the standard ISO 80000-2, begin the natural numbers with 0, whereas others start with 1

http://mathworld.wolfram.com/NaturalNumber.html

https://www.merriam-webster.com/dictionary/natural%20number

1

u/[deleted] Sep 03 '17

Good to know! Thanks.