Go Down

Topic: Magic numbers (Read 1 time) previous topic - next topic


We all know that so-called magic numbers are bad in a program. They are sometimes needed though and the usual technique is to create a human-readable #define so at least you know what the number represents, for example

Code: [Select]
if (x < 3600) do_something();

What the heck is 3600? But

Code: [Select]
#define SECONDS_PER_HOUR  3600
if (x < SECONDS_PER_HOUR) do_something();

Makes some sense. So I had call to revisit some old code for my digital speedo today and found this

Code: [Select]
#define PULSES_PER_K (2089 - 21)
#define PULSES_PER_100M (PULSES_PER_K / 10)
#define MAGIC_NUMBER (16604 * 1.12)

Lucky I had that last define or I'd never know that the heck (16604 * 1.12) represents.


What, no furlongs per fortnight factor macro?



Feb 01, 2013, 08:22 pm Last Edit: Feb 01, 2013, 10:15 pm by liudr Reason: 1
So my question is: if a programmer doesn't use MAGIC_NUMBER but (16604*1.12) instead, how highly do you rate him/her [edit] work[/edit]?


if a programmer doesn't use MAGIC_NUMBER but (16604*1.12) instead, how highly do you rate him/her?

18,596.48  ?   
With my mobile phone I can call people and talk to them -  how smart can you get ?


nothing is ever a complete failure, it can always be used as a bad example

Very true.

furlongs per fortnight

The speedo was for my truck which is not very fast, that may have been an appropriate measure but I didn't think of it at the time :)

the pictures of the Bundaberg area are frightening.

Largest recorded flood ever in Bundy, we have land about 65k away up in the hills so don't worry about floods although I know the roads has washed away not far from us.

Rob Gray aka the GRAYnomad www.robgray.com

Go Up