in reply to 36 Conditions in If Statement [sendhelp]]

G'day UpTide,

Writing a condition, on one line, that is almost a thousand characters long, and contains over a hundred unexplained, numeric constants, is utterly ridiculous! It's a maintenance nightmare; extremely error-prone; and a disaster just waiting to happen.

For a start, when you have complex conditions, spread them over multiple lines, and line up the conditions such that they are easy to read, e.g.

if ( $matrix[0][4][0] == 1 and $matrix[0][4][1] == 1 and $matrix[0][4][2] == 1 ... ) { # action if TRUE }

Next, although those numeric constants might be meaningful to you now, while you're writing the code and it's fresh in your mind, they won't be when you return to this code later; they most certainly will not be for anyone looking at this code for the first time. There are many ways to give these constants meaningful names: the builtin pragma, constant, is one option; the CPAN module, enum, is another. The SEE ALSO sections of both of those have other suggestions; a "CPAN search for 'constant'" produces hundreds of results.

Using meaningful names should make your code self-documenting; however, you may want to add comments (e.g. to explain why you're testing for exact equality with "1", as opposed to, say, testing for any TRUE value).

Instead of putting that much code in the middle of your script, use a subroutine. I think you'll agree this is much clearer:

print "it worked" if matrix_ok(\@matrix);

In this instance, instead of dozens of equality tests, you can use loops similar to what you've already written. For efficiency, I wouldn't repeatedly redeclare and redefine the fixed indices. Here's an untested example:

sub matrix_ok { my ($matrix) = @_; { my $i = 4; for my $j (0 .. 2, 4 .. 6) { for my $k (0 .. 2) { return 0 unless $matrix->[$i][$j][$k] == 1; } } } { my $j = 4; for my $i (0 .. 2, 5, 6) { for my $k (0 .. 2) { return 0 unless $matrix->[$i][$j][$k] == 1; } } } return 1; }

Note that I didn't use meaningful names, because I have no idea what all those numbers mean. If I did know what they meant, I might write code like this:

... for my $k (MIN_DEPTH .. MAX_DEPTH) { ...

See also (functions such as any() and all() in): the builtin module List::Util; and the CPAN modules, Perl6::Junction and Quantum::Superpositions.

— Ken

Replies are listed 'Best First'.
Re^2: 36 Conditions in If Statement [sendhelp]]
by AnomalousMonk (Archbishop) on Feb 04, 2017 at 18:14 UTC

    To Whom It May Concern: Note that the functions  any() all() et al were added to version number (mumble) of module List::Util; prior to that version, they appeared (and still do appear) in the List::MoreUtils module.


    Give a man a fish:  <%-{-{-{-<