Hello Perl Monks

From the time I matured as a programmer I realised that the code should be written for tomorrow and time should not be spend on reinventing the wheel but to make it more efficient. My Sir under whom I learned and sharpened my programming skills used to maintain his own subroutine library. And so I also started maintaining my own set of subroutine library. As I was the only one who used these subroutines and so whatever I wrote was the Best. Currently I am maintaining my own subroutine file for Perl programming. This week as I was getting ready to put one of my subroutine on this site I saw some Perl programs which came along with the installation and I changed my mind. What I see here is that there is some standard followed while writing subroutines. So I request all the monks out there to share their knowledge about what are their styles and standards for writing subroutines, so that it helps Perl programmers like me.

Regards

Jamnet

Edited 2001-04-09 by mirod: moved from SOPW to Meditations.

Replies are listed 'Best First'.
(Ovid - subroutine standards) Re: Standards for Writing Subroutines
by Ovid (Cardinal) on Apr 09, 2001 at 16:32 UTC
Re: Standards for Writing Subroutines
by arhuman (Vicar) on Apr 09, 2001 at 16:33 UTC
    Using Super search with words 'style' 'good coding' 'good programming' should you provide you tons of useful nodes and advices (too many too mention).

    Here are few :
    • Keep it simple stupid
    • Use strict (when writing them)
    • Use explicit name (for the sub and the var)
    • Document your code
    • Make your code readable (it's even better than just documenting)
    • Don't document your code TOO much (tilly convinced me on this one...)
    • Be efficient (choose the best algo/structures)
    • Test your code.
    • Make your code upgradable (let you a chance to change any part of the design)
    • Make your API clear (a lot of subtle error come from a misunderstood API)
    • Subroutines should fit on a screen (old programming practice, but usually good. If it doesn't it should be possible to break into several smaller subs...)

    All in one if I should give you only one advice, I would say :
    Just take what you find clever/elegant/useful in other people way of coding and include it to your style.
    (and I guess you'll find a lot to learn from the coders hanging in the monastery)

    Any way if you are proud of it, and dare to show it to others, It should mean that it's ok...
    ;-)

    "Only Bad Coders Badly Code In Perl" (OBC2IP)
Re: Standards for Writing Subroutines
by frankus (Priest) on Apr 09, 2001 at 16:40 UTC

    This is what I've done: create a repository, although most of my items are one line fragments and not sub-routines, at present it's got a directory per idiom, but soon it'll be in postgres and fairly cook.

    Best practices is a big can of worms, Ideally subroutines should be written like any other code, concisely, legibly etc. Are you asking for details on whether to prototype and such?

    I kinda did functions at university in lisp, but in learning the Perl way, I did books, and lo they were good:

    • A pretty good book with some generic coding standards is Kernigan and Pikes "Practice of Programming" chapter 2 seperates well-written from beautification.
    • "Effective Perl Programming" also has some insights into the matter, highlightig issues with prototypes.
    • "Debugging Perl is quite useful on functions, and since reading I've increased my use of return values to return status.
    • Damian Conway's "OO Perl" has two blindingly good chapters on what you should already know & what you mightn't already know. There is loads of goodies in there on AUTOLOAD and gifts that can spice up your subroutines ;-)
    The only verbal hints I got were, "it's script dummy, check your failures and leave as soon as possible", which works for me.

    Now I write functions to be called from a 'jump table' a hash of references to functions, my initial reason for doing this was deprecated when I discovered AUTOLOAD. Other than that I use subs to perfom one overall function and further more low-level functions under that, I like abstraction and subs to be no more than 60 lines 1.

    1. Which is a marked increase from my Lisp days when functions never grew longer than 4 lines.

    --
    
    Brother Frankus.
Re: Standards for Writing Subroutines
by Beatnik (Parson) on Apr 09, 2001 at 16:50 UTC
    perlsub, perlmod and perlmodlib are definitly worth reading...

    Greetz
    Beatnik
    100 nodes of pure crap
    ... Quidquid perl dictum sit, altum viditur.
Re: Standards for Writing Subroutines
by Boldra (Curate) on Apr 09, 2001 at 19:03 UTC
    Funnily enough the quote engine at the top of the page answered this in the way I feel is most important:

    "Be consistant"

    Recently I have spent quite a bit more time looking at other people's code than writing my own, so I've developed some pretty strong opinions about what makes good style. (I like subroutines to be less than about 40 lines long and I like them to have explicit return values). Nevertheless, the most important thing for me is consistency. Providing the style of code I'm reading holds throughout the whole program, I can adapt. If it changes for every subroutine with no discernable patter, I get s##tty

    Ironically, consistency has to be very carefully balanced with actually improving your style :)