Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by tye (Sage)
on Jan 22, 2002 at 02:37 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2024-04-24 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found