On 2022-12-15, MRAB <pyt...@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

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to