Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Just some random comments here... my basic principle (and this is just a layman's opinion ;-) is to isolate any repeated sequence of operations by placing them in a function/subroutine with a descriptive name. This can certainly be carried too far, so in practice I make functions of things that are becoming irritating to type or cut/paste from elsewhere.

I certainly agree with your first three points, and I'd like to add that the reduction of repetition also reduces coding errors. Even (especially?) when I cut and paste, I can introduce variations or subtle errors (often involving scoping) which are entirely avoided when I take the time to make a subroutine.

I find that building a sub forces me to think more fully about what exactly I'm trying to express as I try to make it as much a black-box as possible. Subs also give me ample room to add better error-checking and handling that may (gasp!) get left out if I were to strip down the operation and leave it in-line. And after all this extra effort, I get something that I can re-use elsewhere more easily than some sequence of lines from the middle of a big loop.

On your second set of points:

1 - On looping, I just don't like deep indentation, and especially if chunks of the loops are nicely isolated, I will put them in subs just to unclutter the structure, which leads me to commenting:

2- I tend to think the fewer comments the better, and that's not to make things harder for others. I mean that whenever I find myself making any comment at all (apart from header blocks which should be quite detailed) I ask myself just what is so confusing here, why isn't the code obvious, and can I make it obvious and avoid the comment altogether? Jumping from sub to sub shouldn't be confusing if they each do something that makes sense on it's own. For a trivial example, in $a = sin($b) * cos($c) the functions each have clear and obvious purposes of their own, and the thought of calculating them in-line would be a great starter for the "fattest obfuscation" category...

Finally there is the consideration of performance especially if you are passing a lot of data to a function (in which case you should probably pass a reference anyways but there are always issues...). I'd like to think that the compiler (speaking generally here) should optimize what I write and not really care if it's a subroutine or in-line, but again in practice this isn't the case (yet anyway) so it may well be that using a sub call can slow down an operation that I will perform millions of times to the point that I shouldn't make the call. When I wrote a lot of C I enjoyed making elaborate preprocessor definitions to get the best of both worlds, and to some extent I miss that in Perl.

I look forward to the more professional opinions of the learned monks on this question!

I'd like to be able to assign to an luser

In reply to Re: When do you function? by Albannach
in thread When do you function? by zdog

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
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (3)
    As of 2018-05-20 16:50 GMT
    Find Nodes?
      Voting Booth?