Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

++greengaroo, your list contains good advice. I would like to add some of my own. But, first, I do have a few quibbles:

6) Raise a flag, when creating if-elsif blocks without an else. Double-check that you don’t need an else. In doubts put one that would provide an error message.

This should stated more generally:

Be mindful of corner cases. Remember Murphy’s Law! For example, when creating if-elsif blocks without a closing else block, ...
8) Don’t pass big structures directly as an argument to a subroutine or method, use references instead. You never know how big your structure could become and it would use a lot of memory.

This is true in C, but not in Perl, where subroutine arguments are passed by reference, not by value. See perlsub:

The array @_ is a local array, but its elements are aliases for the actual scalar parameters. In particular, if an element $_[0] is updated, the corresponding argument is updated...

Of course, once the argument has been passed in, it is common practice to assign it to a local variable, and this assignment does make a copy. So point 8) is still good advice, but needs to be re-stated (and generalized). For example:

Don’t make an unnecessary copy of a large structure when a reference will serve instead.
14) follow these [debugging] steps:

This tip should definitely include a reference to Basic debugging checklist by toolic.

Now to my proposed additions:

16) Observe the Principle of Least Privilege. Don’t use a global variable where a lexical variable will do. Don’t declare your variables until they are needed. Use accessor methods on objects. Use blocks to minimize the scope of local, no strict 'refs', etc.

17) Avoid premature optimization. Remember,

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. — Donald Knuth

In fact, the Wikipedia article on program optimization says it all:

A better approach is therefore to design first, code from the design and then profile/benchmark the resulting code to see which parts should be optimized. A simple and elegant design is often easier to optimize at this stage, and profiling may reveal unexpected performance problems that would not have been addressed by premature optimization.

18) Study Chapter 21, “Common Practices,” of the Camel Book.

Hope that helps,

Athanasius <°(((><contra mundum


In reply to Re: 20 tips to avoid pitfalls while Programming Perl by Athanasius
in thread 20 tips to avoid pitfalls while Programming Perl by greengaroo

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (3)
    As of 2018-07-19 04:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















      Results (402 votes). Check out past polls.

      Notices?