I once saw a C function full of GOTOs which jumped to the return state at the bottom of the functions because the programmer had learned that “multiple returns are a bad idea.”
I totally agree multiple returns causing confusion is a symptom of poor design, not a cause. Required cleanup is easily handled by the try / finally construct. From: Python-list <python-list-bounces+gweatherby=uchc....@python.org> on behalf of avi.e.gr...@gmail.com <avi.e.gr...@gmail.com> Date: Thursday, December 15, 2022 at 2:07 PM To: python-list@python.org <python-list@python.org> Subject: RE: Single line if statement with a continue *** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. *** Multiple returns is not always a problem as it depends on the nature of a task whether it has complex enough cases. I have seen code that instead sets Boolean variables when it is ready to return and everything else keeps checking the variables to skip further processing so the program then slides down to a single return statement. If done properly, it boils down to the same result if VERY carefully done but with lots of sometimes complex IF statements that may be not be updated well if the logic changes a bit. In such cases, I vastly prefer clean and unambiguous returns from the function right at the point where the decision is made, UNLESS the exit is not always clean as there may be cleanup or finalization of some kind required that is best done at a single point. If efficiency is an issue, then clearly a rapid exit may beat one where processing continues for a while and also often beats a method that involves creating and calling multiple smaller functions with lots of overhead. Having said all that, of course, if you can find a fairly simple algorithm that only returns from one place, use it instead of a convoluted one. The issue is not necessarily that multiple return points are bad, but that they are often a symptom of sloppy planning. But for some problems, they fit well and simplify things. -----Original Message----- From: Python-list <python-list-bounces+avi.e.gross=gmail....@python.org> On Behalf Of Stefan Ram Sent: Thursday, December 15, 2022 7:42 AM To: python-list@python.org Subject: Re: Single line if statement with a continue Chris Green <c...@isbd.net> writes: >I always try to avoid multiple returns from functions/methods, as soon >as things get complex it's all to easy to miss clean-up etc. This "complexity" could also mean that the function has become too large. In such a case, one could say that the /size/ of the function is the actual cause of problems and not multiple returns. |Fools ignore complexity. Pragmatists suffer it. Some can avoid it. |Geniuses remove it. Alan Perlis (1922/1990) Within a small function, multiple returns are rarely a problem. When a function is large, one can apply well-known refactors. For an example, look at the code in the first post of the thread Python script not letting go of files Date: Tue, 29 Nov 2022 12:52:15 +0000 and then at my reply of 29 Nov 2022 14:44:39 GMT. >"No multiple returns" is often found in programming guidelines. I religiously followed that when I did more C programming than today. Then, I read an article about how the result pattern makes functions measurably slower. (It should not with an optimizing compiler, but it did due to those measurements. Can't find that article now, though.) "Result pattern" I call writing, if a: result = 123 else: result = 456 return result , instead of, if a: return 123 else return 456 . -- https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!lVeLOl91qPUjowC1ch_u353upn8X-V4rsReaNberWpIXBlBP6CYcDgr_aaMb0ZHoYX4YWO8id1biCn6sW7V6vJM$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!lVeLOl91qPUjowC1ch_u353upn8X-V4rsReaNberWpIXBlBP6CYcDgr_aaMb0ZHoYX4YWO8id1biCn6sW7V6vJM$> -- https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!lVeLOl91qPUjowC1ch_u353upn8X-V4rsReaNberWpIXBlBP6CYcDgr_aaMb0ZHoYX4YWO8id1biCn6sW7V6vJM$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!lVeLOl91qPUjowC1ch_u353upn8X-V4rsReaNberWpIXBlBP6CYcDgr_aaMb0ZHoYX4YWO8id1biCn6sW7V6vJM$> -- https://mail.python.org/mailman/listinfo/python-list