I said I'd use unless( complex expression ) over if( ! ( complex expression ) ) so your argument doesn't apply since the if( ! ( complex expression ) ) would require the same translation under the same circumstances.
Whether it makes more sense to distribute the "not" inside of the complex expression depends on a lot of things.
# unless object is valid and either isn't busy or can wait:
unless( $a && ref($a) && isa($a,'Foo')
&& ( ! $a->IsBusy() || $a->CanWait() ) ) {
return;
}
is clearer to me than:
# if object is not valid or both is busy and can't wait:
if( ! $a || ! ref($a) || ! isa($a,'Foo')
|| $a->IsBusy() && ! $a->CanWait() ) {
return;
}
and I don't understand why you (seem to) think that unless can't be understood without translating it into if. I'd only use unless in a case where it makes the meaning clearer.
For simple cases, I find that the difference in clarity is minimal and so prefer the consistancy of always using if. As a statement modifier, unless sometimes reads more naturally. As a conditional block, the benefit of unless only becomes worthwhile to me when the expression is complex enough that factoring out the negation can make a relatively big difference in the clarity of the code.
-
tye
(but my friends call me "Tye") |