Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re (tilly) 1: Best way to fix a broken but functional program?

by tilly (Archbishop)
on Aug 22, 2001 at 23:37 UTC ( #107102=note: print w/ replies, xml ) Need Help??


in reply to Best way to fix a broken but functional program?

I am going to suggest a completely different way of doing this.

You know what the script is used for, don't you? Well define your task into 2 interacting jobs:

  1. To write a library or libraries that make it easy to write acripts that do its task or tasks.
  2. To simplify this script by having it written in terms of that library or libraries.
If you were to take these two jobs to completion, you should have a clean library and a simple script. At present you are nowhere near this goal and have no idea how you are going to get there.

Relax.

What you want to do is look at the script and identify something simple which might belong in a more general library that you can easily drop into the script. Found it? Good. Now start a simple module. You can use the following template:

package My::Module; # Magical export invocation. use Exporter; @ISA = 'Exporter'; @EXPORT_OK = qw(); # Will fill in # Rest of header section. use strict; # Body here. # Will fill in 1; __END__ =head1 NAME My::Module - Will fill in =head1 SYNOPSIS use My::Module; # Will fill in =head1 DESCRIPTION Will fill in =head1 BUGS AND LIMITATIONS None known.
Note that the name of the module should match the package name, in this case My/Module.pm, and where it goes depends on your current development setup.

Now go to the hideous, ugly script and have it use your module. Next take that simple function or bit of functionality which you identified, and put it in your module. Put the name of it in @EXPORT_OK, describe it and then put it in the import list in your script. In other words put a bit in all the things which are, "Will fill in" above. Then replace a bit of the script with your brand new function.

Wash, rinse, and repeat, developing tests, refactoring your library, etc as necessary. At no step are you writing the script from scratch. But you are eating away at it, and eventually there will be little enough left that you can do the rewrite easily. Plus when you get done you have this convenient library.

Note that this approach is not doable with all kinds of scripts. But when it is appropriate, it can be a good path to know about from ugly legacy code to a cleaner design.


Comment on Re (tilly) 1: Best way to fix a broken but functional program?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://107102]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-11-01 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (227 votes), past polls