Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: [SOLVED] Return statement in subroutine(s) is necessary when not returning anything?

by Preceptor (Deacon)
on Nov 03, 2015 at 14:37 UTC ( [id://1146813]=note: print w/replies, xml ) Need Help??


in reply to Re: [SOLVED] Return statement in subroutine(s) is necessary when not returning anything?
in thread [SOLVED] Return statement in subroutine(s) is necessary when not returning anything?

Well, whilst I hesitate to say it, there are a few places where perl lets people get away with shoddy coding practices...

Replies are listed 'Best First'.
Re^3: [SOLVED] Return statement in subroutine(s) is necessary when not returning anything?
by Your Mother (Archbishop) on Nov 03, 2015 at 15:09 UTC

    I disagree. A large part of my affection for Perl is its terseness. It doesn't force you to do things that are obvious enough for the compiler so obvious enough for a good dev. Verbosity can be an impediment to communication; especially in technical matters. Back to the OP's example–

    sub ohai { print "OHAI"; return; }

    That is technically "wrong." It prevents you from being able to use the sub like this do_something() if ohai(); It makes the sub less useful (Update: and harder to test). To fix that, you would report on success. That RFC:SHOULD look like:

    sub ohai { return print "ohai"; }

    How good a practice does that appear to be? Looks foolish to me. I much prefer the implicit return (when it's sensible, it's not always) than the explicit being recommended.

Re^3: [SOLVED] Return statement in subroutine(s) is necessary when not returning anything?
by ikegami (Patriarch) on Nov 03, 2015 at 16:27 UTC
    I didn't say a few people don't use the practice. In fact, I don't think I've come across any CPAN code that uses the practice. Claims of shoddiness seem to be *greatly* exaggerated.

      Indeed.

      But my point was just because a lot of people do something, really doesn't have any bearing on it being a good - or bad - practice. Argumentum ad populum

      Especially in perl, which lets you get away with all sorts of horrific nastiness for extended periods of time.

        I didn't say "lot of module use the practice"; I said "lots of that modules that work perfectly fine use the practice". When the argument against the practice is that cause things to malfunction, this isn't argumentum ad populum.

        The opposite is also true. Just because something is rarely used doesn't make it a good practice. Nearly no one really means just that. You can argue that nearly every single author on CPAN writes shoddy code 100% of the time but I don't think the argument has much traction. :P

        Returning to the OP and the theme at hand. Printing from subroutines is a poor practice. They should return data for the controller (program flow) to use. Printing from a sub breaks its extensibility, limits its usefulness, and encourages code duplication (by avoiding view/template practices and making things impossible to reuse as is).

Log In?
Username:
Password:

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

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

    No recent polls found