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

Re^5: eval to replace die?

by BrowserUk (Patriarch)
on Oct 04, 2010 at 15:48 UTC ( [id://863361]=note: print w/replies, xml ) Need Help??


in reply to Re^4: eval to replace die?
in thread eval to replace die?

the risk and fragility of string comparisons for exception handling.

Even in Java, it's still "string compare". It's done at compile-time rather than run-time, which is good. But it is still string compare.

But Exception::Class does not give you that advantage.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^6: eval to replace die?
by chromatic (Archbishop) on Oct 04, 2010 at 16:45 UTC

    I should be more precise, you're right.

    Imagine instead I wrote "the risk and fragility of using fuzzy regular expression matches for exception handling, especially when type checks and polymorphism are much more reliable".

      Imagine instead I wrote "the risk and fragility of using fuzzy regular expression matches for exception handling, especially when type checks and polymorphism are much more reliable".

      And I say you are talking bollocks.

      • Because "type checks" are just string compares.

        Pretty much exactly:

        if( ref( $obj ) eq $type ) { ... ## or if( $obj =~ m[=$type$] ) { ...
        .
      • And polymorphism--of the type you are alluding to--is just multiple string compares.

        Pretty much:

        if( ref( $exception ) eq $type ) or grep{ ref( $exception ) eq $_ } @{ $type::ISA } ) { ... ## or if( $exception =~ m[=$type$] or grep{ $exception =~ m[=$_] } @{ $type::ISA } ) { ...

      which demonstrates the lie of "more reliable, never mind "much more reliable.

      You are selling a philosophy--your own home-spun, personal prejudice--on the basis of a technical merit which it simply does not live up to.

      Exception::Class exceptions are:

      1. defined in terms of strings:
        use Exception::Class ( 'MyException', 'AnotherException' => { isa => 'MyException' },
      2. thrown in terms of strings: MyExceptions->throw( error => 'Divisor undefined' ) unless defined $d;
      3. and caught in terms of strings: if ( $e = Exception::Class->caught('MyException') ) {

      Which means you're even more reliant upon string compares, but now you have more places to change in order to maintain them.

      I really wonder if you think through your justifictions at all. It sure doesn't look like it.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Come on, this is just silly. Yes isa() will use string compares, but to say it's just a string compare is like saying a marathon is "just moving your feet". Just one example is that isa() knows how to look up the inheritance tree and tell me if one of my ancestors also meets the criteria.

        Here's an example from a recent project of mine where structured exception objects helps out a lot. I have some code that runs remote commands on different servers. This code could be called from lots of different places and depending on how it's called the error handling is different. I could just die "Couldn't run $cmd on server $server", but then I have to parse that string with regular expressions to get the $cmd and $server values, every place it could be used (the same regular expression in lots of different places, not very DRY). And if I ever decide to change that error message I have to then track down all the places that regular expression lives (which is much, much harder than searching for a simple string in piles of code) and change them. It gets even worse if this module is used in places that I don't control (I'm flattered when someone uses my CPAN modules and don't want to make their job any harder than it has to be).

        But instead I can use an Exception::Class object and do something like Project::FailedRemoteCommand->throw(error => "Couldn't run $cmd on server $server", command => $cmd, server => $server). Now the code that uses this doesn't have to care about my actual error message, just the type of error. Not only is searching for "FailedRemoteCommand" in a code base easier than searching for regex, it's also more future proof. Modules that use my lib don't have to care about the exact text of my error message either. They can get the $e->server or $e->command easily. And if they don't care about the structured exception, it still works just like a string to them.

        Yes they might have problems if I ever decide to restructure my exception hierarchy (which is much rarer than changing an error message) but even that can be handled by having some special isa() magic to make the new classes still look like the old to old code. Try doing that when you're just using strings.


        -- More people are killed every year by pigs than by sharks, which shows you how good we are at evaluating risk. -- Bruce Schneier

        Which version of Exception::Class did you read? The caught() method in E::C 1.32 includes the type check:

            return unless blessed($e) && $e->isa( $_[1] );

        I trust the Perl 5 core to get isa() right for a type check (and I trust that everyone following this conversation can trivially distinguish between an exact string match and a regular expression match in terms of fragility).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-20 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found