Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by Wassercrats (Initiate)
on Sep 15, 2004 at 04:04 UTC ( [id://391066]=note: print w/replies, xml ) Need Help??


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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Re^5: Global variable vs passing variable from sub to sub

Replies are listed 'Best First'.
Re^6: Global variable vs passing variable from sub to sub
by Steve_p (Priest) on Sep 15, 2004 at 16:54 UTC

    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.

Re^6: Global variable vs passing variable from sub to sub
by dragonchild (Archbishop) on Sep 15, 2004 at 13:37 UTC
    and that takes more planning,

    Programming is a profession built around planning. I would be hesitant to complain that a feature many people depend on should be scrapped because it requires more planning.

    and some luck.

    I'm curious as to where you feel luck enters into the programming arena. It hasn't been my experience that luck plays a factor at all.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://391066]
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: (4)
As of 2024-04-18 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found