Magic numbers

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

if (x < 3600) do_something();

What the heck is 3600? But

#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

#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.


Rob

Is that the number of magic mushrooms you can grow on your smallholding ?

( its so nice to be able to talk of such things now I am not a God member anymore :slight_smile: )

:slight_smile:

If we had any mushrooms they would have been washed away in the floods. 450mm of rain the other day. No rain all year so our friends bought 10,000 litres, the truck was barely down the drive and the rain started. Life's a bitch.


Rob

I hear the flood warnings are very worrying down the East Coast again, the pictures of the Bundaberg area are frightening.

Fresh water in the truck could be valuable in a flood ? but no doubt they dumped it in his tank...

Graynomad:

#define PULSES_PER_K		(2089 - 21)

#define PULSES_PER_100M (PULSES_PER_K / 10)
#define MAGIC_NUMBER (16604 * 1.12)

Ha! Love it. Just remember, nothing is ever a complete failure, it can always be used as a bad example :wink:

Graynomad:
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

if (x < 3600) do_something();

What the heck is 3600? But

#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



#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.

______
Rob

What, no furlongs per fortnight factor macro?

Lefty

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 ?

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 :slight_smile:

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