in reply to Nested testing of many conditions--a better way?

if ( $today > $start && $today < $stop ) { if ( $today!= $notthisday[0] ) { if ( $today != $notthisday[1] ) { if ( $today != $notthisday[2] ) { if ( $day == $dayofmon[0] || $day == $dayofmon[1] ) { print "$today is the day."; } } } } }

I'd have to kill you if you implemented this in my code base :)

The and-composite is much better and will not draw criticism. By any chance could you put a negated grep in there for the notthisday part to make it a little more Perlish? Also, add some parens rather than relying on your knowledge of the operators as a cautionary technique.

Halley's quick fail routine is one I like a lot, but I think kesterkester's technique may be better for more complex tests, to a C programmer, that's just extra code to read through (IMHO) -- yet allows you to reuse things at the price of method call overhead. But hey, if you are reusing things, you are better off using a really cool well-tested Date module!.

Replies are listed 'Best First'.
Re: Re: Nested testing of many conditions--a better way?
by bradcathey (Prior) on Mar 23, 2004 at 16:36 UTC
    Thanks all for great suggestions. I do like kesterkester's subroutine approach because I have lots of tests, some with more conditions than others, so being able to pick and choose is nice (at the expense of all the "wiring" of subroutines). I also like the quick-fail method (I tried this with unless but couldn't get my head around it) of halley, but as applied by dragonchild. And I'm glad flyingmoose won't have to kill me, and let me see what I can do with grep or even map.

    —Brad
    "A little yeast leavens the whole dough."