Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I use two general solutions, depending on how complex the problem is.

The first option is to use a table of function pointers:

%FLUT = ( 'condition_1' => \&func_1, 'condition_2' => \&func_2, 'condition_3' => \&func_3, ); $condition = ## reduce the input to a known condition $FLUT{$condition}->(@args);

and since I happen to like object syntax, I have a simple little class that wraps function pointers up as objects:

package Command; sub new { my $type = shift; my $O = shift; bless $O, $type; return ($O); } sub execute { my $O = shift; return ($O->( @_ )); }

Naturally, it helps to make sure all the functions/objects in a table take the same list of arguments. That restriction appeals to me, because it helps me write code that ends in a known state no matter what. Nested if-else statements make it a little too easy to set one set of globals in one branch, and another set of globals in another, IMO. That makes it really difficult to decide what state the program is in when it leaves the outermost statement.

If a project involves serious branching, I'll occasionally roll my own linear state machine:

package Machine; ## new (nil) : Machine_ref # # trivial constructor # sub new { my $self = bless {}, shift; return ($self); } ## execute # # run through the list of commands, halting when one of them fails # sub execute { my $self = shift; my $model = shift; for $cmd (@{ $self->_get_commands() }) { # [1] last if ( 0 == $cmd->execute ($model) ); # [2] } return(); } ### # [1] iterate over the list of Command objects that belongs to the # specific Machine, executing each one in turn. # [2] drop out of the loop if any Command in the sequence returns # a zero. # # in practice, this is equivalent to executing a set of # short-circuit conditionals: # # if (<condition 1>) { return ('value 1') } # if (<condition 2>) { return ('value 2') } # if (<condition 3>) { return ('value 3') } # # but it's cleaner, and you can make each Command much more complex # than the body of a sensible conditional. ### ## STATE-SPECIFIC OPERATIONS ## _get_commands (nil) : listref # # each Machine has its own list of Commands that should be # executed in response to the user's input. this routine returns # a reference to that list. # sub _get_commands { return ([]); } 1;

This code comes from a larger project, where the parameter $model was itself an object. The Model was responsible for all the data in the program, and the Commands simply called sets of Model methods.


In reply to Re: Why I Hate Nested If-Else blocks by mstone
in thread Why I Hate Nested If-Else blocks by jeffa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-18 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found