Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^6: Global variable vs passing variable from sub to sub

by Steve_p (Priest)
on Sep 15, 2004 at 16:54 UTC ( [id://391242]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Global variable vs passing variable from sub to sub
in thread Global variable vs passing variable from sub to sub

merlyn said "Both 'passing around' and 'global variables' are a signs of a misdesign....provide a higher-level interface that don't need to expose that variable...create a module..."

merlyn was explaining the Too many parameters code smell. If you are passing the same parameters around constantly from function to function, then they belong together in a single module or object. The quote that seems most appropriate is:

TooManyParameters is often a CodeSmell. If you have to pass that much data together, it could indicate the data is related in some way and wants to be encapsulated in its own class. Passing in a single structure data that belongs apart doesn't solve the problem. Rather, the idea is that things that belong together, keep together; things that belong apart, keep apart;....

That sounds like a script using local variables might require a different structure.

Not really. If you are properly scoping your variables, it would require very few changes to the structure of your script. What you would do is tease out the reusable or highly related portions of the code into separate modules.

Obviously, scoping a variable to a certain block means it shouldn't be needed outside that block (or "region"), and that takes more planning, and some luck.

Again, not really. If you notice that you are repeating yourself in your code needlessly, then separate it out. If the repeat is pretty narrow in scope, adding a new function is all that maybe needed. If its more complex, or you can use the code elsewhere, a module may be needed. This involves little planning and very little luck. Then, getting the data you need elsewhere would require a function call.

Another obvious bit of effort comes from declaring variables, and even by using "my".

Well, typing three characters ("my ") does involve some effort, but its pretty minimal. The benefits, however, in having scoped code, such as: the ability to test your code in an automated manner; reusability of code; the fact that smaller pieces of code are easier to understand, debug, and fix; etc., are much more cost effective than the alternative.

  • Comment on Re^6: Global variable vs passing variable from sub to sub

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-20 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found