Single line if statement with a continue
MRAB
python at mrabarnett.plus.com
Thu Dec 15 14:34:28 EST 2022
On 2022-12-15 19:05, avi.e.gross at gmail.com wrote:
> 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.
>
A problem with having a single return is that it can lead to excessive
indentation:
if test_1:
...
if test_2:
...
if test_3:
...
return
With multiple returns, however:
if not test_1:
return
...
if not test_2:
return
...
if not test_3:
return
...
return
> 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.com at python.org> On
> Behalf Of Stefan Ram
> Sent: Thursday, December 15, 2022 7:42 AM
> To: python-list at python.org
> Subject: Re: Single line if statement with a continue
>
> Chris Green <cl at 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://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list