Click here to Skip to main content
15,905,144 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
Generalbrain cells fading... article discussing the use of early returns from functions... Pin
charlieg23-Jan-17 11:55
charlieg23-Jan-17 11:55 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
Kent Sharkey23-Jan-17 12:02
staffKent Sharkey23-Jan-17 12:02 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
charlieg23-Jan-17 12:05
charlieg23-Jan-17 12:05 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
Super Lloyd23-Jan-17 13:54
Super Lloyd23-Jan-17 13:54 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
BillWoodruff23-Jan-17 14:33
professionalBillWoodruff23-Jan-17 14:33 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
Pete O'Hanlon23-Jan-17 22:21
mvePete O'Hanlon23-Jan-17 22:21 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
BillWoodruff24-Jan-17 18:18
professionalBillWoodruff24-Jan-17 18:18 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
den2k8823-Jan-17 21:51
professionalden2k8823-Jan-17 21:51 
I can't read the articles (company rule blocking Internet access) but I give my experience on the subject.

At first I believed - and wrote my code in such a way - that returning early from a function wasn't a bad choice because it allowed to avoid conditional execution of subsequent actions should one prerequisite not be met and because all in all it's faster: if something's already over then why execute other operations?

Then reality hit me hard:
1) Intermediate return points are easy to miss and lead to a waste of time during debug, as I'd have to set breakpoints on every return statement and as I said they are easy to miss;
2) Adding further logic that check complex conditions in order to decide the course of action requires more work as you'd have to substitute the clean return with the very same state variables that you'd have to use in case of a single point of exit;
3) Logging at the end of a function with the whole state of the operations is done extremely cleanly if there is one or only a few return points. The more exit points the more recaps you have to write... and that's redundance, with all the known problems it carries;
4) clean-ups!!! With the bare minimum of return points it's very easy to place the various free(), delete, WSACleanup, whatever you need: just before the return! If there are previous triggerable returns chance are that in those cases, which may be rare, the cleanups are not called. Hilarity ensues.

So while I'm not a taliban of single point of exit (sometimes they are the best choice) as of now I prefer going in that direction as my experience proved me that on the long run it's the most stable and observable pattern.

Take my two cents for what they are worth.
CALL APOGEE, SAY AARDWOLF
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X

If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver

"Go ahead, make my day"

GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
OriginalGriff23-Jan-17 22:03
mveOriginalGriff23-Jan-17 22:03 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
den2k8823-Jan-17 22:20
professionalden2k8823-Jan-17 22:20 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
User 1013254623-Jan-17 23:55
User 1013254623-Jan-17 23:55 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
den2k8823-Jan-17 23:57
professionalden2k8823-Jan-17 23:57 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
User 1013254624-Jan-17 0:09
User 1013254624-Jan-17 0:09 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
den2k8824-Jan-17 0:15
professionalden2k8824-Jan-17 0:15 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
User 1013254624-Jan-17 0:19
User 1013254624-Jan-17 0:19 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
den2k8824-Jan-17 0:25
professionalden2k8824-Jan-17 0:25 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
User 1013254624-Jan-17 0:42
User 1013254624-Jan-17 0:42 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
den2k8824-Jan-17 0:49
professionalden2k8824-Jan-17 0:49 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
User 1013254624-Jan-17 0:54
User 1013254624-Jan-17 0:54 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
BillWoodruff25-Jan-17 17:49
professionalBillWoodruff25-Jan-17 17:49 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
Mark_Wallace23-Jan-17 22:48
Mark_Wallace23-Jan-17 22:48 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
den2k8823-Jan-17 23:10
professionalden2k8823-Jan-17 23:10 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
#realJSOP23-Jan-17 23:22
professional#realJSOP23-Jan-17 23:22 
GeneralRe: brain cells fading... article discussing the use of early returns from functions... Pin
den2k8823-Jan-17 23:27
professionalden2k8823-Jan-17 23:27 
GeneralRollercoaster... Pin
R. Giskard Reventlov23-Jan-17 10:52
R. Giskard Reventlov23-Jan-17 10:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.