Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Alright, it had to come to this in the end, but it was dragonchild's challenging note that stimulated me to raise this question.

I'm about a third of the way through what seems to me to be a huge script, and I've come to a natural point at which I can stop and take stock. Basically how it works is, users get emails when they have to do something. The email contains a link to the script with a query string of CGI parameters. When the users click on their links they get their own personal query page in which they enter the information they're being asked for. The result is that I've got two thousand lines of code which serves up and processes a series of mini-forms, each unique, but all having lots of things in common, both as to form and content.

I knew from the outset that I was going to want to re-factor this, but I thought I'd write a big gobbet of it so that I had something to work with... and that's where I am now. Before I write the remaining two thirds I want to take what I have and split it up into intelligent re-usable subroutines and tidy them away into modules. Then the remaining 2/3 should be a bit quicker to write.

At the moment what I did was I paged down through the script and kept an eye out for repeating patterns (and indeed remembered where I had put some things that looked the same as other things) and I found one thing I was doing over and over and I put it in a module. But that doesn't seem like a very scientific way to go about it.

Perhaps the scientific way to go about it would have been to conceive the whole thing in modular form at the outset - but that's beyond me at my current level of experience. I hope when I've finished this project I'll have enough of a feel for how this stuff works that I could do that for the next one - but at present I decided the only way I cd do it was by writing a bit of it out and then condensing that.

So I'm appealing to wise monks for your views on how to approach this task, in the most general terms.
  • In the inevitable compromises involved in reducing several different activities into a uniform format, what do you trade off for what?
  • Where do you start in deciding what goes in which subroutine?
  • Do you use one module or several?
  • What's the optimum size for a subroutine?
  • If you've got one thing you do over and over again in two slightly different ways, do you want one subroutine with an if or two subroutines?
  • And anything else?
I've put the WHOLE of my existing script here so you can see what I'm up against. Of course, I'd love to have comments on individual bits of the code - but my main interest is in getting an over all approach to boiling it down so that (A) it works efficiently and (B) it's easy to expand / edit.

George Sherston

In reply to Re-Factoring: Philosophy and Approach by George_Sherston

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

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (4)
    As of 2018-07-17 06:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















      Results (355 votes). Check out past polls.

      Notices?