[CrackMonkey] Evolution of Code
Ben Brockert
ben at mepotelco.net
Sat Apr 21 23:15:43 PDT 2001
Code evolves. Every time someone looks at a piece of code, they are
bound to spot at least one thing that could be changed to make it
more flexible, more efficient, or work better.
You all know that. I even know that. That's the basis of one of the
big arguments for open source.
What I want to know is the microhistory of it.
I started with a piece of perl code from my indexing search engine,
which is working now. The code was this:
print 'Done in '.(int($t/86400)).' days, '.(int(($t%86400)/3600)).'
hours, '.(int(($t%3600)/60)).' minutes and '.($t%60)." seconds.\n";
It took $t, which was the time in seconds it took for the script to
run, and output it in a more reader friendly format.
Then, because I'm a bored geek that geeks, I thought I'd make it
into a subroutine. Thus came this:
sub howlong($){
my $t=$_[0];
my @l;
$l[0]=int($t/86400);
$l[1]=int(($t%86400)/3600);
$l[2]=int(($t%3600)/60);
$l[3]=$t%60;
return @l;
}
It takes a number of seconds and gives back the number of days,
hours, minutes, and seconds. Then I thought of another thing it
could do, take two times and figure out the difference itself. Now
you could call it as howlong(1000000,time) or howlong(time,999999).
sub howlong($;$){
my $t=$_[0];
my @l;
if ($_[1]){ $t=abs($_[1]-$_[0]); }
$l[0]=int($t/86400);
$l[1]=int(($t%86400)/3600);
$l[2]=int(($t%3600)/60);
$l[3]=$t%60;
return @l;
}
Ugly, eh? Then, as usual, I actually looked at the functional code
and streamlined it:
sub howlong($;$){
my $t=$_[0];
if ($_[1]){ $t=abs($_[1]-$_[0]); }
my @l=(int($t/86400), int(($t%86400)/3600), int(($t%3600)/60), $t%60);
}
Then I got bored again and decided to e-mail this list.
So, how should this function continue to evolve? If I've learned one
thing in my time of perling, it's that I never find the optimal way.
More information about the Crackmonkey
mailing list