in reply to Re^3: 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.

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.

Replies are listed 'Best First'.
Re^5: Perl vs Python revisited
by eyepopslikeamosquito (Archbishop) on Jan 07, 2017 at 21:18 UTC

    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!