Go Down

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

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

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.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Boffin1

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 :-)  )
With my mobile phone I can call people and talk to them -  how smart can you get ?

Graynomad

:)

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
Rob Gray aka the GRAYnomad www.robgray.com

Boffin1


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...
With my mobile phone I can call people and talk to them -  how smart can you get ?

Jack Christensen


Code: [Select]
#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 ;)
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

retrolefty


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.

______
Rob



What, no furlongs per fortnight factor macro?

Lefty

liudr

#6
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]?

Boffin1

Quote
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 ?

Graynomad

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

Very true.

Quote
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 :)

Quote
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
Rob Gray aka the GRAYnomad www.robgray.com

Go Up