Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Question on "my" in the variables

by einhverfr (Pilgrim)
on Feb 25, 2014 at 04:14 UTC ( #1076081=note: print w/ replies, xml ) Need Help??


in reply to Question on "my" in the variables

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.


Comment on Re: Question on "my" in the variables

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1076081]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2014-12-18 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (48 votes), past polls