Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: return if defined

by Anonymous Monk
on Dec 20, 2012 at 13:11 UTC ( #1009742=note: print w/ replies, xml ) Need Help??


in reply to Re: return if defined
in thread return if defined

Clever, but maybe a little too clever.

This isn't the clearest construct in the world to begin with, and it especially has the potential to be confusing if the code within the do block is long. In that case, the last statement of the block is implicitly returned by the block and used as the return value, but it is a long way from the return statement itself.

You could do the exact same thing a lot more clearly just by using a subroutine.

sub my_find_or_create { # ... return $self->find_by_something(@args) // $self->create_new(@args); }


Comment on Re^2: return if defined
Download Code
Re^3: return if defined
by AnomalousMonk (Monsignor) on Dec 20, 2012 at 13:42 UTC
    Clever, but maybe a little too clever. This isn't the clearest construct in the world ...

    But I thought the whole point of the OP was to be too clever by half, and clarity be damned.

    IMHO, the pair of statements
        my $existing_result = $self->find_by_something(@args);
        return $existing_result if (defined $existing_result);
    is perfectly clear and sufficiently unclever to help me avoid future foot trauma.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-07-23 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (140 votes), past polls