Hacker News new | past | comments | ask | show | jobs | submit login

I guess I find it ironic then, that one of the most common programming errors, is of the "off-by-one" variety[1]. It is in part due to differences in convention and notation in various fields[2].

This isn't common in 1-based indexing languages. Very common in 0-based indexing languages[3].

If the 0-based indexing was objectively superior, wouldn't there be fewer instances of such bugs?

[1] https://en.wikipedia.org/wiki/Off-by-one_error

[2] https://en.wikipedia.org/wiki/Zero-based_numbering

[3] https://softwareengineering.stackexchange.com/questions/4289...




I was hoping to read some code analysis that supported your claim of off-by-one being more common in 0-based languages, but all I see in your [3] is a few different speculative reasons - am I missing something?

Off-by-one can show up in a bunch of ways that are agnostic to the underlying indexing, so it isn't obvious to me that there would be a substantial difference at all. Also, given what's said below about the ease of writing code which fails to consider custom indices, I'm not sure I believe just switching to 0-based or anything else in Julia is as painless as you suggested earlier - it could introduce its own off-by-one errors!


Agreed. Off-by-one often shows up when calculating indexes e.g. differences in indexes or offsets. These calculations are independent of the base of the index (0 or 1 or even N).


This implies that off by one is more common in zero based counting. Evidence? Your link didn't seem tob support that changing convention of indexing mattered. Rather, no longer indexing at all is what helped. (E.g., iterators.)

This fits my understanding. I've seen people do off by one just in the wild when counting how many posts/nails/etc are needed.




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

Search: