Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re (tilly) 2: mapcar -- map for more than one list

by tilly (Archbishop)
on Jan 22, 2002 at 01:28 UTC ( #140499=note: print w/ replies, xml ) Need Help??

in reply to (tye)Re3: mapcar -- map for more than one list
in thread mapcar -- map for more than one list

A note about if not versus unless.

I am diametrically opposed to tye here. The only win for unless is to make some things be said in a way that more directly matches how we speak. But I would *never* use it for complex expressions. As I have found from painful experience, people do not apply De Morgan's laws on the fly. In other words while debugging it takes a lot of thought to translate:

unless (A or B) { .... }
and recognize that as
if (!A and !B) { ... }
After you have been there a couple of times, you learn not to use unless with complex expressions. :-)

Comment on Re (tilly) 2: mapcar -- map for more than one list
Select or Download Code
(tye)Re4: mapcar -- map for more than one list
by tye (Cardinal) on Jan 22, 2002 at 02:37 UTC

    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")
      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
Node Status?
node history
Node Type: note [id://140499]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-09-20 20:33 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (163 votes), past polls