Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

gav (2710)

gav
  (email not shown publicly)
http://www.estey.com/
AOL IM: flufflegavin (Add Buddy, Send Message)

Hacker in NYC.

Journal of gav (2710)

Wednesday September 11, 2002
02:09 PM

delving into the sludge

[ #7679 ]
What do you do if you see things like this in a script a co-worker wrote:
1.

   if ($dbh) {
        return 1;
    } else {
        return 0;
    }

2.

$searchsql = sprintf("%s%s",$searchsql,$sql);

3.

while ($stopsearch == 0) {
my $rv = $sth->execute || die $dbh->errstr;
# loads of commented out code
if ($rv eq '0E0') {
   $stopsearch = 1;
} else {
  # lots more code
  $stopsearch = 1;
}
}

In total there is about 400 lines of code. I wrote a replacement for a similar script that had more features in about 230. He decided not to use my script and just rehash his old one. He also doesn't seem to like using the parsing library I wrote and prefers using about 30 lines of:

($price) = ($input =~ m/<pr>(.*)<\/pr>/si);
($saleprice) = ($input =~ m/<sp>(.*)<\/sp>/si);
# etc

I've tried to subtly suggest to him and my boss that I could help him with his programming. So far I have been ignored. What can I do?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Maybe you can get him to tap into the community, through this site, perlmonks, various books, etc. Send him links to interesting articles from time to time, until he is reading these sites regularly and learning. He may give more credence when he can see the consensus behind various practices, and he's sure to learn something.

    One of Larry's credos is that it's okay to program in Perl baby talk, and we won't laugh. That may be so, but I'm still pretty ashamed of some of the things I produced more than a

    --
    J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
  • The first one is actually justified in Perl in the case that you need to return numeric 0 or 1, although you might prefer the ?: operator:

            return ( $dbh ? 1 : 0 );

    Some people, not I, stay away from the ?: like the plague because they think it's unreadable.

    There's a trick I've used in C to turn an integer into 1/0:

            return !!dbh;

    This is too clever, I think, simply:

            return ( dbh != 0 );

    Is probably preferable.

    B

  • What do you do if you see things like this in a script a co-worker wrote:

    Nothing at first. Do they work? Yes. Are they icky? Sure, but they also work. Are they wrong? No, not at all.

    Now, I'm not saying you ignore it, but be logical and methodical about it. "Seek first to understand, then to be understood." Try to understand why the code is written that way. For example:

        if ($dbh) {
            return 1;
        } else {
            return 0;

    --

    --
    xoa

  • Off the cuff, here's how I would transform this stuff and why.

    if ($dbh) {
      return 1;
    } else {
      return 0;
    }

    That would become:

    return 1 if $dbh;

    Unless there is a demonstrable need to return zero for false, I consider it a bug. What if the results are assigned to an array? The array then has one element, zero, which causes an if (@array) conditional to incorrectly evaluate as true. Further, since boolean tests don't warn about undefined values, why bother to ever return the

  • Oh, just kill him and feed him to the neighborhood cats!