Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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

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

in reply to Re: mapcar -- map for more than one list
in thread mapcar -- map for more than one list

  1. I don't see unless as being any clearer. I rarely use unless. I consider its real value to be providing a way to not have to say if( ! ( complex combination of Boolean expressions ) ) and other methods often serve that same purpose. I could write:
    if( ! @_ ) if( 0 == @_ ) if( @_ < 1 )
    to list a few. I read the first one as "if no arguments", which is perfectly clear to me, clearer than "unless some arguments". But it all seems rather trivial in this case and any of them (including using unless) would be fine by me. (: (I guess I'm more likely to use unless as a statement modifier, not much for a conditional block.)
  2. use would cause be loaded at compile-time. I don't want to load unless I actually end up needing it. I wouldn't go to this much work in a script, but I pay attention to minor details when writing modules.
  3. Mostly because the code started out simpler and the error handling was expanded later and the code hasn't been refactored since then. :)

Update: dragonchild tells me that the question was more about require Exporter. use would call Exporter->import and I'm not importing anything from Exporter so I see no point in useing it. Even Exporter docs use require and not use.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re (tilly) 2: mapcar -- map for more than one list
by tilly (Archbishop) on Jan 22, 2002 at 01:28 UTC
    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. :-)

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2022-01-26 09:10 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (69 votes). Check out past polls.