Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How NOT to do it

by jlawrenc (Scribe)
on Mar 18, 2001 at 03:53 UTC ( [id://65219]=perlmeditation: print w/replies, xml ) Need Help??

Rants on bad (Perl) programming remind me of a former employee (that I kind of inherited).

To get the length of a string he coded this little bit:

$string="the quick brown fox"; @temp=split //, $string; $string_length=$#temp; $string_length++; undef @temp;
Duh! What happened to "length $string"? Do you honestly think that Perl does NOT have a function to obtain the length of a string? Do you know ANY modern language that does not have such a function? And, no, its not like he was some sort of newbie.

I didn't know whether to slap him upside the head, laugh, cry or fire him on the spot. Then my mind wandered to the notion that this guy has and will leave a path of destruction in his wake. I hope that he moves over to writing in Java sometime soon. :)

J

Update: Corrected his code I pulled from memory. I had forgotten about the increment statement too. So, now it is four lines to do what you could do in 1... If we work at this we could make it worse, no?

Replies are listed 'Best First'.
Re: How NOT to do it
by dws (Chancellor) on Mar 18, 2001 at 04:23 UTC
    Much as I like to believe that some people's behavior can be turned around when gently shown the errors of their ways, there are indeed some people who you should never, never let near a project that you're responsible for.

    Before writing someone off, though, it's wise to check a few things out:

    1. Did this person come out of an environment that rewarded wacky code? (Any "reward" here is usually some peer-level cred thing.)
    2. Is this person bored? Bored people can wreak as much havoc as incompetent people, but they tend to be better at getting the havoc un-wreaked.
    3. Is this person working outside of their area of competence? (This never, every happens to anyone here. Right? :)
    4. Does this person have something going on in their outside life that's affecting their behavior on the project? They may think they're keeping a lid on things. (I've been here a few times.)

    There's probably hope for anyone who falls into one of these categories. Often the mere fact of pointing out the consequences of their behavior is enough to either turn them around or have them send themselves on the way.

    Then there are cretins -- people who leave their Perl golf practice in the code base, or worse. One strategy I've found for cretins is to wait until a headhunter calls, and then refer the cretin to the headhunter.

Re: How NOT to do it
by merlyn (Sage) on Mar 18, 2001 at 09:20 UTC
    You know, I think I'm to blame for this. The Llama book never mentions the length operator at all, and that was by design. Almost any time I've seen people need the length of the string, they were about to cycle through the string character-by-character (as you might need to do in other languages), and therefore miss the more natural and powerful regular expression operations. Since I teach regex very early on, there's usually no need for length.

    The hardest part about a tutorial book is coming up with a teachable, self-consistent, useful subset of the language. Many decisions to be made.

    -- Randal L. Schwartz, Perl hacker

      NO, I disagree! I think that there is a certain level of reasonability to be expected here. If the person in question really thinks about it - Perl's origins stem from the need to crunch through tomes of text. There stands to reason that our traditional string functions like lenght, substring, etc., would be in the language right from the get go!

      I think the hardest part about tutorials, teaching students, or developing staff is fostering the correct foundations of common sense.

      Maybe what we need is a tutorial on how to RTFM. That was one skill I learned early on in my career. :)

      So what is the shortest way we can rewrite this code?

      Kickstart

        Another fellow monk once wisely showed me the path to enlightment with the length func.

        -= Ozzy =-

        Edit: chipmunk 2001-03-18

(ar0n) Re: How NOT to do it
by ar0n (Priest) on Mar 18, 2001 at 06:20 UTC
    What makes it even worse, is that he isn't getting the correct length. He's getting the length of the string minus one, since $#temp returns the index of the last element of the array.

    Sheez...

    [ar0n]

Re: How NOT to do it
by grinder (Bishop) on Mar 19, 2001 at 13:49 UTC
    Do you honestly think that Perl does NOT have a function to obtain the length of a string? Do you know ANY modern language that does not have such a function?

    Um, SQL?

    --
    g r i n d e r
      HA HA HA - touche!
      Obviously a Windoze Axe-S guru. Racle SQL has had a length function since at least version 6

        Actually no, Sybase.

        You actually made me go look at the documentation. Turns out that there does exist such a function in Sybase, but it's called char_leng (talk about obvious).

        In any event, there are length functions in the various SQL dialects, but there is no such beast in the SQL standard (although it's been years since I've tracked the standard closely, SQL-92 is still pretty new-fangled for me). So much for portability.


        --
        g r i n d e r
Re: How NOT to do it
by t0j0 (Novice) on Mar 19, 2001 at 13:29 UTC
    if from your snippet shows that that there's a space in the split @temp=split / /, $string; instead of @temp=split //, $string; maybe what he wanted to do is to count number of words in the string.. which can also be done like this
    $ths = "this is a string"; $i=split(/\s/,$ths); print $i;

      That is not what the original code snippet does (it does in fact count the string length .. hence the variable named $string_length). If you pasted the code into your editor you could see if there was a space in the regex or not (not). Your code to count words, however, not only isn't -w compliant (implicit split to @_), it can very easily give wrong results:

      $ths = ' this is a string'; $i = split(/\s/,$ths); print "$i\n";

      If you want to split a string on whitespace, use the special case of split(" ", $ths) which splits on multiple whitespace and ignores any leading null field (see: perlfunc:split for details).

      $_ = ' this is a string'; my @words = split " "; print scalar @words, "\n"; # or you could go for this version :-) $_ = ' this is a string'; my $words =()= /(\S+)/g; print $words, "\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found