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

Can't happen bug follow-up -- Can an if/else be bypassed?

by Your Mother (Archbishop)
on Jul 04, 2008 at 18:49 UTC ( [id://695619]=perlquestion: print w/replies, xml ) Need Help??

Your Mother has asked for the wisdom of the Perl Monks concerning the following question:

Okay, what am I missing? In "Can't happen" bugs this example was presented as something which *could* execute the die statement. No one contradicted it.

if ($some_condition) { return $X; } else { return $Y; } die "not reached"; # but see comment below

So is that really possible? If so, what conditions would allow it?

Replies are listed 'Best First'.
Re: Can't happen bug follow-up -- Can an if/else be bypassed?
by Joost (Canon) on Jul 04, 2008 at 19:04 UTC

      Thank you. The idea that it somehow was possible was making me a little shaky with the universe. Existential crisis averted.

Re: Can't happen bug follow-up -- Can an if/else be bypassed?
by Herkum (Parson) on Jul 05, 2008 at 01:54 UTC

    I hate this type of code, there is no reason to an if/else using return statements. To give two good examples of accomplishing the same thing but a little bit clearer,

    return $some_condition ? $x : $y;

    Or

    if ($some_condition) { return $x; } return $y;

    I use this last example a lot when I am testing success or failure of subroutines,

    return 1 if $self->_condition_1_is_true and $self->_condition_2_is_true and $self->_condition_3_is_true; # etc... return;

    The extra else statement sort of implies that something special is going to happen when it is not. Better to get rid of it (as far as return statements are concerned).

      Uhh ... no. The if-else construct likely means (at least) one of the following:
      • I expect the number of statements in either condition to grow. This is particularly true with transient debugging print statements.
      • I expect to add one or more elsif clauses.
      • I just removed the last elsif clause.

      Code is written for humans, not computers. Please remember that.


      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Can't happen bug follow-up -- Can an if/else be bypassed?
by Anonymous Monk on Jul 05, 2008 at 00:55 UTC
    If source filters get involved, anything is possible :)
Re: Can't happen bug follow-up -- Can an if/else be bypassed?
by Neutron Jack (Pilgrim) on Jul 05, 2008 at 20:24 UTC
    The following prints “not reached”:
    $_ = <<'}'; if ($some_condition) { return $X; } else { return $Y; } die "not reached";
      True, but that's not bypassing an if/else. Rather, it's creating a string that happens to contain the words 'if' and 'else'.
        $_ = <<'}';

        Eh? That's the left shift operator, right? How does that code work?

        from perlop:

        'Binary "<<" returns the value of its left argument shifted left by the number of bits specified by the right argument. Arguments should be integers.'
        but how does it let you fiddle with the if..else statement? (I'm not very good at 'non-obvious' code..)


        thanks! why_bird

        ........
        Those are my principles. If you don't like them I have others.
        -- Groucho Marx
        .......

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://695619]
Approved by Joost
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-03-29 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found