in reply to Re^2: Test Number of Elements In Array
in thread Test Number of Elements In Array
One doesn't introduce any new "magic numbers", if instead of writing:
if (@wishes == 3) { print "The genie has granted all of your wishes\n"; }
One writes:
if (3 == @wishes) { print "The genie has granted all of your wishes\n"; }
"And the trick only works when comparing against a literal, not a variable."
Granted. But that doesn't make it any less useful to use for constants, which was my point.
Speaking of magic numbers, I think one can get too carried away with their usage. I would argue the following goes a bit overboard ...
my $days_per_year = 365; # In case the number of days in a year chang +es ;-) ... if (@calendar < $days_per_year) { warn "Hold on, Cinderella -- you're calendar isn't full yet.\n"; }
The above example is based on a college classmate of mine who, back in the days of BASIC, had the constant "dpy" ("Days per year") in a startup script, so when he needed it, he could type "dpy" instead of "365"!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Test Number of Elements In Array
by akho (Hermit) on Jun 07, 2009 at 15:58 UTC |