Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

(tye)Re4: mapcar -- map for more than one list

by tye (Sage)
on Jan 22, 2002 at 02:37 UTC ( #140511=note: print w/replies, xml ) Need Help??

in reply to Re (tilly) 2: mapcar -- map for more than one list
in thread mapcar -- map for more than one list

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")

Replies are listed 'Best First'.
Re (tilly) 5: mapcar -- map for more than one list
by tilly (Archbishop) on Jan 22, 2002 at 04:22 UTC
    Suppose that you are trying to figure out what happened last night in a production run. Then no matter how clearly the intention read when you wrote it, the question that you are trying to answer is, "Did it do this?" Which usually looks like, "OK, I should have had X, Y, and Z, do I enter this block?" And in that situation, unless is an extra "no" from the question you are trying to figure out.

    Even if I want to factor out a no, I will write it explicitly as an if. Seeing the no written explicitly simplifies a complex expression for me just enough to turn potential confusion into understanding.

    I know it sounds silly, I know it sounds stupid. It did to me when I first heard this tip from another programmer. But after I was bitten a couple of times, I learned that it was true. On complex expressions, the implied not in unless is a nasty debugging trap waiting to happen.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://140511]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2022-01-29 12:46 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (74 votes). Check out past polls.