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

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: 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?

Replies are listed 'Best First'.
Re^6: Perl vs Python revisited
by BrowserUk (Patriarch) on Jan 07, 2017 at 21:27 UTC
    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.
Re^6: Perl vs Python revisited
by jdporter (Paladin) on Jan 09, 2017 at 21:28 UTC
    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.
Re^6: Perl vs Python revisited
by LanX (Saint) on Jan 07, 2017 at 21:50 UTC
    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!