Development Wiki

Magic numbers

From phpBB Development Wiki

Revision as of 22:57, 29 October 2008 by 3di (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Don't use them.

Use named constants for any literal value other than obvious special cases.

Basically, it's ok to check if an array has 0 elements by using the literal 0.

It's not ok to assign some special meaning to a number and then use it everywhere as a literal. This hurts readability AND maintainability. The constants true and false should be used in place of the literals 1 and 0 -- even though they have the same values (but not type!), it's more obvious what the actual logic is when you use the named constants.

Typecast variables where it is needed, do not rely on the correct variable type (PHP is currently very loose on typecasting which can lead to security problems if a developer does not have a very close eye to it).

Magic Numbers, we refer to the programming stuff here: [1]