Think about Loose Coupling | |
PerlMonks |
Re: TIMTOWTDI meets Rube Goldbergby graff (Chancellor) |
on Aug 07, 2011 at 04:49 UTC ( [id://919026]=note: print w/replies, xml ) | Need Help?? |
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: 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: This $datestr variable was dutifully copied into all 48 print statements.
In Section
Meditations
|
|