Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magic numbers vs named constants

When writing code, especially when dealing with dates and times, you have to work with a lot of specific numbers, eg: 60 seconds in a minute, 3600 = seconds in an hour.

Some people stick to using the raw values for many of these, whereas others put them into constants to increase readability.

eg:

$x = time() + 3600;
$y = time() + 86400;
$z = time() + 604800;

// vs

define('MINUTE', 60);
define('HOUR',   60 * MINUTE);   // 3600
define('DAY',    24 * HOUR);     // 86400
define('WEEK',    7 * DAY);      // 604800

$x = time() + HOUR;
$y = time() + DAY;
$z = time() + WEEK;

Of course, the second one is easier to read, but slightly OTT for some of the lower values, so where exactly do you draw the line? Personally, I see no problem with the readability of 86400 (in my head I automatically read that as "24 hours"), but would draw the line at the WEEK constant.

like image 808
nickf Avatar asked Mar 04 '09 00:03

nickf


People also ask

Is a magic number consider replacing it with a named constant?

Declare a constant and assign the value of the magic number to it. Find all mentions of the magic number. For each of the numbers that you find, double-check that the magic number in this particular case corresponds to the purpose of the constant. If yes, replace the number with your constant.

What is the difference between a constant and a named constant?

When associated with an identifier, a constant is said to be “named,” although the terms “constant” and “named constant” are often used interchangeably. This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution, i.e., the value is variable.

Why are magic numbers problematic?

A magic number is a number in the code that seems arbitrary and has no context or meaning. This is considered an anti-pattern because it makes code difficult to understand and maintain. One of the most important aspects of code quality is how it conveys intention. Magic numbers hide intention so they should be avoided.

How do you define magic number?

A magic number is a numeric literal that is used in the code without any explanation of its meaning. The use of magic numbers makes programs less readable and hence more difficult to maintain and update. Example: int salary = 20000 * workedhours; // what is the meaning of 20000?


3 Answers

86400 is not ok, since you can easily mistype it as 84600, 88400, etc

A mistyped constant will be a compile error

like image 100
Pyrolistical Avatar answered Oct 03 '22 17:10

Pyrolistical


One of my professors once told us not to put any magic numbers in our code except 1, -1, and 0. That's a little extreme, but it sticks in my mind and still guides me although I don't adhere to it completely.

In your example, I would prefer the symbolic names in all cases.

like image 45
Craig S Avatar answered Oct 03 '22 17:10

Craig S


I'd go constants (or some cutesy derivative, like Rails' 15.minutes convention) pretty much everywhere. For me, it's about simplifying the "typing" of it all; if I see "10 * MINUTES" somewhere in a line, I know I'm dealing with time (or someone's up for an arse-kicking). If I see 10 * 60 or 600 it's entirely possible that I might not grok that we're dealing with time quite so easily.

like image 23
womble Avatar answered Oct 03 '22 19:10

womble