Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Critique

by davido (Archbishop)
on Dec 06, 2003 at 06:08 UTC ( #312735=note: print w/replies, xml ) Need Help??

in reply to Critique

Here are a few snippets you can use as examples for "shortening" your code. Whether it improves your code is something you'll have to decide for yourself. Golfed down code isn't necessary more legible or readable.

#hash slice with a range my %months; @months{1..12} = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;

You could write your "get_days()" function like this:

sub get_days { my $month = shift; return 31 if grep { $month eq $_ } qw/1 3 5 7 8 10 12/; return 30 if grep { $month eq $_ } qw/4 6 9 11/; return 29 if $month eq '2'; die "Bad month entered!\n"; }

This method eliminates the use of global variables that intentionally leak into functions.

For more speed-efficient lookup tables hashes are preferable, but these are small lists, and you're probably not checking thousands of times a second, so grep is probably fine. Not sure why I chose to treat month numbers as strings. I guess because I usually think of hash keys as strings too.

Those are the biggest changes I can think of, and they're really not necessary. Just look at them as "Another Way To Do It"


Replies are listed 'Best First'.
Re: Re: Critique
by phenom (Chaplain) on Dec 06, 2003 at 14:43 UTC
    I've never seen this before:
    my %months; @months{1..12} = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
    Thanks, I like it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://312735]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-01-21 13:48 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (228 votes). Check out past polls.