Too many return-statements = bad style?
Pierre-Frédéric Caillaud
peufeu at free.fr
Fri Jul 9 04:17:23 EDT 2004
I do it just like your style.
I tend to put tests at the beginning of the function and evacuate all
error cases with returns or throwing exceptions.
This is readable :
if not something_to_do:
return
else:
big blob of code
Or even :
if not something_to_do:
return
big blob of code
This can get you lost if the big blob of code is large so the if is
offscreen :
if something_to_do:
big blob of code
else:
return
Basically I often arrange my if's so the 1-2 lines block is just below
the if and the 20 lines block is after the else...
>
> # Example 2: "Bad" style
> def func(value):
> return_value = xyz
> IF wrong_value:
> return wrong_value
>
> do something does change return_value
> IF not do soemthing worked out:
> return another_wrong_value
>
> do something other with return_value
>
> return return_value
>
> This pylint made me feel guilty again... so I wonder: How do you handle
> this? Is "my" style really bad style?
>
> Thanks for your hints in advance,
> Greetings,
> Marco
>
More information about the Python-list
mailing list