From: HH on
I have a question about best practices when it comes to line wrapping/
continuation and indentation, specifically in the case of an if
statement.

When I write an if statement with many conditions, I prefer to use a
parenthesis around the whole block and get the implicit continuation,
rather than ending each line with an escape character. Thus, using
the example from the style guide (http://www.python.org/dev/peps/
pep-0008/) I would write:

if (width == 0 and
height == 0 and
color == 'red' and
emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")

The problem should be obvious -- it's not easy to see where the
conditional ends and the statement begins since they have the same
indentation. Part of the problem, I suppose, is that Emacs indents
'height' and the other lines in the conditional to 4 spaces (because
of the parenthesis). How do people deal with this situation?

Thanks,
Henrik
From: Martin P. Hellwig on
On 05/27/10 13:22, HH wrote:
> I have a question about best practices when it comes to line wrapping/
> continuation and indentation, specifically in the case of an if
> statement.
>
> When I write an if statement with many conditions, I prefer to use a
> parenthesis around the whole block and get the implicit continuation,
> rather than ending each line with an escape character. Thus, using
> the example from the style guide (http://www.python.org/dev/peps/
> pep-0008/) I would write:
>
> if (width == 0 and
> height == 0 and
> color == 'red' and
> emphasis == 'strong' or
> highlight> 100):
> raise ValueError("sorry, you lose")
>
> The problem should be obvious -- it's not easy to see where the
> conditional ends and the statement begins since they have the same
> indentation. Part of the problem, I suppose, is that Emacs indents
> 'height' and the other lines in the conditional to 4 spaces (because
> of the parenthesis). How do people deal with this situation?
>
> Thanks,
> Henrik

Well style guide aside (if pylint is happy with it, so am I) it depends
on what I want to emphasize.

For example if it is really one long line with every item in it being
equally important I do this:
if width == 0 and height == 0 and color == 'red' and emphasis ==
'strong' \
or
highlight > 100:
raise ValueError("sorry, you lose")

In case it doesn't display correctly, I break up the line to nearest
80th character and align the remaining part on the next line to the
right to the 80th character.

If I want to emphasize visually a certain part I would do something like
this:

if width == 0 and height == 0 and color == 'red' \
and emphasis == 'strong' or highlight > 100:
raise ValueError("sorry, you lose")

But these are my preference, and since it is most likely that I have to
read again what I have written I write it in a way that it is most
readable to me within the constraints of pylint.

--
mph

From: Jean-Michel Pichavant on
HH wrote:
> I have a question about best practices when it comes to line wrapping/
> continuation and indentation, specifically in the case of an if
> statement.
>
> When I write an if statement with many conditions, I prefer to use a
> parenthesis around the whole block and get the implicit continuation,
> rather than ending each line with an escape character. Thus, using
> the example from the style guide (http://www.python.org/dev/peps/
> pep-0008/) I would write:
>
> if (width == 0 and
> height == 0 and
> color == 'red' and
> emphasis == 'strong' or
> highlight > 100):
> raise ValueError("sorry, you lose")
>
> The problem should be obvious -- it's not easy to see where the
> conditional ends and the statement begins since they have the same
> indentation. Part of the problem, I suppose, is that Emacs indents
> 'height' and the other lines in the conditional to 4 spaces (because
> of the parenthesis). How do people deal with this situation?
>
> Thanks,
> Henrik
>
One possible solution

if (
width == 0 and
height == 0 and
color == 'red' and
emphasis == 'strong' or
highlight > 100
):
raise ValueError("sorry, you lose")


JM
From: Tim Chase on
On 05/27/2010 07:22 AM, HH wrote:
> When I write an if statement with many conditions, I prefer to use a
> parenthesis around the whole block and get the implicit continuation,
> rather than ending each line with an escape character. Thus, using
> the example from the style guide (http://www.python.org/dev/peps/
> pep-0008/) I would write:
>
> if (width == 0 and
> height == 0 and
> color == 'red' and
> emphasis == 'strong' or
> highlight> 100):
> raise ValueError("sorry, you lose")

While it's not PEP material, I tend to use the coding standards I
learned working for Computer Sciences Corporation (10 yrs ago, so
things may have changed) that mandated 2 levels of indentation
for continued lines, turning the above into

if (width == 0 and
height == 0 and
color == 'red' and
emphasis == 'strong' or
highlight> 100):
# or the closing "):" on this line,
# aligned with the previous line
raise ValueError("sorry, you lose")

which is fairly close to Jean-Michel's proposal.

-tkc



From: MRAB on
HH wrote:
> I have a question about best practices when it comes to line wrapping/
> continuation and indentation, specifically in the case of an if
> statement.
>
> When I write an if statement with many conditions, I prefer to use a
> parenthesis around the whole block and get the implicit continuation,
> rather than ending each line with an escape character. Thus, using
> the example from the style guide (http://www.python.org/dev/peps/
> pep-0008/) I would write:
>
> if (width == 0 and
> height == 0 and
> color == 'red' and
> emphasis == 'strong' or
> highlight > 100):
> raise ValueError("sorry, you lose")
>
> The problem should be obvious -- it's not easy to see where the
> conditional ends and the statement begins since they have the same
> indentation. Part of the problem, I suppose, is that Emacs indents
> 'height' and the other lines in the conditional to 4 spaces (because
> of the parenthesis). How do people deal with this situation?
>
I would probably use half-indentation:

if (width == 0 and
height == 0 and
color == 'red' and
emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")

Try doing that with tabs! :-)
 |  Next  |  Last
Pages: 1 2 3 4 5
Prev: type error raise
Next: Minor annoyances with properties