Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
"be consistent"
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 the web crawler heard nothing...

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

      April first is:







      Results (489 votes), past polls