Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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); }

Replies are listed 'Best First'.
Re^3: return if defined
by AnomalousMonk (Canon) 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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2017-06-25 00:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (563 votes). Check out past polls.