Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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:

# 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.

In reply to Re: TIMTOWTDI meets Rube Goldberg by graff
in thread TIMTOWTDI meets Rube Goldberg by Tanktalus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found