Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: TIMTOWTDI meets Rube Goldberg

by graff (Chancellor)
on Aug 07, 2011 at 04:49 UTC ( [id://919026]=note: print w/replies, xml ) Need Help??


in reply to TIMTOWTDI meets Rube Goldberg

If you're not shaking your head in disappointment over that code, you're probably not realising that s/\n+$// does the same job...

Um, I think you mean s/\s+$// does the same job. But anyway, one of the limitations of sharing really bad code is that it's typically too long and too boring to make for a good joke, usually because the most frequent symptom of bad code is copy/paste programming.

I saw a script a few years back whose purpose was to produce a summary report every day, showing year-to-date quantities of data received from about two dozen sources -- so, roughly 24 lines of output. The solution? If "previous day" was the first day of January, no need to compute "year-to-date", so this makes up an "if" block with 24 distinct "print" statements, where the name of each data source is part of a distinct but otherwise identical string being printed, saying the count is 0 for each one. The else block was more "interesting": 24 copies of a 4-line block, where the source name was changed in 3 of the 4 lines. (And 2 of the lines were shell commands in back-ticks to assign strings to two variables being used in the print statements.)

As a bonus, here was the method at the top of the script for getting yesterday's date:

# get the date of yesterday my $nowsecond = time (); my $yestersecond = $nowsecond - 24 * 60 * 60; my @yesterdate = localtime ($yestersecond); my ($day, $month, $year) = ($yesterdate[3], $yesterdate[4], $yesterdat +e[5]); $day = sprintf("%.2d", $day); $month = sprintf("%.2d",$month+1); $year = $year + 1900;
Based on this programmer's general concept of "logic", it's almost surprising that this part wasn't copied into each of the "if/else" blocks -- but it turns out he decided to get the previous day's date as well (though he never used that value), so he repeated the above block, adding a factor of 2 to the subtraction from "$nowsecond". And of course, both the "if" and "else" blocks contained their own copies of this essential "glue" operation:
my $datestr = "$year.01.01-$year.$month.$day";
This $datestr variable was dutifully copied into all 48 print statements.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-19 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found