Leap Year Determination Code

Alternate Perl version
Home > Leap Year Code

- Join - Honor Roll - Update - LEAPzine - Buy Leap GearContact Us - About Us -

Read about this in The Register.
See our press release.
Alternate Perl Version
We received the email displayed to the right, shortly after our free code offer was publicised. 
While I appreciate that http://leapyearday.com/hr/freecode.html is in Perl, it is a bad example.

For a start, the name of that language is Perl.  Not PERL since it is not an acronym.  If you don't believe me see http://perldoc.perl.org/perlfaq1.html#What's-the-difference-between-%22perl%22-and-%22Perl%22%3f.  (That should also be in your local documentation.  On a Unix system you can type perldoc -q '"Perl"' to see it.)

Secondly you should not use prototypes.  Prototypes in Perl don't do what you think they do.  See http://library.n0i.net/programming/perl/articles/fm_prototypes/ for a full explanation of how they work and *exactly* why you don't want to use them.

Thirdly there is no reason to use short-circuiting logical operators instead of ones that look like English and do the same thing.  Why not make the code look more understandable to casual browsers?  And if we're dealing with them, there is no particular reason to use unneeded shortcuts.

This leads to a much cleaner way to write it.  Namely

sub days_in_year {
    my $year = shift;
    return 366 if 0 == $year%4 and 0 != $year%100;
    return 366 if 0 == $year%400;
    return 365;
}

And this translates easily into other languages.  For instance Ruby:
def days_in_year (year)
    return 366 if 0 == year%4 and 0 != year%100;
    return 366 if 0 == year%400;
    return 365;
end

Copyright © 1997-2008  The Honor Society of Leap Year Day Babies
Please send comments by email to usMarch, 2008