jeri_rl:

Yes, I'd suggest that you use subroutines in your subroutines. Generally the CPU impact is insignificant, but it can make maintenance so much simpler. In general, if you use good subroutine names, and restrict subroutines to doing a particular task, it will make your program *more* readable, rather than less.

I try to make a subroutine stick to a particular task, which tends to help keep them short. It also helps keep them reusable. Suppose I have a function called send_report and it loads up a mailing list from the database, formats an EMail, attaches a report and then sends it to everyone on the list. It's not terribly reusable. Sure, if you have to do something similar later, you can copy it and hack it up a bit. Or, you could chop it into smaller, more reusable parts, something like:

sub send_report { my ($list, $report, $template) = @_; my @recipients = get_list($list); my $email = format_EMail($template); send_EMail( to=>[@recipients], body=>$email, attachments=>[$report] + ); }

This function is pretty easy to read and understand. Sure, the code that does the bulk of the work is in a different subroutine, but that's a feature: If your EMail is formatted incorrectly, you know where to look: format_EMail. You don't have to worry about the code and variables that worries about sending EMail or getting a mailing list.

Another way to determine if your subroutine needs to be split up is to name a subroutine by what it's doing. If you're trying to name a subroutine do_this_and_that, the "_and_" is a tipoff that you need to split it up or that it'll be calling do_this and then do_that.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: efficient use of subroutines by roboticus
in thread efficient use of subroutines by jeri_rl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.