logoalt Hacker News

Fraterkesyesterday at 12:27 PM2 repliesview on HN

This is an article that (among other things) talks about off-by-one errors being caused by mixing up index and count (and having to remember to subtract 1 when converting between the two). That's what it has to with it.


Replies

adrian_byesterday at 1:20 PM

If you always use half-open intervals, you never have to subtract 1 from anything.

With half-open intervals, the count of elements is the difference between the interval bounds, adjacent intervals share 1 bound and merging 2 adjacent intervals preserves the extreme bounds.

Any programming problem is simplified when 0-based indexing together with half-open intervals are always used, without exceptions.

The fact that most programmers have been taught when young to use 1-based ordinal numbers and closed intervals is a mental handicap, but normally it is easy to get rid of this, like also getting rid of the mental handicap of having learned to use decimal numbers, when there is no reason to ever use them instead of binary numbers.

SkiFire13yesterday at 2:06 PM

I must have missed that part, my bad

show 1 reply