Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: (Zigster) How to split code into subs

by zigster (Hermit)
on May 04, 2001 at 14:02 UTC ( #77915=note: print w/replies, xml ) Need Help??


in reply to Script design: Complexity/generalisation vs. simplicity/specialization (was: Majordomo Utility...)

To quote another node: Re: Silly code reviews and shift
  • code should be as simple as it needs to be be and no more so.
  • code should be as efficient as it needs to be and no more so.
  • code should be as readable as it needs to be and no more so.
  • code should be as reliable as it needs to and no more so.
  • code should fulfil its technical design brief.
Everything comes at a cost. Writting generic flexible code often (but not always) increases it complexity. You have to look at it with a critical cost/benifit eye. Are you likely to need to reuse this code. If the answer is yes there are other applications for this code, then you are probs best off writting a module and pulling the generic stuff out. If the answer is no then write the code to be as maintainable as possible. Look at the points I have bulleted above, consider your priorities and code to those.

Re reading your question, I seem to detect a query about how to split up your code. How much to put into subs and how much in the main body. Asside from obvious sub candidates to prevent code duplication; consider the following. A technique often used is to write pseudo code in terms of comments. Then replace each comment with a block of code. I then look at each block of code and consider how valuable the comment is. Does the code describe itself such that the comment is not required, if it does I leave it alone. If the comment is still required then I would move the code to a sub to simplify the main block. This pulls out complex code, allowing for nice partitianing and easier maintenence. I then apply a similar process to all subs.

Consider as a rule of thumb not allowing any sub to contain any more code than you can fit onto your screen at a time. Consider the 'main block' as just another sub.

ALWAYS KISS ;-)

UPDATE
Having strolled about pm I noticed:

These provide some different advice and may be of interest.
HTH
--

Zigster
  • Comment on Re: (Zigster) How to split code into subs

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-01-20 02:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (56 votes). Check out past polls.

    Notices?