Single line if statement with a continue
Grant Edwards
grant.b.edwards at gmail.com
Thu Dec 15 16:34:54 EST 2022
On 2022-12-15, MRAB <python at mrabarnett.plus.com> wrote:
> 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
I sometimes have to work on code like that with bocks nested 8-10
levels deep spread out over hundreds of lines. The first thing I do is
convert it to something like the code below. Check for error
conditions up front and exit accordingly.
When working in C, a "goto error" or "goto done" instead of "return"
can provide a "single return" if that's deemed important.
>
> With multiple returns, however:
>
> if not test_1:
> return
>
> ...
>
> if not test_2:
> return
>
> ...
>
> if not test_3:
> return
>
> ...
>
> return
More information about the Python-list
mailing list