RE: single return from function
"Mark Miller" <[email protected]> Thu, 15 Nov 2012 20:07:53 -0800
| Newsgroups | gmane.comp.programming.refactoring |
|---|---|
| Message-ID | <[email protected]> |
I prefer guard clauses at the top of method blocks (see also programming by contract) as they keep the remaining code unindented and easier to read. Methods should be small, and multiple returns at the top are totally fine. Large methods with returns scattered throughout are harder to read, harder to reuse, and often serve multiple purposes (e.g., they should be broken into two or more smaller methods). - Mark Miller -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Keith Ray Sent: Thursday, November 15, 2012 7:40 PM To: [email protected] Subject: Re: [refactoring] single return from function I don't think the Refactoring book makes that recommendation. That advice dates back to "structured programming" (google it). In a long function, multiple returns are hard to see, and may require duplicated code. In less smelly code, after refactoring, a function is typically less than 10 lines long, and could have a few early returns (see "guard clause") in addition to the final return statement. On 2012 Nov 15, at 6:09 PM, alex_us01 wrote: > On the internet, I found somewhere* that the Refactoring book advocates for a single return from a function (thus, promoting assigning values to a variable, such as result, and then say return result; at the end). > > Is that true? > > I couldn't find it anywhere in the online catalog. > > Thanks! > > * see item 8 (might change) > http://www.javalobby.org/forums/thread.jspa?forumID=61&threadID=16937 > > [Non-text portions of this message have been removed] ------------------------------------ Yahoo! Groups Links ------------------------------------ Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/refactoring/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/refactoring/join (Yahoo! ID required) <*> To change settings via email: [email protected] [email protected] <*> To unsubscribe from this group, send an email to: [email protected] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/