Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I've read that article -- a long time ago and just re skimmed it. It doesn't really say much that's is surprising to someone who uses prototypes all the time -- even on methods where their typing is pointless (documentation benefit for me, but pointless as far as catching errors).

**If** I have routines that are not called and/or they are in areas fraught with danger, I do many checks. Example: this is a snippet of code I wrote for a util for _myself_.

The structure of the sub, is such that it's body doesn't begin until 118 lines below the 'sub'. In the intervening lines are nested subroutines to simplify the code below. Just above where the main code starts (the main code being 25 lines long!), there are two routines (36 lines) of code that JUST does error checking.

## "Devel" - extra checking code local *validate_returned_pointer = sub ($$) { my ($rp,$mpn)=@_; my $refp= ref $rp; $refp || die P "$self: undef inode returned in arg# %d", $mpn; $rp->can('Ino') || die P "$self: Bad type: %s: Cannot get ". "Ino num in arg# %d", $refp, $mpn; $rp->Deleted_Ino_chk && die P "$self: Deleted Ino returned in arg# %d", $mpn; }; use constant first_call => 1; use constant subsequent_call => 2; local * param_checks = sub ($) { if (scalar @$retp) { die "Error -- passed back params when Size index and\n". "next_in_Q are in pre-run state (%s, %s)", $mp->{Size2ss_n +odes}, $mp->{nextbase} unless $mp->{Size2ss_nodes} || $mp->{nextb +ase}; TPe "$self: received %s params", scalar @$retp; die P "$self: Too many arguments (%s)", scalar @$retp if @$ret +p > 2; # validate returned poin +ters for (my $mpn = 0; $mpn <= $#$retp; ++$mpn) { validate_returned_pointer($retp->[$mpn], $mpn); } return subsequent_call; } else { die "$self: takes 1 or 2 params: a surviving inode\n". "if a link occurred, or both there was no linking.", if $mp->{size_idx} != 0; return first_call; } }; state $exec; #performs same 'function' as first/subseq call w/o + checks if (($mp->{optimize} and $exec||=$exec++) || param_checks() == subsequent +_call) {
Note the 'optimize' param -- I fully expect those routines not to be executed in the final solution (though they make <1% of a difference in overall execution speed).

Also that entry point is the only place those routines are used.. and they just do parameter checking (even my throw away error routines have prototypes!)

....Vs.....the code above... is the largest single execution/cpu sucker of any part of a program. Adding anything that would slow down runtime -- would directly harm performance -- since the code is critical path code that is frequently called.

I don't often find a need for dynamic types. Your example would be rare in my code, since 3 params is about the cutoff point for passing stuff in a reference or hash in my usage (as a general rule). So my prototype would look like

sub bar($) {...} # and the calling routines would use either... [1,2,3,4] # or {p1=>1, p2=>2...}
Cuz I have a pretty short, short term memory -- unless I sprinkle my code with lots of hints (including prototypes), I'll find it near impossible to figure out what my "previous self" wrote or was doing.

I've sat down and looked at code written 18-24 months prior and didn't even recognize it for my coding style, let alone as code that I wrote!

So when I put in lots of error checking, it's not to protect others so much, but to protect myself from myself! Almost any error that can possibly happen I will eventually have happen in my code. So better to write the code defensively up front and save myself headaches later.

Make sense? ;-)

In reply to Re^8: How to return values from a hash? by perl-diddler
in thread How to return values from a hash? by perl-diddler

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    [james28909]: why, what is the point. if you cant see that we are smarter than cavemen then i need to just shutup lol. cavemen didnt propell themselves onto a moon. they stared at it and probably howled
    [SuicideJunkie]: We are certainly vastly more educated and wealthy. Raw intelligence is much more difficult to measure.
    [james28909]: exactly. i think the question that should be asked is, where will intelligence take us.

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (8)
    As of 2017-12-15 15:07 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (433 votes). Check out past polls.