Indexing By Zero: Part 2

Indexing By Zero: Part 2

Once again, here’s the transcript (and feel free to use the image above as well):

I’m not questioning your teaching abilities, but that apparently (from tone) you dislike the idea of indexing from 0.

This is unfortunately not uncommon (see the lua language, for example) but the fact remains that indexing from 1 is a bad idea and EWD was right on this.

Sometimes it helps thinking to indexes as being “between” elements…

[Insert image of “Hello, World” with the indices labeled]

so the interval 3-5 is clearly “lo” and the number of elements included in the interval a, b is b – a.

This way of thinking simplifies a lot reasoning for example when implementing binary search or raster graphics algorithms.

This mental model is equivalent to [a, b[ , but (maybe) easier to understand and remember.

x[i] is just the element between i and i+1.

Some API solve the problem of substring-like interfaces by relying on start/size instead of start/end (this is what Qt does in many cases, for example). Unfortunately the same Qt framework made the wrong choice of using “boundary-inclusive” intervals when implementing right() and bottom() method for integer rectangles, a poor choice that makes hard to write pixel-perfect code and force a lot of +1 and -1 in code: floating point rectangles are ok, and they mistake on the integer case is acknowledged in the documentation (but unfortunately cannot be removed because of backward compatibility reasons).

To me, this was one of the weirdest responses I’ve ever gotten. After all, not only was this person arguing against a strawman (i.e. I never once said I didn’t like indexing by 0), he also felt the need to “educate” me on something I clearly already understood. It was so very weird.

After the second giant comment from this guy, I felt the need to ask him: “what is your end goal here?” For real though, what was his end goal?


If you came from the Wall of Shame, here are links to the previous and next comments. Also, here’s a link back to the Wall of Shame.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Content