Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Question on "my" in the variables

by einhverfr (Friar)
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1076081]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2017-05-23 04:07 GMT
Find Nodes?
    Voting Booth?