4 ms·
Dijkstra's argument is obsolete. Iterators have largely replaced the explicit specification of integer ranges in loop control. Also, the recent rise in progra
by randcraw 10y ago
Dijkstra's argument is obsolete. Iterators have largely replaced the explicit specification of integer ranges in loop control.
Also, the recent rise in programming with multidimensional data invites the question of why matrix integer indices should include zero, which Dijkstra's essay doesn't address.
Likewise, the increasing use of real variables in software invites asking whether an elegant syntax for specifying integer ranges applies equally well when used for real ranges, since <= operators make less sense when constraining floats. Also not addressed.
- joatmon-snoo 10y agoBut iterators are also generally commonly implemented with the same inclusive-front exclusive-end paradigm, e.g. in C++ you have for (Object &o : iter) being essentially equivalent to for (i = begin_iter; i < end_iter; i++)
- seanwilson 10y ago> Iterators have largely replaced the explicit specification of integer ranges in loop control. I'd agree with that for lots of domains. If you're dealing with indices often you're probably working at the wrong level of abstraction. Indices are just error prone in general even if you count from zero.
- jimmaswell 10y agoWhat if the index has some meaning to my calculations? What if I'm indexing parallel arrays? These both happen all the time in my python scripts.
- seanwilson 10y agoI said "for lots of domains". Obviously you have to use indices in some code but if it's possible to avoid using indices it's usually a good idea (e.g. using iterators over a standard for loop).
- EpicEng 10y agoThere's a whole lot of work that goes into the high level abstractions you take for granted. The people making those abstractions care about this, even if you don't.
- avian 10y ago> Iterators have largely replaced the explicit specification of integer ranges in loop control. Yes, explicit indices are less common than they used to be due to iterator support in languages. However they are far from being obsolete. I've been recently going through a lot of code dealing with numerical calculations and there you see hardly any iterator use. Also, MATLAB code, which uses 1-based indices, is an absolute pain to deal with compared to Python/numpy, for example.
- tnecniv 10y agoMaybe I've been drinking to koolaide for too long (and it's not a good flavor), but the 1-based indices in MATLAB are not bad once you get used to them. They're especially nice when translating mathematical papers that normally index from 1.
- goatlover 10y agoYeah, I don't get the love for 0 base, unless you're doing C level stuff, which is the real reason for 0 based indexing.
- avian 10y agoI guess it depends on the field you're working with. I'm definitely more at home with 0-based languages, although at this point I have worked a lot with MATLAB/Octave code as well. In my case, it is very common that i-th element in an array is somehow related to say (x0 + i*dx). This fits naturally with 0-based indices. With 1-based indices you tend to have (i-1) or (x0-dx) everywhere. It gets extremely annoying when people get clever and rearrange this -1 in equations. Then it is no longer clear what is there as a side effect of the indexing and what is perhaps some constant in the numerical method.
- goatlover 10y agoI like Python a lot, but I find Julia's 1 based to be much more intuitive and easier to deal with.
- dagss 10y agoFor much number crunching there is often no substitute for jumping around using indexes computed by sometimes elaborate rules. Although whether to add or subtract 1 in the end is the least complicated part then, but it would have been nice if Python (0), MATLAB (1), Fortran (1), C (0), Julia (1) didn't disagree on the convention as you move between them.
- solipsism 10y agoAlso, the recent rise in programming with multidimensional data invites the question of why matrix integer indices should include zero Could you explain this?
- randcraw 10y agoWhether an array index should start at 0 or 1, and whether loop index range intervals should be open, half-open, or closed are long-standing religious debates among programmers. It seems everyone has a preference. It's all about each language's default semantics -- what can and should be left unsaid, and how that expresses the language's model for array access, pointer de/referencing, and loop control. For array indexing, the most accommodating syntax is to do what Fortran does: allow the programmer to explicitly specify each array index's closed array range. That may be the most user-tolerant and unambiguous model for arrays, but it doesn't work well if you also want the same syntax to dereference pointers, whose indices must start at 0. Thus array indices of pointer-based languages (always?) start at 0. But that annoys the hell out of folks who work extensively with arrays AND real numbers, where half-open 0-based integer index ranges compel clumsy semantics when iterating over their precious arrays. (Folks like me, BTW.)
- CountSessine 10y agoThat's nice. I wish I could somehow hide the fact that Lua uses 1-based indexing by concealing them completely with iterators. And yet here I am, programming in Lua with 1-based indices in tables and dealing with the logical inconsistencies of interfacing with application bindings that necessarily expose C-native structures using 0-based indexing.
- Koshkin 10y agoI think that the biggest reason to continue to use explicit indexing in modern programming is that this remains the most straightforward way of translating problem from the mathematical notation to code. It is not too easy for me to imagine, for instance, matrix arithmetic done using iterators. Another, even more striking, example is computational problems that are solved using methods of tensor analysis, where indices are heavily relied upon in the way things work.
- _0ffh 10y agoHmmm, I'd say multidimensional arrays are an extra argument in favour of zero-based indexing. The position of an element in a 2d array is &(a[k0,k1])==a+k0+n0k1 (modulo syntax and ordering convention), where kx is index in direction x and nx is length of array in direction x. For a 3d array it's &(a[k0,k1,k2])==a+k0+n0(k1+n1k2). Using 1-based indexing you'd need to adjust all the kx, which would be an unnecessary waste of space and time. Edit: Removed asterisks for multiplications, as they got interpreted as markup.
- nabla9 10y agoHalf-open intervals are even more important when handling real ranges and float indexes. Consider splitting and joining ranges in arbitrary floating point b. split [a, c[ into [a, b[ and [b, c[ vs. split [a, c] into [a, b] and [b+e, c]
- belovedeagle 10y ago> the increasing use of real variables in software This betrays the complete lack of mathematical qualification evident in the rest of your comment. IEEE numbers don't even pretend to be the reals. The most that can be said for them is that they're two copies (positive and negative) of a very small subset of the nonnegative rationals, adjoined to a couple extra symbols (+inf, -inf, NaN), with really wonky operations to work around the obvious non-closure of the rational subsets under standard mathematical operations (let alone the approximations to transcendental functions). I think it's important to be extremely clear: this is not at all like the mathematical reals, not even a tiny little bit. The use of IEEE numbers in serious applications (Bitcoin, iiuc) is truly frightening. They should be considered "useful for computer graphics" (although I can't say whether modern gpus are using IEEE numbers; they're at least useful for software rendering) and nothing more.