Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: To sub or not to sub, that is the question?

by dimmesdale (Friar)
on Jun 25, 2001 at 23:36 UTC ( #91402=note: print w/replies, xml ) Need Help??


in reply to To sub or not to sub, that is the question?

Well, a lot of good replies have already been offered, but when has that stopped me?

A simple thing to remember is that a section of code should be no longer than a person is willing to read at a time. A person earlier suggested about one page. The exact length may vary, and depends on specific needs. BUT, remember that if someone else has to come in and read it (or you, a while after you wrote it--which brought us this question :) SO, you should make the logic of the code flow. If your function is getting too long and you're finding yourself scrolling up and down to find out something about it, you're probably better off to make some sub-functions(ha; thinking of perl, that's kind of redundant).

If there's a function that's calling too many small functions, there are several possibilities.
(1)Not worry about it
(2)Combine some of them
(3)Make a function that's only job is to call them

To explain point three: if, for example, you have some complex data types that you need to have the user initialize, and your code looks like this:

$var1 = init_var1(); $var2 = init_var2(); $var3 = init_var3(); ...var4, etc...
Then what you could do is this:
($var1,$var2,$var3,..etc.) = init_all_variables();
init_all_variables() could just be a function that calls the init_varX functions. This method is very useful when it is needed.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2020-01-20 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?