[sdiy] Beware Magic Numbers!`
Jay Schwichtenberg
jschwich53 at comcast.net
Thu Dec 28 01:10:25 CET 2017
Tim,
The proper way of doing it is use defines (C) or EQUs (asm) in a header file
and not use numbers directly in the code.
1) Using a name for the number gives you a better idea of what the number is
used for.
2) If the number is used multiple places in the code you change it in one
place and all the code is changed. This avoids the problem of changing the
code in the 3 places you remember its used and not changing it in the 4th
you forgot.
Also having something like a cheap logic analyzer or scope that decodes
serial, I2C or SPI will save you a lot of time trying to track down bus
issues. I have an older saleae and that has saved my ass so many times when
doing embedded stuff.
Happy coding.
Jay S.
-----Original Message-----
From: Synth-diy [mailto:synth-diy-bounces at synth-diy.org] On Behalf Of Tim
Ressel
Sent: Wednesday, December 27, 2017 3:40 PM
To: SYNTH DIY
Subject: [sdiy] Beware Magic Numbers!`
Yo,
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
_______________________________________________
Synth-diy mailing list
Synth-diy at synth-diy.org
http://synth-diy.org/mailman/listinfo/synth-diy
More information about the Synth-diy
mailing list