M
mahurshi
Hi folks.. I write code as a hobby to keep me from getting bored. Of
late, I have been trying to think of ways to improve my coding style.
(organizing, commenting, indenting, error checking, uncluttering, etc)
I don't want to be all anal but I want to write good code. (I know the
word "good" is subjective)
Anyway, I just wanted to see what you guys thought of this. And I
would find it helpful if you gave me some suggestions regarding the
same.
I am pasting my code here. The program is supposed to find the day of
the week for a given date.
#!/usr/bin/perl -w
#################################################
## this program has a set of functions
## that will help determmine the day of the week
## for a given day
#################################################
use Getopt::Std;
getopt('mdy');
## this function returns a century code given a year
sub get_century_code
{
my($year) = @_;
my($century);
$century = int($year/100);
if ($century == 17) { return 4; }
elsif ($century == 18) { return 9; }
elsif ($century == 19) { return 0; }
elsif ($century == 20) { return 6; }
else
{ print "Can't find century code. Century = $century, Year =
$year\n";
print "Year should be between 1700 and 2099\n"; die; }
}
## this function returns 1 if a given year is leap year
## it returns 0 otherwise
sub is_leap_year
{
my($year) = @_;
## if year is divisible by 4
if ($year%4)
{
## if year is divisible by 100
if ($year%100)
{
## if year is divisible by 400
if ($year%400) { return 1; }
## if not
else { return 0; }
}
else { return 1; }
}
else { return 0; }
}
## this function returns the month code given a month and a year
sub get_month_code
{
my($month, $year) = @_;
my($leap_year);
$leap_year = is_leap_year($year);
if ($month == 1)
{
if ($leap_year == 1) { return 0; }
else { return 1; }
}
elsif ($month == 2)
{
if ($leap_year == 1) { return 3; }
else { return 4; }
}
elsif ($month == 3) { return 4; }
elsif ($month == 4) { return 0; }
elsif ($month == 5) { return 2; }
elsif ($month == 6) { return 5; }
elsif ($month == 7) { return 0; }
elsif ($month == 8) { return 3; }
elsif ($month == 9) { return 6; }
elsif ($month == 10) { return 1; }
elsif ($month == 11) { return 4; }
elsif ($month == 12) { return 6; }
else { print "Can't find month code. (Invalid month?) Month = $month,
Year = $year\n";
die; }
}
## this function checks the sanity of the given input
sub check_sanity
{
my($month, $date, $year) = @_;
my($leap_year);
## if year is out of range
if ($year < 1700 || $year > 2099)
{ print "Please enter a year between 1700 and 2099\n"; die; }
## if month is out of range
if ($month < 1 || $month > 12)
{ print "Invalid month!\n"; die; }
## if date is below 1
if ($date < 1)
{ print "Invalid date!\n"; die; }
## if date is greater than 31 for some months
if ($month == 1 || $month == 3 || $month == 5 || $month == 7 ||
$month == 8 || $month == 10 || $month == 12)
{
if ($date > 31)
{ print "Invalid date!\n"; die; }
}
## if date is greater than 30 for some months
elsif ($month == 4 || $month == 6 || $month == 9 || $month == 11)
{
if ($date > 30)
{ print "Invalid date!\n"; die; }
}
## for february
elsif ($month == 2)
{
$leap_year = &is_leap_year($year);
## allow only upto 29 days for leap years and
## 28 days for non leap years
if ($leap_year == 1)
{
if ($date > 29)
{ print "Invalid date!\n"; die; }
}
else
{
if ($date > 28)
{ print "Invalid date!\n"; die; }
}
}
}
## function returns the year code given a year
sub get_year_code
{
my($year) = @_;
my($last_two_digits);
my($divide_by_4_quotient);
my($divide_by_7_remainder);
my($sum);
## get the last two digits of the year
$last_two_digits = $year % 100;
## divide year by 4 and take the quotient
$divide_by_4_quotient = int($last_two_digits/4);
## add the results up
$sum = $last_two_digits + $divide_by_4_quotient;
## divide by 7 and take the remainder
$divide_by_7_remainder = $sum % 7;
## that's the result
return $divide_by_7_remainder;
}
## function returns a number (0 thru 6), 0 corresponding to saturday
## and 6 corresponding to friday
## function takes month, date, and year as arguments
sub get_day_of_week
{
my($month, $date, $year) = @_;
my($month_code);
my($century_code);
my($year_code);
my($day_of_week);
$month_code = &get_month_code($month, $year);
$century_code = &get_century_code($year);
$year_code = &get_year_code($year);
## add month code, century code, year code and date,
## divide by 7, and take the remainder
$day_of_week = ($month_code + $century_code + $year_code + $date) % 7;
return $day_of_week;
}
## show usage information
if (!defined($opt_m) || !defined($opt_d) || !defined($opt_y))
{ print "Usage: day_for_date.pl -m <month> -d <date> -y <year>\n"; die;
}
$day = get_day_of_week($opt_m, $opt_d, $opt_y);
print "$opt_m/$opt_d/$opt_y (MM/DD/YYYY) is $day (where: 0 - Sat ... 6
- Fri)\n";
late, I have been trying to think of ways to improve my coding style.
(organizing, commenting, indenting, error checking, uncluttering, etc)
I don't want to be all anal but I want to write good code. (I know the
word "good" is subjective)
Anyway, I just wanted to see what you guys thought of this. And I
would find it helpful if you gave me some suggestions regarding the
same.
I am pasting my code here. The program is supposed to find the day of
the week for a given date.
#!/usr/bin/perl -w
#################################################
## this program has a set of functions
## that will help determmine the day of the week
## for a given day
#################################################
use Getopt::Std;
getopt('mdy');
## this function returns a century code given a year
sub get_century_code
{
my($year) = @_;
my($century);
$century = int($year/100);
if ($century == 17) { return 4; }
elsif ($century == 18) { return 9; }
elsif ($century == 19) { return 0; }
elsif ($century == 20) { return 6; }
else
{ print "Can't find century code. Century = $century, Year =
$year\n";
print "Year should be between 1700 and 2099\n"; die; }
}
## this function returns 1 if a given year is leap year
## it returns 0 otherwise
sub is_leap_year
{
my($year) = @_;
## if year is divisible by 4
if ($year%4)
{
## if year is divisible by 100
if ($year%100)
{
## if year is divisible by 400
if ($year%400) { return 1; }
## if not
else { return 0; }
}
else { return 1; }
}
else { return 0; }
}
## this function returns the month code given a month and a year
sub get_month_code
{
my($month, $year) = @_;
my($leap_year);
$leap_year = is_leap_year($year);
if ($month == 1)
{
if ($leap_year == 1) { return 0; }
else { return 1; }
}
elsif ($month == 2)
{
if ($leap_year == 1) { return 3; }
else { return 4; }
}
elsif ($month == 3) { return 4; }
elsif ($month == 4) { return 0; }
elsif ($month == 5) { return 2; }
elsif ($month == 6) { return 5; }
elsif ($month == 7) { return 0; }
elsif ($month == 8) { return 3; }
elsif ($month == 9) { return 6; }
elsif ($month == 10) { return 1; }
elsif ($month == 11) { return 4; }
elsif ($month == 12) { return 6; }
else { print "Can't find month code. (Invalid month?) Month = $month,
Year = $year\n";
die; }
}
## this function checks the sanity of the given input
sub check_sanity
{
my($month, $date, $year) = @_;
my($leap_year);
## if year is out of range
if ($year < 1700 || $year > 2099)
{ print "Please enter a year between 1700 and 2099\n"; die; }
## if month is out of range
if ($month < 1 || $month > 12)
{ print "Invalid month!\n"; die; }
## if date is below 1
if ($date < 1)
{ print "Invalid date!\n"; die; }
## if date is greater than 31 for some months
if ($month == 1 || $month == 3 || $month == 5 || $month == 7 ||
$month == 8 || $month == 10 || $month == 12)
{
if ($date > 31)
{ print "Invalid date!\n"; die; }
}
## if date is greater than 30 for some months
elsif ($month == 4 || $month == 6 || $month == 9 || $month == 11)
{
if ($date > 30)
{ print "Invalid date!\n"; die; }
}
## for february
elsif ($month == 2)
{
$leap_year = &is_leap_year($year);
## allow only upto 29 days for leap years and
## 28 days for non leap years
if ($leap_year == 1)
{
if ($date > 29)
{ print "Invalid date!\n"; die; }
}
else
{
if ($date > 28)
{ print "Invalid date!\n"; die; }
}
}
}
## function returns the year code given a year
sub get_year_code
{
my($year) = @_;
my($last_two_digits);
my($divide_by_4_quotient);
my($divide_by_7_remainder);
my($sum);
## get the last two digits of the year
$last_two_digits = $year % 100;
## divide year by 4 and take the quotient
$divide_by_4_quotient = int($last_two_digits/4);
## add the results up
$sum = $last_two_digits + $divide_by_4_quotient;
## divide by 7 and take the remainder
$divide_by_7_remainder = $sum % 7;
## that's the result
return $divide_by_7_remainder;
}
## function returns a number (0 thru 6), 0 corresponding to saturday
## and 6 corresponding to friday
## function takes month, date, and year as arguments
sub get_day_of_week
{
my($month, $date, $year) = @_;
my($month_code);
my($century_code);
my($year_code);
my($day_of_week);
$month_code = &get_month_code($month, $year);
$century_code = &get_century_code($year);
$year_code = &get_year_code($year);
## add month code, century code, year code and date,
## divide by 7, and take the remainder
$day_of_week = ($month_code + $century_code + $year_code + $date) % 7;
return $day_of_week;
}
## show usage information
if (!defined($opt_m) || !defined($opt_d) || !defined($opt_y))
{ print "Usage: day_for_date.pl -m <month> -d <date> -y <year>\n"; die;
}
$day = get_day_of_week($opt_m, $opt_d, $opt_y);
print "$opt_m/$opt_d/$opt_y (MM/DD/YYYY) is $day (where: 0 - Sat ... 6
- Fri)\n";