I don't see a huge benefit in nailing down a
precise definition of what is, and what is not,
a magic number.
We don't have hard and fast rules about magic numbers at work.
Zero and one, for example, will usually survive a code review
(though perhaps not if used with bools).
There is room for common sense, negotiation and programmer discretion.
The focus is on simplicity, clarity and maintainability.
For example, a magic number like 7.4269 that may change in the future
being sprinkled all over the place would not survive the review;
you'd be asked to choose a meaningful name for it,
$customer_interest_rate for example.
Even if the number is PI, and so won't ever change,
the code will usually be clearer if you use $pi
rather than 3.14159.
To be honest, I don't feel strongly about magic numbers
and can't ever remember them being a big issue during a code review.
There are bigger fish to fry, things like:
- Is the component testable in isolation?
- Is the component interface well-designed?
- Has the programmer gone berserk with unnecessary cleverness?
- Is there a coherent error handling policy?
- Are there gaping security violations?
- Is the program easy to support and troubleshoot remotely?
Obligatory references:
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.