Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Coding for maintainability

by dragonchild (Archbishop)
on Feb 16, 2006 at 18:07 UTC ( #530722=note: print w/replies, xml ) Need Help??

in reply to Coding for maintainability

You need a modified dispatch table. A dispatch table is where you have (generally) a hash of names where the values are references to subroutines.
my %dispatch = ( foo => sub { print "foo\n"; }, bar => \&bar, ); chomp( my $input = <> ); unless ( exists $dispatch{ $input } ) { die "I don't know what to do with '$input'\n"; } $dispatch{$input}->(); sub bar { print "bar\n"; }
The modification needed is that you aren't matching on a simple string. There's a few ways to improve this. One is to pass $family to every function and let them determine if they want to handle it. Then, the function returns either true (I handled it) or false (I don't deal with this value). Errors would be propagated with die and caught with an eval-block.

Then, whenever you add something, you're working with a much smaller piece of the puzzle because the engine and the parts are separated.

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: Coding for maintainability
by planetscape (Chancellor) on Feb 17, 2006 at 07:15 UTC
      Hello, Currently I have used the code provided by jdporter instead of my switch statement. However, I will now look at the dispatch table option that you have recommended and see if that will make this script easier to maintain. Thanks for your reply

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://530722]
[Tanktalus]: SwaJime: wouldn't status & status < 13 be true if status was 0?
[SwaJime]: I thought 0 was false, and false AND anything would be false ...
[SwaJime]: unless & is higher precedence than <
[SwaJime]: couldn't find bitwise in precedence list for sybase
[SwaJime]: I bet that is why ... and that makes it to be I think terribly written code :-|

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2017-08-22 18:07 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (338 votes). Check out past polls.