Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Not golfing, because you won't get decent code from golfing horrible code.

I'd rather see this code as a spot-the-mistakes execise.

Thus, here come my changes:

my @quarters = qw/none first second third fourth/; sub termgrades { my ($period, $quarter) = @_; $sth = $dbh->prepare_cached( qq{SELECT * FROM $quarters[$quarter]quarter WHERE userid = ? AND period = ? AND gradetype = ?} ) or &error( "2", "$userdata[0] $quarters[$quarter] quarter " . "grades database search failed: $DBI::errstr" ); $sth->execute( $userdata[0], $period, 2 ) or &error( "2", "$userdata[0] quarter grades " ." database search failed: $DBI::errstr" ); }

Changed the @quarters array to avoid a minor bug. Thanks to itub.

However, the first blunder, the one that eventually led to the current horrible code, is that there are four database tables instead of one.

Thus the first order of business would be to merge those four tables into one, adding a "quarter" column.

With that in mind, you could rewrite the above function as follows:

sub termgrades { my ($period, $quarter) = @_; $sth = $dbh->prepare_cached( qq{SELECT * FROM quarters WHERE userid = ? AND period = ? AND gradetype = ? and quarter = ?} ) or &error( "2", "$userdata[0] quarter $quarter grades " . "database search failed: $DBI::errstr" ); $sth->execute( $userdata[0], $period, 2, $quarter ) or &error( "2", "$userdata[0] quarter grades " ." database search failed: $DBI::errstr" ); return $sth; # perhaps ... see text }

That said, this function, in its original form, is completely useless, because it is executing a SELECT request from a locally scoped statement handler, and its result would be simply lost. A returning statement for such a handler is needed, if you want to pursue any practical purpose.

What else? Ah, yes, the global @userdata array, of which we have just to assume it exists and has valid data in it. It should be passed as an argument as well.

FInally, that naked numeric literal "2" used as an argument to "execute" and "error" should be turned into a variable, so that whoever has to maintain the code knows what it is about.

In reply to Re: I present to you... Horrible code! by cchampion
in thread I present to you... Horrible code! by BUU

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (5)
    As of 2018-06-23 03:39 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (125 votes). Check out past polls.