Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Perl vs Python revisited

by eyepopslikeamosquito (Archbishop)
on Jan 07, 2017 at 05:44 UTC ( [id://1179112]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Perl vs Python revisited
in thread Perl vs Python revisited

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:

Replies are listed 'Best First'.
Re^4: Perl vs Python revisited
by BrowserUk (Patriarch) on Jan 07, 2017 at 19:17 UTC
    I don't see a huge benefit in nailing down a precise definition of what is, and what is not, a magic number.

    Fair enough. My personal criteria is that a number used once, is just a number. Used more than once -- to represent the same value -- it should be a symbolic constant or enum.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.

      My personal criteria is that a number used once, is just a number. Used more than once -- to represent the same value -- it should be a symbolic constant or enum.
      From magic number (wikipedia), some possible exceptions:
      • The use of 0 and 1 as initial or incremental values in a loop, such as for (int i = 0; i < max; i += 1)
      • The use of 2 to check whether a number is even or odd, as in isEven = (x % 2 == 0)
      • The use of 100 to calculate percentages
      If 0, 1, 2, 100 in the examples above were used more than once in your program, would you create a symbolic name for them?

        If 0, 1, 2, 100 in the examples above were used more than once in your program, would you create a symbolic name for them?

        Not for the uses in your examples.

        I do use enums for 0 and 1 where they are indexes in multi-dimensional arrays with the 0th element being an X coordinate and 1 the Y coordinate; and similar examples.

        This is the results of a grep for "enum" in *.pl files in my junk directory:

        1031775.pl:use enum qw[ NAME FREQ LEFT RIGHT ]; 1161363.pl:use enum qw[ IN DBI ]; 1161647.pl:use enum qw[ IN DBI_ENUM ]; 1177385.pl:use enum qw[ IN DBI_ENUM ]; 904729-2.pl:use enum qw[ CODE GRAPH START PATH SEEN ]; 904729-3.pl: use enum qw[ CODE GRAPH START PATH SEEN ]; CIDR.pl:use enum qw[ CIDR NETWORK SIZE RANGE ]; CIDR.pl:use enum qw[ FIRST LAST ]; dumpTree.pl:use enum qw[ LEFT RIGHT ]; dumpTree.pl:use enum qw[ WIDTH DEPTH NAME ]; FPstuff.pl:use enum qw[ X Y ]; GraphBench.pl: use enum qw[ CODE GRAPH START END PATH ]; GraphBench.pl: use enum qw[ CODE GRAPH START END PATH ]; hFP.pl: use enum 'KEY', 'VAL'; lazyTree.pl:use enum qw[ LEFT RIGHT ]; philo3.pl:use enum qw[LEFT RIGHT ALL]; plotTSP.pl:use enum qw[ X Y ]; plotTSP2.pl:use enum qw[ X Y ]; plotTSP3.pl:use enum qw[ X Y ]; SameFringe.pl:use enum qw[ LEFT RIGHT ];

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
        In the absence of evidence, opinion is indistinguishable from prejudice.
        0, 1, 2, 100 ...

        And that's the difference between "magic" number and "hard-coded" number.

        Obviously, not all numeric literals are magic.

        I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.
        For completeness

        I've already seen code binding 0 and 1 to false and true .

        edit

        of course only in boolean context.

        update

        But I wouldn't be surprised if some people called Perl's use of 0 and 1 in these cases magic numbers.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1179112]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-25 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found