Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^2: The joys of bad code

by revdiablo (Prior)
on Oct 26, 2004 at 16:47 UTC ( #402702=note: print w/replies, xml ) Need Help??

in reply to Re: The joys of bad code
in thread The joys of bad code

Strangely enough nobody's offered bad code they wrote

Is this a challange? Oh boy, do I have some bad code that I've written. Of course, I know better nowadays. But back then... boy oh boy. Let me dig through my dead code archive for some examples.

Let's start with a nice subroutine to convert a decimal number to its binary form:

sub bin { my $num = shift; my $bin = ""; for my $digit (128, 64, 32, 16, 8, 4, 2, 1) { if($num >= $digit) { $num -= $digit; $bin .= 1; } else { $bin .= 0; } } return $bin; }

The name is pretty bad to begin with, but the real problem is the brute-force algorithm. If only I'd known that I could've written it like this instead:

sub bin { substr unpack("B*", pack "i", $_[0]), 0, 8; }

Or even:

sub bin { sprintf "%08b", $_[0]; }

Then of course, there's code like this, which just makes me shudder:

if(-e "$bmfile") { &addlink("$bmfile","$url","$desc"); } else { &newbmfile("$bmfile","$url","$desc"); }

Or how about this doozy:

LINE: foreach $line (@bmfile) { chomp($line); if($line =~ /title/i) { ($null,$title,$null) = split(/\<\/*title\>/i,$line); } elsif($line =~ /a href/i) { ($null,$link,$null) = split(/\"/,$line,3); ($null,$desc) = split(/\>/,$line); ($desc,$null) = split(/\</,$desc); } else { next LINE; } if($link) { $links[$#links+1] = "$desc,$link"; } }

Wait, wait! There's more!

while(defined($line = <IN>)) { chomp($line); @chunks = split(/\s+/,$line); $ctr = 0; while(defined($chunk = $chunks[$ctr])) { if($chunk =~ /HREF/) { ($null,$url) = split(/\"/,$chunk); print "$url\n"; @full = split(/\//,$url); $dir = $full[5]; $file = $full[6]; system("wget $url"); unless(-e "$dir") { mkdir "$dir",0755; } rename "$file","$dir/$file"; } $ctr++; } }

I can keep going all day:

open(IN, "<$accesslog_file") or die "Cannot open $accesslog_file for input: $!\n"; while(defined(my $line = <IN>)) { chomp $line; if($line =~ /(.*?) - - \[(.*?)\/(.*?)\/(.*?):(.*?):(.*?):(.*?) ( +.*?)\] \"(.*?)\" (.*?) (.*?)/) { my $host = $1; my $day = $2; my $month = $3; my $year = $4; my $hour = $5; my $minute = $6; my $second = $7; my $timezone = $8; my $request = $9; my $response = $10; my $unknown = $11; $host = &dns_lookup($host); # push information into our hash # adds a reference to the log entry array into # the hash element for this host push @{ $hosts{$host} }, [ $month, $day, $year, $hour, $minute, "'$request'", $response ]; } } close(IN);

So yeah. Now that I've let some of this stuff out there, and everyone thinks I'm a terrible coder, are you happy? Did I live up to your challenge? :-)

Update: added <readmore> tags.

Update: I guess my willingness to post my own old, bad code gets me downvotes. How very pleasant.

Replies are listed 'Best First'.
Re^3: The joys of bad code
by jayrom (Pilgrim) on Oct 27, 2004 at 14:08 UTC
    I can't believe you got downvoted!
    I wish I could have upvoted you more than once for your willingness to show yourself in an unflattering way.
    I, for one, has only to look at yesterday's code to already find stupid things that could be rewritten better... but I'd rather not share them ;-)


      I can't believe you got downvoted!

      It must have just been a random thing, because the node rep is now quite positive. I probably should not have put that snarky update. It seems silly now that I think about it.

      I, for one, has only to look at yesterday's code to already find stupid things that could be rewritten better

      Yeah, mine is probably still not perfect, but what I pasted is 3 or 4 years old. I'd like to hope I do better stuff than that nowadays. :-)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2018-06-18 19:09 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.