3 ms·
So ... what? Don't have clear, intuitive methods for common cases? Ruby also provides arr[-1], arr[-2], etc. But there's a subtle difference when I read arr.
by jamesbritt 17y ago
So ... what? Don't have clear, intuitive methods for common cases?
Ruby also provides arr[-1], arr[-2], etc. But there's a subtle difference when I read arr.last vs. arr[-1].
- codexon 17y agoThe first index is 0, the next is 1, and if you loop around backwards, it's -1. Makes sense to me.
- stcredzero 17y agoThat's another demonstration of my point. Many, many people out there in the non-tech world would expect that the first element would have an index of "1". First = "1st" = "#1." On the other hand, we often experience the convenience of pointer arithmetic (or similar) with zero-indexed arrays. Yet another programmer/techy and ordinary person divide.
- codexon 17y agoIf you want something for non-techies, you pick something like Applescript. 0-based indexing is noob friendly because most cs algorithms use it so you can do a straightforward translation. Would it be better to go with 1-based and make this harder for novice programmers? I don't think so.
- jamesbritt 17y ago"Makes sense to me." Me too. But: when I see code that refers to first and last, I expect that the first and last elements are special. When I see integers, it leads me to think that all of the elements are about equally important. I also find it easier to read the text form rather than the integers; I don't have to look carefully to see if it's 1, 11, 7; code reading goes more smoothly. Having both forms allows me to express, and parse, intention better.