Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Style question on conditional return statements:

by Anonymous Monk
on Oct 06, 2006 at 17:49 UTC ( [id://576728]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a style question about return statements for small subs. Which of these three do you prefer, and why:

# 1: sub is_connected { return 1 if shift->is_writable } # 2: sub is_connected { return shift->is_writable } # or 3: sub is_connected { return shift->is_writable ? 1 : 0 # or undef, etc. }

Replies are listed 'Best First'.
Re: Style question on conditional return statements:
by VSarkiss (Monsignor) on Oct 06, 2006 at 18:58 UTC
Re: Style question on conditional return statements:
by shmem (Chancellor) on Oct 06, 2006 at 17:57 UTC
    I wouldn' write that in the first place, I'd call is_writable() directly ;-)

    But your cases are simplifications, I guess. Then I'd use

    • the first form if it's not at the end of the sub, i.e. for early returns
    • the second form if I know what is_writable() returns (i.e. it's my own code, and I know how it behaves in scalar or array context)
    • the third one to force scalar return, no matter what is_writable() actually gives.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Style question on conditional return statements:
by jdporter (Paladin) on Oct 06, 2006 at 17:58 UTC

    I'd leave out the return entirely. Not necessary.

    Of the three, #1 is bad style. Which of the other two you use depends on whether is_writable returns anything else you might want to pass through, e.g. "level of writability" or something, vs. wanting to ensure that the return value is a pure boolean regardless of what is_writable actually returns.

    We're building the house of the future together.
Re: Style question on conditional return statements:
by wojtyk (Friar) on Oct 06, 2006 at 18:14 UTC
    I totally agree that this is an unnecessary function.
    Another case of going a little too gungho with the whole refactoring/OO mentality.

    Personally I've always found all the get and set functions to be a bit much as well. Frankly, if you don't have a specific need to filter what your private variables get set to, a function call is needless complexity. Just expose the privates (pun semi-intended), access control be damned.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found