3 ms·
Fortunately, many off-by-one errors can be caught with more ergonomic tooling. For the simplest example: compare the old C-style for loop vs a Python style for
by eru 6y ago
Fortunately, many off-by-one errors can be caught with more ergonomic tooling.
For the simplest example: compare the old C-style for loop vs a Python style for-each loop.
- Gene_Parmesan 6y agoI don't find I ever make off-by-one errors with simple collection iteration; at some point "i < len" becomes tattooed on your brain stem. The off-by-one errors I tend to make are related to implementation details of certain data structures or algs. Really, I would describe them more as "thinking at the margins can be challenging." Correctly handling doubly linked lists, that sort of thing. Oh, and slicing. I will never get Python slicing right the first time. The fact that the range is [begin, end) is just never the way I expect it to work.
- Ygg2 6y agoBut slicing 0..len and for (i=0; i< len) are literally the same thing. In [0, len) ')' means less than. As in 0≤ x< len.