1

I've used for a long time single-return style (as structural programming style). I've started reading Fowler's "Refactoring" and found "Removing control flag" and "Replace Nested Conditional with Guard Clauses" where he writes, that single return should be avoided.

As for me, there are a lot of benefits using single return, and only some more difficults for reading. So what are the profit using multiple return?

Benefits:

  1. Single return allows easily put breakpoint to return statement
  2. It's easy to add Assert for result value if there are single return
  3. Single return makes code more readable
user1284151
  • 875
  • 4
  • 12
  • 23
  • You say there are a lot of benefits of using a single return. What are those benefits? Also, Fowler states, on page 245, in the middle paragraph, why one exit point is bad. Do you disagree with that? – Bob Horn Jul 19 '12 at 02:08
  • I have translated book, so it seems to me, you meant idea about equivalence of if-then-else. Imo, if-then-else is used even if variant very close to impossibilty. For ex, "If I have $1kk USD I'll go to Vegas, else I'll go to work" – user1284151 Jul 19 '12 at 17:27
  • There is a [good discussion](http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement) about single return vs multiple return – N30 May 30 '13 at 21:30
  • This is a software engineering question here:https://softwareengineering.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from – Anthony Jun 16 '17 at 11:24

1 Answers1

1

Even with multiple returns there is one function exit point. It is the closing curly bracket. Just place a breakpoint on it and set a conditional to check rAX (if IA32e).

int f()
{
    if (condition)
        return 1;
    return 0;
} // Place here. Would break at epilog just before return. 
BenMorel
  • 34,448
  • 50
  • 182
  • 322
OZone
  • 11
  • 1