For some time now I've been obsessed with clean decision trees. No solution is appropriate for all needs and, as usual, tradeoffs are in play. Today I hacked out one particular way of simplifying these trees.

Here's the original mess, which usually worked but sometimes did not, and looks messy: fragile, unmaintainable. A block of code is set up for string eval:

if ( not $code ) { $code = $unit; }; if ( $args and ref $args ) { @args = @$args; $code .= q{(@args)}; } else { @args = (); }; if ( $argv and ref $argv ) { @ARGV = @$argv; }

Both $code or $args may be supplied or not; and an attempt is made to exhaust all four possible cases. As you can see, I've painted myself into such a corner that I've introduced a third input variable.

Another approach, which truly annoys me, is to nest conditionals:

if ( $A ) { if ( $B ) { {...} } else { {...} }; } else { if ( $B ) { {...} } else { {...} }; };

This is so ugly and miserable I've rarely employed it. The second evaluation of the nested comparison, whatever it may be, is particularly offensive.

Alternatively, exhaust all four cases explicitly, at the cost of introducing two scratch flags:

# Four-outcome tree my $cf = !!$code; my $af = !!( $args and ref $args ); if ( $cf && $af ) { @ARGV = @$args; } elsif ( $cf && !$af ) { @args = (); } elsif ( !$cf && $af ) { @args = @$args; $code = $unit . q{(@args)}; } elsif ( !$cf && !$af ) { @args = (); $code = $unit . q{()}; } else { die 'Fifth element!' };

Of course tim toady; and I don't doubt others will rush to display their favorite decision trees. This one may work for me sometimes.

Death only closes a Man's Reputation, and determines it as good or bad. —Joseph Addison

In reply to Case Exhaustion Tree by Xiong

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.