Velocity Reviews > Newbie question about formatting long conditionals

# Newbie question about formatting long conditionals

Barney Frank
Guest
Posts: n/a

 07-28-2004
I am writing my first application using Python, and there are a
couple points in code at which I need a fairly complex if-else block.
I've discovered that you can nest compound conditions in parentheses, but
I haven't found a way to break a really complex if condition up into
multiple lines.

For example, take this little fake snippet:

#############
a = 1
b = 2
c = 3
d = 4

if a == 1 and b < 4 and (c == 2 or d == 4):
print "Conditions met!"
#############

... if the "if" condition gets too long and unweildy for a single
line, is there any way to format it along these lines?:

#############
a = 1
b = 2
c = 3
d = 4

if a == 1
and b < 4
and (c == 2 or d == 4):
print "Conditions met!"
#############

James Henderson
Guest
Posts: n/a

 07-28-2004
Barney Frank wrote:

> I am writing my first application using Python, and there are a
> couple points in code at which I need a fairly complex if-else block.
> I've discovered that you can nest compound conditions in parentheses, but
> I haven't found a way to break a really complex if condition up into
> multiple lines.
>
> For example, take this little fake snippet:
>
> #############
> a = 1
> b = 2
> c = 3
> d = 4
>
> if a == 1 and b < 4 and (c == 2 or d == 4):
> print "Conditions met!"
> #############
>
>
>
> ... if the "if" condition gets too long and unweildy for a single
> line, is there any way to format it along these lines?:

Put the whole condition in parentheses:

if (a == 1 and
b < 4 and
etc.):

HTH,
James

Grant Edwards
Guest
Posts: n/a

 07-28-2004
> ... if the "if" condition gets too long and unweildy for a single
> line, is there any way to format it along these lines?:
>
> #############
> a = 1
> b = 2
> c = 3
> d = 4
>
> if a == 1
> and b < 4
> and (c == 2 or d == 4):
> print "Conditions met!"
> #############

if a == 1 \
and b < 4 \
and (c == 2 or d == 4):
print "Conditions met!"

if (a == 1
and b < 4
and (c == 2 or d == 4)):
print "Conditions met!"

--
Grant Edwards grante Yow! One FISHWICH coming
at up!!
visi.com

anton muhin
Guest
Posts: n/a

 07-29-2004
Barney Frank wrote:
> I am writing my first application using Python, and there are a
> couple points in code at which I need a fairly complex if-else block.
> I've discovered that you can nest compound conditions in parentheses, but
> I haven't found a way to break a really complex if condition up into
> multiple lines.
>
> For example, take this little fake snippet:
>
> #############
> a = 1
> b = 2
> c = 3
> d = 4
>
> if a == 1 and b < 4 and (c == 2 or d == 4):
> print "Conditions met!"
> #############
>
>
>
> ... if the "if" condition gets too long and unweildy for a single
> line, is there any way to format it along these lines?:
>
> #############
> a = 1
> b = 2
> c = 3
> d = 4
>
> if a == 1
> and b < 4
> and (c == 2 or d == 4):
> print "Conditions met!"
> #############
>
>

It doesn't answer your question, but still...

I personally prefer to introduce additional variables (in all the
languages I write) that _explain_ what precisly the condition mean:

isTheWorldGoingToBlow = (a == 1) and (b < 4) ....

if isTheWorldGoingToBlow:

The reason is simple: usually such a long conditions are too complicated
for a human to understand without additional hint.

Just my 5 kopecks ,
anton.

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post veryhotsausage C++ 1 07-04-2008 05:41 PM Bart C C Programming 27 01-15-2008 05:27 AM Mathieu Dutour C Programming 4 07-24-2007 11:15 AM Daniel Rudy C Programming 5 09-20-2005 02:37 AM George Marsaglia C Programming 1 07-08-2003 05:16 PM

Advertisments