I am going to add another reason to use "my" and that is testing.

Here is my background. I am a core maintainer of LedgerSMB and as such we have a lot of legacy code we inherited from SQL-Ledger, which almost never lexically scopes variables. If left alone the code works ok most of the time, but it is relatively brittle and it cannot be tested because of sloppy scoping. Our focus is mostly in removing this code and replacing it with something more manageable.

Lexically scoping variables is a prerequisite for being able to sanely write test cases. Very often times you really need to be able to make repeated function calls in ways you might not necessarily want to do in the general operation of your application. Without lexical scoping, the variables are not reset on function call and consequently things like running totals in reports keep on running. Managing this in a test case is a mess.

I personally spent 8 hours one day working on getting a single report we inherited to pass automated test cases. The report, mind you, passed *manual* test cases consistently, but the sloppy scoping meant that automated test cases invariably failed on second and subsequent runs.

Without automated test cases, you can't ever be really sure that code contracts you specify today will be upheld in future versions even if you don't intend to change them. Without thinking through variable scope, you can't have automated test cases. So scope your variables and scope them as tightly as you can.


In reply to Re: Question on "my" in the variables by einhverfr
in thread Question on "my" in the variables by Anonymous Monk

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.