[sdiy] Beware Magic Numbers!`

Tim Ressel timr at circuitabbey.com
Thu Dec 28 00:40:13 CET 2017


I just spent days chasing down a bug with SPI and a flash memory. I 
finally nailed it, and it was something dumb: a magic number. This when 
you set a variable or register to a number without any annotation of 
what that number is. In my case it was an Arduino call to set the SPI 
clock mode. It was being set to '0' in the code I borrowed. My squishy 
brain decided that must be SPI mode 0. Wrong-o. When I set it the the 
hard coded constant 'SPI_MODE0' it started working.

The moral of this story is: don't be a lazy schlub like me. Annotate all 
your numbers so they are clear. A simple test: ask yourself, "If I look 
at this code a year from now, will I know that this means?

--Tim Ressel
Circuit Abbey
timr at circuitabbey.com

More information about the Synth-diy mailing list