I have a number of habits that I use regularly to prevent syntax and certain simple semantic errors.

Firstly, I always type the parens first, then go back an fill in the contents:

if () if () { } if () { } else { } if ($foo) { } else { }

Similarly, I write file-reading loops outside-in:

open my $file, ... open my $file, ... close $file; open my $file, ... while () { } close $file; open my $file, ... while (<$file>) { chomp; } close $file;

Or fire-and-forget database connections:

my $db = DBI->connect(...); END {$db and $db->rollback and $db->disconnect}

This way I can do race ahead doing what needs to be done, and I know that no matter how my programs ends, everything will be undone. It's only after I've tested that the desired results are achieved do I go back afterwards and change the END to commit the db changes.

I know there are auto-completion editors that generate this boilerplate for you, but I've never come across one I could really come to terms with. The main point is that your code should always be runnable (assuming you finish the statement you're writing).

I find this cuts out a lot of edit-compile-kaboom cycles. Once you are confident that the program will run each time without syntax errors, I find you can concentrate a lot more on the problem itself, and this accrued concentration naturally helps you pay more attention to the the larger issues. I hate wasting five minutes trying to track down a poorly-closed heredoc. The time wasted on this issue is an interruption in concentration about the problem you're really trying to solve. Get that sorted out, and you reduce the chance of creating subtle errors in the larger picture.

• another intruder with the mooring in the heart of the Perl


In reply to Re: What habits do you have to avoid introducing bugs? by grinder
in thread What habits do you have to avoid introducing bugs? by superfrink

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.