The visuals are definitely valuable in explaining this.<p><i>It used to be popular, and still is in some circles, to debate whether programming languages ought start array indexing at 0 or 1.</i><p>When talking about this with other programmers, I've discovered that a lot of the issues/confusion could be avoided by consistent use of terminology: Offsets/offsetting always being zero-based and indexes/indexing always being one-based.<p>Using rulers and birthdays also helps to explain differences. You're in the first year before your first birthday, being zero (whole) years old.<p>To make matters potentially more confusing, culturally, I remember something about the ground floor in the UK buildings not being "Floor 1" like it is in the United States.<p><a href="http://www.hacker-dictionary.com/terms/fencepost-error" rel="nofollow">http://www.hacker-dictionary.com/terms/fencepost-error</a>
See also <a href="https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html" rel="nofollow">https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...</a> ("Why numbering should start at zero")
People have taken different approaches to this in bioinformatics for numbering intervals of dna bases in chromosomes. I think this approach is catching on, though. In that realm, it's important to speak unambiguously about insertions and deletions, and the "interbase" mental model makes it a lot clearer.<p>edit: Probably the most popular genome browser, based at UC Santa Cruz, uses this zero-based, half-open numbering internally. But, at some point in the past, biologists developed an expectation that numbering would be 1-based. So the Santa Cruz genome browser actually adds 1 to the start position of everything for display purposes.
Once at a party I accidentally started an argument about which way toilet paper should hang from the roll (front or back) by mentioning how silly it was that people would argue about such a trivial matter.
I've found this a helpful way of explaining ranges in Python to students, it also makes Python's negative indices understandable for the same reason.<p>But it is contextual. When it comes to languages like C, where arrays are more directly mapped to pointers and memory layout, I've found it better to talk about pointers, and allow people to derive the behavior that way.<p>Either way, I'd be careful of trying to claim that 'this is what it <i>is</i>', rather than 'here is a way to remember it'.
He undermines his own argument right out of the gate. When he shows the part about which number should be used for the last element in the range there's still two valid choices, you could choose to stop at 3 with the understanding that the element to the right of that is the last element to be selected, or to stop at 4 with the understanding that in this context your indexing is "special" and you're actually selecting the element to the left of the index.
Further notes about half open intervals and why starting at zero is a good idea: <a href="http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF" rel="nofollow">http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF</a>
There is a reference to Emacs in the OP, but not directly to what immediately sprung up in my mind[0]. In Emacs, it has a cursor (the block) and a "point" which is described as an imaginary spot at the left edge of the cursor - the point is what's important when one is indexing data (ie: setting a mark and selecting a bunch of text). The model fits for lots of things... Unfortunately for me, tmux highlighting does <i>not</i> follow this model.<p>[0] <a href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Point.html" rel="nofollow">https://www.gnu.org/software/emacs/manual/html_node/emacs/Po...</a><p>edit: clarify Emacs reference in OP
I have sometimes wondered whether it would be useful to have two different types for indices, depending on if we are indexing the elements themselves or the "gaps" between them. Let's say I'm thinking about some language like Haskell, where types are already a big deal.<p>That way the compiler would be able to tell if I accidentally mixed the two. Every conversion would have to explicit: for example, there might be two functions, "before" and "after", that take a gap index, and return an element index.<p>I think I might actually enjoy programming this way, but perhaps others would find it needlessly bureaucratic.
Isn't this the classic fence post vs fence section analogy? I like this statement from Djikstra: "an element's ordinal ... equals the number of elements preceding it in the sequence"[1]
[1]<a href="http://c2.com/cgi/wiki?WhyNumberingShouldStartAtZero" rel="nofollow">http://c2.com/cgi/wiki?WhyNumberingShouldStartAtZero</a><p>Another nice link: <a href="http://betterexplained.com/articles/learning-how-to-count-avoiding-the-fencepost-problem/" rel="nofollow">http://betterexplained.com/articles/learning-how-to-count-av...</a>
And this is why JavaScript's `lastIndexOf` method gets it wrong, wrong, wrong. (`"foo".lastIndexOf("o", 2)` yields 2, whereas I, and probably a lot of other people, would expect it to be 1, with the search starting at the space between element 1 and 2)
This is the same model used by the Cairo graphics library and the HTML5 Canvas (albeit in two dimensions). All the same benefits apply when you're addressing pixels as elements of an array.