Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Control Flow - Multiple returns or nested conditionals

by lostjimmy (Chaplain)
on Oct 07, 2010 at 12:03 UTC ( #864026=note: print w/replies, xml ) Need Help??


in reply to Control Flow - Multiple returns or nested conditionals

My company's coding standard requires a single return at the end of each function/method. I think the idea behind that is that you always know where the exit from the function occurs. What you end up with is a significant increase in conditional statements and lots of nesting, as your examples show. I'm not sure whether or not I agree or disagree with that approach, but it's what I've gotten used to.
  • Comment on Re: Control Flow - Multiple returns or nested conditionals

Replies are listed 'Best First'.
Re^2: Control Flow - Multiple returns or nested conditionals
by JavaFan (Canon) on Oct 07, 2010 at 12:21 UTC
    I think the idea behind that is that you always know where the exit from the function occurs.
    The idea comes from "structured programming". Dijkstra's famous "Go To Considered Harmful" isn't about avoiding the goto keyword. It's about having structured units in your program (what we now call blocks), each with a single entry and exit point. Goto defeats that, but next, last, redo, and multiple return statements do so as well.

    Structured programming is considered useful is when you are make statements (proofs) about the program itself. Proofs typically make use of pre- and post conditions. Not having single entry/exit points make those proofs much harder, if not impossible.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2021-07-27 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?