-2

I always wanted to ask this question. I would like to know what form I should use, and why. What is better:

if my_condition:
    # very long block of code with multiple indention levels

OR:

if not my_condition:
    exit

# Still here ?
# very long block of code with multiple indention levels

EDIT:

I'm not asking for personal opinion. Is there any PEP recommendation/ any criterion I'm not aware of ?

JPFrancoia
  • 4,866
  • 10
  • 43
  • 73

1 Answers1

2

From the Zen of Python:

Flat is better than nested.

The second approach, if not my_condition: exit, avoids a level of nesting and is therefore superior according to this particular criterion.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97