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 16: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.