Think about Loose Coupling | |
PerlMonks |
Re: To sub or not to sub, that is the question?by dimmesdale (Friar) |
on Jun 25, 2001 at 23:36 UTC ( [id://91402]=note: print w/replies, xml ) | Need Help?? |
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.
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: Then what you could do is this: init_all_variables() could just be a function that calls the init_varX functions. This method is very useful when it is needed.
In Section
Meditations
|
|