Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: Re: Re: Re: where do you put your subs

by Juerd (Abbot)
on Mar 08, 2002 at 15:52 UTC ( [id://150345]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: where do you put your subs
in thread where do you put your subs

I'm not talking about aesthetics, I'm talking about maintenance.

I'm glad we all want easy maintenance!

subs at bottom => sharing of ALL variables used out of the subs => greater chance of having unintended behaviour => more maintenance
subs at top => sharing of explicitly chosen variables => less unintended behaviour => less maintenance

(I assume strict)

44696420796F7520732F2F2F65206F
7220756E7061636B3F202F6D736720
6D6521203A29202D2D204A75657264

Replies are listed 'Best First'.
Why Globals can be a good thing
by dragonchild (Archbishop) on Mar 08, 2002 at 16:44 UTC
    I find this kind of logic to be ... less than satisfactory.
    1. Maintenance includes enhancements.
    2. Every single program will always need maintenance. Every single one.
    3. A maintainer will want to be able to read the code easily, to be able to determine where best to make the change.
    Global variables are a feature. If you misuse this feature, then you will be bitten. This is the same if you misuse any feature. There are perfectly good reasons to make some variables global. However, just like injudicious use of map and grep and $_ (some or all of which some code shops ban, to improve maintainability), misusing globals can reduce maintainability. Using them intelligently can IMPROVE maintainability.

    For example, let's say that you have an object that you use to do your logging. It's a one-stop-logging machine that you use in every single function you have. What options do you have to make sure it's in scope in every function?

    1. You could pass it in to every function. That starts looking like tramp data.
    2. You could make it a Singleton and have it grabbed every time. This simply makes the classname a hard-coded value in your code. (Or, the classname is now a global .....)
    3. You could make the logger (or logger class) global. (Of course, heavy commenting would help here, as well as a set of standards.)
    Remember, the only difference between a constant and a global is that the global can change. But, if you use 2-5 globals in your entire application and they're well-named and well-documented up-front, you shouldn't have any problems.

    Remember - you can always try to get the idiot out of the programmer by force, but it's better just to educate and document. You'll have more success that way.

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

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      I agree with you on every point you have made in the node I'm replying to.

      Globals can be useful, and special globals are simply unavoidable. Further, if you need another package to be able to read a variable directly, it has to be a global. But whenever you need something that is global to the package, you must of course put their declaration before subs. That doesn't mean the entire body should be above the subs. If you have your subs at the end, every single variable declared in file scope is useable by all of your subs! Which in practice means that if you use the same name for a variable, and forget to my it in your sub, you're using the variable that's also used by your script's main code. In most cases, that is NOT handy.

      If you use strict, and use lexicals in your subs, ask yourself WHY you do that. Why do you use strict? Why do you use lexicals in subs? One of the reasons to use strict and use lexcials in subs is to avoid using variables that were meant for another part of the code.

      If you put subs at the end, do you put module loading there too? After all, use is handled at compile time, so it doesn't matter (technically) if you put it near the top or at the end, or somewhere in between.

      Do you first tie your shoelaces and then figure out how you did it, or do you first learn how tieing the laces is done, and then do it?
      Do you read the manual after fully comprehending things, or do you read it in advance?
      Do you drink a glass of tea, and put the tea bag in the water afterwards?
      Is it not far more logical to define things first, and then use them?

      Let's have a look at object orientation. To create the object, you use a constructor method, probably new. After that, you can create other objects, and it's quite possible some methods need objects as their parameters (an HTTP::Request object, for example). You can't use an object until its creation! That's how I think you should look at subs: don't trust the interpreter to look for your sub everywhere, just assume you can use a sub only after having defined it, which is after all the way most things in life work. And it'll also increase maintainability, because only the variables you declare explicitly _before_ the subs are global or semi-global.

      44696420796F7520732F2F2F65206F
      7220756E7061636B3F202F6D736720
      6D6521203A29202D2D204A75657264
      

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-28 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found