Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Dijkstra is a smooth talker and sometimes he's able to convince people that an argument is settled even when it is not... Here, he conveniently ignores examples that are better for [closed,closed], such as iterating backwards over the range. That is more annoying for [closed,open) because it turns into the abhorent (open,closed].

An argument I prefer is this: [closed,closed] is better for representing an index into the range while 0-based with [closed,open) is better for offsets. They are not the same, as C would have you believe, and there are situations where behaves nicer than the other.



> That is more annoying for [closed,open) because it turns into the abhorent (open,closed].

What's wrong with (open, closed] ?? Especially if you're going backwards?

    for(int i=559; i>554; i--){
        //do stuff with i
    }
Perhaps the most important reason to do zero-indexed + [closed,open> and <open, closed] is because these are the defaults the C / C++ / Java for loops assume you're doing.

Its all non-intuitive because humans are really bad with off-by-one errors.

------

EDIT: I guess familiarity with [closed, closed] is somewhat important, because other programmers like the form. The pattern here is: for(int i=start_closed; i<=end_closed; i++), if anyone's curious.


The problem with that example is that you had to ±1 both of the ends of the original range. That is, you started with [555,560) and had to rewrite it to (554,559] for the reverse loop.


No one really wants [x, y). That want [0,y) or [x,x+y) because y is the length of the interval.

So no one wants (y,x] when going backwards, because x<>0 there.


I remember at the time when C++ ranges were getting standardized there was a talk about an alternative replacement for iterators: splitting them into things that point at elements and between them. That solved the reversal / backward iteration problem rather neatly, although IIRC there wasn’t always a clear choice of which kind of thing should now be returned in place of a single iterator.

(Of course, that doesn’t help at all if you’re using raw indices, but then again, people used to describe screen coordinates as pointing between “little square” pixels rather than at sample locations back in the days of the original Macintosh and 16-bit Windows.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: