Three easy ways to avoid off-by-one errors in your software

Here's how to avoid one of the commonest software bugs

If you're in the software business you've probably encountered the dreaded 'off-by-one' error.

An off-by-one error often occurs when an iterative loop iterates one time too many or too few. Someone might use "is less than or equal to" where "is less than" should have been used.

It's also easy to forget that a sequence starts at zero rather than one. To put it another way, we sometimes fail to distinguish between a count (how many are there?) and an offset (how far is that from here?)

Ten gaps in eleven posts
These problems aren't unique to programming.

At the time of the last millennium, there was a lot of discussion as to whether it should have been celebrated on 1st January 2000 or 2001.

In debates about the millennium off-by-one errors may not be too serious. In software they can be fatal.

How can you avoid them?

Here are three techniques which help. I'll save the best till last.

Do manual tests


If you've got a bit of code that is looping or indexing, do some manual tests to check it's doing the right thing. If you're using a language with a REPL, like Python, Ruby, Clojure or APL, you can do the test in no time at all.

Test the boundary conditions. What happens if the loop shouldn't be traversed at all, or if the array you're iterating through has no elements?

 

Do automated tests


Even better, use an automated testing framework like junit, pytest or its equivalent for your language.

Make sure you write tests that check normal operation and operation at the boundary.

 

Prevention is better than cure


If you can, use a language construct or language that eliminates the need to loop.

In Java, Python or Ruby you can use a for-loop to operate on each element of an array. In Python or Ruby you can use generators to lazily collect the results of operating on the elements.

Many other languages now have similar features. And of course, in APL it's very rare to need explicit loops.

Want to add together two APL arrays?

Just type a←b+c

and there's not a loop in sight!

 

Loop-free code is safer 


If the idea of writing loop-free code appeals, take a look at my introduction to APL. It's incomplete, but free for the next few days, and if you 'buy' it you'll get free updates as further sections of the book appear.

Get it here.


Comments

Popular posts from this blog

Controlling a Raspberry Pi Pico remotely using PySerial

Five steps to connect Jetson Nano and Arduino

Raspberry Pi Pico project 2 - MCP3008