Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Do I write one large program or lots of small programs?

( #304314=categorized question: print w/ replies, xml ) Need Help??
Contributed by bradcathey on Nov 04, 2003 at 03:36 UTC
Q&A  > subroutines


Description:

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:

On subroutines:

  • 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
On modules:
  • "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)
Refactoring: streamlining your code
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!
  • 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:
    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
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (7)
    As of 2014-09-16 11:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (10 votes), past polls