I like to use early exits because they often save a lot of obfuscating indentation and state management. Test simple cases first and exit early. Complicated cases then tend not to be indented and the conditions that get you to the complicated case are generally much easier to see. Note that this early exit technique works in the context of loops just as well as it does for subs.

To me a single point of exit seems a rather artificial constraint that often requires extra testing and nesting to achieve. Removing that constraint very often allows much clearer logic flow - it's easier for people to understant and prove, which is much more important than making it easier for compilers.

Using undef as the failure case allows 0 to be used as a valid return value. That was part of what I was alluding to in the paragraph about "sanity checking".

C required that all variables be declared at the start of a block. C++ and Perl (to name but two) allow variables to be declared wherever they are needed. So yes, I did mean to take advantage of that feature of Perl (and other languages) to declare variables as close as possible to their first use.


Perl is environmentally friendly - it saves trees

In reply to Re^3: Use of uninitialized variables? by GrandFather
in thread Use of uninitialized variables? by Zadeh

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.