I feel that consistency is the most important thing for writing maintainable code in any language. Being consistent with your indentation is probably the first thing to get right . The perlstyle man page has lots of recommendations for 'standard' Perl style.

Comments used in moderation can also be very helpful. I always like to include a comment before each subroutine which describes what it takes, what it does and what it returns. Of course these comments are worse than useless if you don't change them when you change your code. On the other hand, this sort of comment probably isn't very useful:

# print out the results print $results;

Using sensible identifier names (eg: variables typically nouns and subroutines typically including a verb) should help make the code fairly readable without having lots of comments.

I also recommend breaking up long sections of code into subroutines with descriptive names. That way someone can 'zero-in' quickly on the section they're looking for.

And no, you wouldn't have to look very far at all to find examples of my code where I have failed to meet my own recommendations :-(

Update: I also meant to point out that searching for maintainable code would probably be another good place to start.


In reply to Re: What is maintainable perl code? by grantm
in thread What is maintainable perl code? by disciple

Title:
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, details, 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, summary, 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.