Pages

Wednesday, 22 June 2016

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.


Sunday, 19 June 2016

Learn APL on the $5 Raspberry Pi

APL is one of the most productive programming languages ever invented, and it's available free on all models of the Raspberry Pi.

I've just launched an early access version of an Introductory eBook, and it's free for the next seven days.

You should read this book if you want to
• find out what programming in APL is like
• learn how to use the language effectively
• decide if APL is appropriate for your project
• take part in the Dyalog annual APL problem-solving competition

The fast-paced introductory text will teach you the core of the language in a few short, fun sessions.

Once you've finished the book you'll get links to free resources you can use to master the rest of APL's amazing capabilities.

The book is only 30% complete at present, but if you 'buy' the free version now you'll pay nothing for the book, and you'll get free updates as soon as they are published.

I'll start to increase the price next Sunday, and it will continue to go up as the book nears completion.

So don't wait - click here to get it now!

A new/old approach to Parallel processing

Morten Kromberg of Dyalog APL has just published a video of his keynote from the PLDI 2016 ARRAY Workshop.

It's titled Notation for Parallel Thoughts and it describes some exciting innovations in the field of programming for parallel processing.