in reply to Re: if/unless and chaining
in thread if/unless and chaining

Demorgan's law avoidance is why I like unless. In this simple case, you can reverse eq to ne while chaning unless to if, so it's still simple.

But if the condition is complex, putting a ! in front of the whole thing means more parens  if (!( ...real stuff )) { ... and I don't like the extra layer with only one char outside them! Reversing the Real Stuff would mean changing ands to ors and inverting whatever, and is easy to mess up. Plus I'd rather express it the way that's closest to the problem domain.

Replies are listed 'Best First'.
Re: Re: Re: if/unless and chaining
by Enlil (Parson) on Nov 06, 2002 at 05:34 UTC
    One could always just have another variable set to 1 (say $matched_already=1 )outside the first condition and then set to 0 if it passed into the initial unless block. A second unless in the chain would be considered only if:
    unless { second_condition or $matched_already } { do_something_else}

    and so on. . . Granted you won't get the nifty optimizations that come with the if/elsif/else structure where the subsequent elsif/else conditions are checked only if the previous if/elsif conditions all failed, but no extra parens. But for the same reasons you stated ..

    Reversing the Real Stuff would mean changing ands to ors and inverting whatever, and is easy to mess up

    , I think the elsunless would be another layer of complexity that I for one would have to stop the flow of code flowing from the fingers to grab a piece of paper to be able to gather figure what exactly I was excluding in every case, in all but the simplest cases where I would have used the unless/else or if/elsif/else structure anyhow. Then again at the moment I am hard pressed to think of one instance (I have pondered this for a couple of hours already), where an unless/elsunless/else structure would be more natural than its if/elsif/else counterpart. (though, I guess like most logic is a subjective matter)

    -enlil