Code formatting question: conditional expression

J

John Posner

While refactoring some code, I ran across an opportunity to use a
conditional expression. Original:

if total > P.BASE:
excessblk = Block(total - P.BASE, srccol, carry_button_suppress=True)
else:
excessblk = None

Is there any consensus on how to format a conditional expression that is
too long for one line? How about this:

excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
if total > P.BASE else
None)

The above format separates the values from the if-then-else machinery.
Too many lines? Would it be better to line up "if" and "else"
vertically? ...

excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
if total > P.BASE
else None)

PEP 308 is silent on this topic.

-John
 
D

Diez B. Roggisch

John said:
While refactoring some code, I ran across an opportunity to use a
conditional expression. Original:

if total > P.BASE:
excessblk = Block(total - P.BASE, srccol,
carry_button_suppress=True)
else:
excessblk = None

Is there any consensus on how to format a conditional expression that is
too long for one line? How about this:

excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
if total > P.BASE else
None)

The above format separates the values from the if-then-else machinery.
Too many lines? Would it be better to line up "if" and "else"
vertically? ...

excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
if total > P.BASE
else None)

My choice would be

excessblk = None
if total > P.BASE:
excessblk = ...


You don't lose any vertical space, and it's much more readable IMHO.

Diez
 
J

Jean-Michel Pichavant

Diez said:
John Posner wrote:



My choice would be

excessblk = None
if total > P.BASE:
excessblk = ...


You don't lose any vertical space, and it's much more readable IMHO.

Diez
+1, I'm using such layout whenever possible.

JM
 
S

Steven D'Aprano

While refactoring some code, I ran across an opportunity to use a
conditional expression. Original:

if total > P.BASE:
excessblk = Block(total - P.BASE, srccol,
carry_button_suppress=True)
else:
excessblk = None

Is there any consensus on how to format a conditional expression that is
too long for one line?

Er, that defeats the purpose of using a conditional expression. If it's
too long for one line, leave it as an if...else statement.
How about this:

excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
if total > P.BASE else
None)

If you insist on using the conditional expression, my preference would be:

excessblk = (
Block(total - P.BASE, srccol, carry_button_suppress=True)
if total > P.BASE else None
)
 
R

Richard Brodie

if total > P.BASE:
excessblk = Block(total - P.BASE, srccol, carry_button_suppress=True)
else:
excessblk = None

I wonder if it is appropriate to replace the None sentinel with one that is an instance
of Block() e.g.

size = total - P.BASE
excessblk = Block(size, srccol, carry_button_suppress=True, empty_block=(size <= 0) )
 
J

Jan Kaliszewski

18-08-2009 Steven D'Aprano said:
On Tue, 18 Aug 2009 10:04:36 -0400, John Posner wrote:
[snip]
How about this:

excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
if total > P.BASE else
None)

If you insist on using the conditional expression, my preference would
be:

excessblk = (
Block(total - P.BASE, srccol, carry_button_suppress=True)
if total > P.BASE else None
)

Generally I'd prefer:

excessblk = (Block(........) if total > P.BASE
else None)

But it this case first line would be too long, then I'd try using:

excessblk = (Block(total - P.BASE, srccol,
carry_button_suppress=True) if total > P.BASE
else None)

or

excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
if total > P.BASE else None)

I'd use conditional expression only (rather) in situation when the first
expression-part was 'common' and the other (after else) was 'rare'.

Cheers,
*j
 
B

Bruno Desthuilliers

Richard Brodie a écrit :
I wonder if it is appropriate to replace the None sentinel with one that is an instance
of Block() e.g.

size = total - P.BASE
excessblk = Block(size, srccol, carry_button_suppress=True, empty_block=(size <= 0) )

In which case the last param is possibly redundant - the Block object
knows its size, so it might be able to know by itself if it's empty.

NB : please notice the 'possibly' and 'might' cautions !-)
 
A

Aahz

I don't see vertical space as such a scarce resource; we don't have an
imminent newline shortage, to my knowledge. I value it far lower than,
say, local readability.

We don't have a newline shortage, but we do have a pixel shortage.
 
N

Nicola Larosa (tekNico)

Nicola said:
Here's my take:

    excessblk = Block(total - P.BASE, srccol,
carry_button_suppress=True
        ) if total > P.BASE else None

Oops, it got shortened out: line longer than 72 chars, acceptable in
code, but not in email. I'll try again.

If the first line is too long, I would write it like this:

excessblk = Block(total - P.BASE, srccol,
carry_button_suppress=True) if total > P.BASE else None

If not, like this:

excessblk = Block(total - P.BASE, srccol, cbs=True
) if total > P.BASE else None

If the condition or the last value were too long to make it all fit on
two lines, then it would probably best to revert to a plain "if"
statement.
 
J

Jorgen Grahn

Fine in email; just munged on your behalf (and probably without your
knowledge) by your email service provider. If you don't want that
happening, it's probably best to avoid Google Mail.

But this is Usenet (or at least it gets gatewayed there) and there
are such limits here (either by common agreement or by RFC).
Probably not Google's fault.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top