Slash Boxes
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

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • A perfect example to illustrate one of your points: I was working on some code when I ran across several instances of a variable named $number. Since this was one of our ubiquitous several hundred line subroutines that we are cleaning up, it was very, very frustrating to see such a useless variable name. After a bit of hunting through the code, I finally found the following:

    # $number is invoice number
    if ( defined $number ) {
        my @results = $dbh->selectrow_array(...);


  • I'd like to recommend the debugger's comments and documentation (even if I did write them - [] for the formatted POD and [] for the raw source). I worked very hard to make them work both as an integrated whole (the POD summarizes, the comments detail).
  • I keep writing these off-the-cuff essays to people in response to questions and I really should archive them somewhere and turn them into something.

    Yes, you should.
    Kirrily "Skud" Robert
  • Nice writeup, and well put.

    Another point that I would have made is: often, novices structure code in weird ways, with procedures that aren’t very useful outside the context in which they were conceived because they are big and do too many things. They use variables that get recycled over too many intermediate calculations, or conversely fail to break calculations into intermediate steps saved in variables.

    Naming is a good detector for such problems. If you can’t think of a good precise name

    • When I name an array after the objects it contains, I don't know whether to use the plural, or the singular, ie whether to call it objects, or object.

      @persons means I start saying $persons[0], $persons[2]. @person reads nicer when referring to the elements, $person[1], $person[2].

      But the second might confuse a reader of the code?
      • No don't name it after the members. Name it after the collection. Thus, the array becomes @person. When read out loud, it becomes "the person array".

        This avoids ever having to ask the "is it singular, is it plural?" question. Name all your variables in the singular, even if, in some corner cases it comes out sounding a little weird.

        If everything is always singular, no exceptions, you never have to stop and pause to think about its name. This optimisation pays off big time in the long run.

        • My array and hash names are always singular, but not my scalars. When a scalar is intended to store an arrayref, I use the plural, and when it’s intended to store a hashref, its name ends in _for.

          I agree otherwise, though.

          • Can you sketch an example?

            I can't see how it can be a win over a blanket "no plurals" regardless of type and/or content. But you're no dummy, so I'd like to if I can be convinced :)

            • It’s basically the same as people who like to append _ref or _r to the names of their reference-storing scalars. The only difference is that with this scheme, the code reads more like plain English.

      • For arrays and hashes, I always use the singular form. The plurality is already implied by the sigil – putting it in the name as well would be repetition. Note also that there are many cases where you use the collection as a whole, which read nicer with a singular name, such as push @person, $last_record (OK, I suppose that can be argued either way) or keys %person_name.

    • I read in a software engineering textbook about a South African software company that went broke, because its code had been written by Portuguese speakers, who used Portuguese for their variable names. The Portuguese programmers left the company, and were replaced by Afrikaans-speaking ones, who couldn't maintain the code, because they couldn't understand Portuguese.

      I found this hard to believe, but I've never had the experience.
      The textbook writer, from South Africa, said names must be in English.

      I was thi
  • would be an awful lot of fun. :) "It was a dark and storm night in front of the green glow of the monitor as I snacked on pop tarts and contemplated what I am about to write to you....". One of my Engligh teachers in high school was his great-great granddaughter which made for a lot of fun at times. :)
  • Hi Schwern!

    Thanks for taking the time for writing this and thanks for sharing it with us. I couldn't have said it better myself.<tm>.
  • "I keep writing these off-the-cuff essays to people in response to questions and I really should archive them somewhere and turn them into something."

    What can I do to help make "Dear Schwern:" a reality?