[sdiy] Beware Magic Numbers!`

Tim Ressel timr at circuitabbey.com
Thu Dec 28 01:13:32 CET 2017


Agreed on all points, including the logic analyzer. You can't fix it if 
you can't see it.

--tr


On 12/27/2017 4:10 PM, Jay Schwichtenberg wrote:
> 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




More information about the Synth-diy mailing list