Skip to content

Instantly share code, notes, and snippets.

@rizkyabdilah
Created April 23, 2012 17:44
Show Gist options
  • Save rizkyabdilah/2472636 to your computer and use it in GitHub Desktop.
Save rizkyabdilah/2472636 to your computer and use it in GitHub Desktop.
Using while-break to prevent 'ugly' nested if
# example nested if, in real life it will be uglier
if a:
# do some code with a
if b:
# do some code with a and b
if c:
# do some code with a, b and c
else:
print "you must set b"
else:
print "a is not set, so b no check"
# its better
while True:
if not a:
print "a is not set, so b no check"
break
# do some code with a
if not b:
print "you must set b"
break
# do some code with a and b
if c:
# do some code with a, b and c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment