Eliminating Magic Numbers: When is it time to say "No"?
Posted
by
oosterwal
on Programmers
See other posts from Programmers
or by oosterwal
Published on 2011-03-09T16:30:24Z
Indexed on
2011/03/10
8:18 UTC
Read the original article
Hit count: 240
We're all aware that magic numbers (hard-coded values) can wreak havoc in your program, especially when it's time to modify a section of code that has no comments, but where do you draw the line?
For instance, if you have a function that calculates the number of seconds between two days, do you replace
seconds = num_days * 24 * 60 * 60
with
seconds = num_days * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE
At what point do you decide that it is completely obvious what the hard-coded value means and leave it alone?
© Programmers or respective owner