Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Refactoring a large script

by mdunnbass (Monk)
on Jan 18, 2007 at 16:47 UTC ( [id://595284]=perlquestion: print w/replies, xml ) Need Help??

mdunnbass has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

Hypothetically speaking, of course, if a lowly perl user had written a large and unwieldy program that was more than likely far clunkier than it needed to be, where could I, err, I mean he, go to ask for help in refactoring/improving/tightening/increasing the speed and efficiency of the program?

Is that just a matter of asking for help from willing monks, or is there instead a forum/online work-group willing to help out with that?

I've got a program I've been writing that's about 35 subroutines, maybe 1k lines of code, with another 1k of comments, and at this point, I'm finished debugging all but about 5 of the subs. There are areas of the program where I am sure I did things the hard way, or at least, not the quick or efficient way.

I'm self-taught in perl, with only what I've learned here and in the perldocs, and I don't do any programming outside of this one script, so I've never ventured into the realms of mapping, transposing, TIEs, or anything more than intro-level stuff.

Any advice or guidance would be greatly appreciated.

Thanks,
Matt

Replies are listed 'Best First'.
Re: Refactoring a large script
by xdg (Monsignor) on Jan 18, 2007 at 17:05 UTC
    refactoring/improving/tightening/increasing the speed and efficiency of the program

    I think you first need to ask yourself which goal you are pursuing. "Refactoring" is usually associated with removing duplicate code and generally improving the maintainability of your code. Efficiency is a different thing entirely (and sometimes opposite, as refactoring things into subroutines increases overhead).

    For efficiency, it's critical to figure out real bottlenecks and here profiling tools will help. See Devel::DProf for examples. Once you've identified particular bottlenecks, work on those or post examples to Perl Monks if you're stuck. Don't overlook all the good Tutorials.

    There are also some great reference books on these topics. The book that's almost written for your case is "Perl Medic: Transforming Legacy Code" by Peter J. Scott.

    For refactoring, for the most part, just look for things that you do over and over again (cut-and-paste stuff) and try to isolate that into separate subroutines. If you're up to it, you could consider pushing some of it into separate modules. (See How a script becomes a module).

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Thanks xdg.

      I guess what I'm looking for is more efficiency than refactoring. In that sense, I've already pulled much of the redundant code into subs. So, it's more the overhead and speed which I want to tackle. And thanks for the book ref. I'll give it a look-see.

      Thanks,
      Matt

Re: Refactoring a large script
by Limbic~Region (Chancellor) on Jan 18, 2007 at 17:52 UTC
    Matt,
    To end up with a product that you are happy with and that is easy to maintain in the future, you are going to have to do a lot of work. Since it seems you are eager to learn, you won't mind. Please keep in mind I am writing this on my lunch break so I am likely going to miss a few things. I am sure others will expand on what I have been too frugal with and fill in the gaps for what I missed outright.

    Cheers - L~R

Re: Refactoring a large script
by gaal (Parson) on Jan 18, 2007 at 18:04 UTC
    Quality is an elusive thing but there are probably several aspects you want to improve. Some of them might be:
    • Maintainability
    • Succinctness
    • Algorithmic efficiency
    • Correctness
    • Confidence of Future Correctness in Face of Changing Requirements
    • Hackability
    • Pleasantness to Look at After a Month of Doing Something Else
    • Locality of effects
    • Showoffability to your peers
    • Showoffability to your betters
    These are not well-distinguished from each other and are often overlapping or related. As you learn more and more Perl and programming, always ask yourself "how can this technique or principle help me achieve these goals in my code"? An incremental attitude is very good to have: you can't make your stuff perfect in one go, and gradual improvement is not a bad thing!

    There are many things you can pursue in this line. For example, writing tests are a wonderful and permanent benefit even if at first they only cover a small part of your functionality. Adding use strict and use warnings if they aren't there already (and heeding their advice!) will help a lot too. (If you find it difficult to do and have already started splitting your code to modules, tackling one file at a time may be useful.) Depending on your program's goals and your existing code various "more than intro-level" stuffs will be suitable next—I'd suggest showing us some code and saying "this does X but can how would you suggest doing it better?". Many of us here are self taught and virtually all have learned from others on this site: welcome! you are in good company.

Re: Refactoring a large script
by OfficeLinebacker (Chaplain) on Jan 19, 2007 at 05:15 UTC
    Hi.

    Depending on how much work you want to do, I'd think about buying Perl Best Practices and just going through each of the tips/principles and applying them one-by-one. There are over 100. I've been having fun applying the principles to code I'm writing as I read each chapter. I wish I had time to fix it all, but I maintain so many scripts that I and/or myself have written at various stages of expertise that I do a little here and a little there when I can. If you're just concentrating on one big program, I think it would be worth it. A lot of the principles hinge on efficiency just as much as maintainability.

    Good luck!

    I like computer programming because it's like Legos for the mind.
Re: Refactoring a large script
by derby (Abbot) on Jan 18, 2007 at 17:48 UTC

    Come on Matt, your scripts have all ready been refactored.

    -derby
    Sorry ... couldn't help myself.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://595284]
Approved by liverpole
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-19 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found