Contributed by bradcathey
on Nov 04, 2003 at 03:36 UTC
This is a summation of a question
I asked on SoPW: Is it better to have one large program, dividing up the tasks into subroutines; or divide the tasks among several small programs? Then, of course, there is the matter of modules or objects. Also, which method is more efficient—does one run faster than the other?
|Answer: Do I write one large program or lots of small programs?|
contributed by bradcathey
The performance issues notwithstanding (that is a discussion for another day), I think it comes down to a matter of style and personal taste. You have to ask yourself, "Is it easier for me to keep tabs on what is going on in a larger program or several smaller ones?" The answer could vary from coder to coder.
The Perl literature is relatively quiet on the issue, with the exception of O'Reilly's CGI Programming which dedicates a few pages (378-381) to the subject. In a nutshell, the authors favor the larger program model for the ease of editing (having to open, know, and look at only one program), and distribution (only one). The smaller programs would be better when several programmers are working on the project, or for security reasons—allowing access to programs by only those that need them.
Our own Perl Monks tend to favor the former model, one biggie, with the option of putting commonly used subroutines into a module (or see another PM Q&A).
Here is a summary of their comments:
- group your subroutines by task, minimizing the calls between subs in different sections; separating sections clearly in your code
- be able to describe what a subroutine does in one sentence, without using "and" or "or"; i.e, clearly define a single task for each subroutine
Refactoring: streamlining your code
- "If you could simply wish any module into existence (within reason and logic), can you imagine any that would simplify your design? 'Gee, if the Such::and::Such module existed, I'd only have to do this and this, which is really the core of my application, and not all this other stuff.'"
- "Design your functional blocks so that they have minimum cohesion between them. In other words, minimize interaction between modules at the same level."
- "Keep the size of a module to less than a few pageful." (I've heard 50 lines as a gauge)
This is process of methodically editing your code to optimize it for speed, size, and efficiency. Here's a very thorough example of refactoring.
And here's a more thorough discussion of program design.
Thanks to tilly, Art_XIV , Demerphq, Roger, and the ubiquitous Anonymous Monk.
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||