Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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"


Dave


Comment on Re: Critique
Select or Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2014-12-19 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (82 votes), past polls