3 ms·
See the Dijkstra essay[1] for a convincing argument for half-open ranges. https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utex
by Peaker 13y ago
See the Dijkstra essay[1] for a convincing argument for half-open ranges.
https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831...
- ronaldx 13y agoI have read it, and Dijkstra is a good call to authority, but I don't agree. His opening sentence is instructive: using the 'pernicious three dots', everything is totally clear: with an ellipsis, it's impossible to understand anything other than a closed interval. It's primary school mathematics that 2 <= x <= 12, i.e. the interval mathematically written [2,12] (or 2,...,12), contains 11 integers. A programmer who doesn't understand that is in a world of trouble as half-open intervals won't stop the inevitable rain of off-by-one errors. A programmer who actually calculates the size of a list in the way that Dijkstra suggests (specifically, by calculating b-a) is also perhaps acting questionably. Dijkstra's argument here might make more sense when correctly framed in 1982. As an aside, if Dijkstra would agree to use a fully-closed interval, his suggested advantage of 0-indexing is eliminated and he would presumably then prefer 1-indexing.
- Peaker 13y agoThat's not the entirety of the argument. There's the natural number stuff, as well. Also, b-a is appealing because simplicity is a good indicator you're on the right track, and part of the definition of elegance.