Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
...and when to cut and paste code...

The idea of cut and paste code makes me shudder as it tends to create bugs very easily (speaking from experience), the checking and correction of which may well consume any time saved by the cut and paste operation. Fortunately I think it can often be avoided:

  • if you are pasting code and not changing it (i.e. it is a duplicate) then that certainly needs to be a subroutine (or sometimes just a loop depending on the complexity). Remember that if the source and destination of the cut and paste are identical now, they should probably always be identical, and the only way to ensure that is to use a single subroutine.
  • if you are pasting code and changing the copy slightly, it may still be better as a subroutine if the code is fundamentally the same.
  • as others have said, if you have a hard time understanding code with a lot of subroutine calls, then you probably need better names. After all, the built-in Perl functions are also aggregating code and you're far better off calling split than pasting in code to do the splitting each time! In fact, many people will often start a project with some vague pseudocode like
    for each data file { check for bad data and skip file if found repair damaged data calculate statistics add to database add to report }
    and something like that could easily be made into the program's main loop almost word-for-word, making the code self-documenting. </code>

Finally, here's another good Monastery thread on this topic: When do you function? - I'm certain there are more.

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


In reply to Re: To sub or not to sub, that is the question? by Albannach
in thread To sub or not to sub, that is the question? by tachyon

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, 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?
    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 2020-01-23 13:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?