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")
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.