views:

128

answers:

7

Which is preferred ("." indicating whitespace)?

A)

def foo():
    x = 1
    y = 2
....
    if True:
        bar()

B)

def foo():
    x = 1
    y = 2

    if True:
        bar()

My intuition would be B (that's also what vim does for me), but I see people using A) all the time. Is it just because most of the editors out there are broken?

A: 

Emacs does B) for me, but I really don't think it matters. A) means that you can add in a line at the correct indentation without any tabbing.

Skilldrick
A: 

That empty line belongs to foo(), so I would consider A to be the most natural. But I guess it's just a matter of opinion.

Toon Van Acker
A: 

I wouldn't necessarily call the first example "broken", because I know some people hate it when the cursor "jumps back" when moving the cursor up or down in code. E.g. Visual Studio (at least 2008) automatically prevents this from happening without using any whitespace characters on those lines.

Deniz Dogan
+2  A: 

If you use A, you could copy paste your block in python shell, B will get unexpected indentation error.

S.Mark
+2  A: 

The PEP 8 does not seem to be clear on this issue, although the statements about "blank lines" could be interpreted in favor of B. The PEP 8 style-checker (pep8.py) prefers B and warns if you use A; however, both variations are legal. My own view is that since Python will successfully interpret the code in either case that this doesn't really matter, and trying to enforce it would be a lot of work for very little gain. I suppose if you are very adamantly in favor of one or the other you could automatically convert the one to the other. Trying to fix all such lines manually, though, would be a huge undertaking and really not worth the effort, IMHO.

Michael Aaron Safyan
+1  A: 

My experience in open-source development is that one should never leave whitespace inside blank lines. Also one should never leave trailing white-space.

It's a matter of coding etiquette.

wm_eddie
+1: I have my IDE strip trailing whitespace.
S.Lott
not in a language where indentation is mandatory like python
Lo'oris
@Lo'oris Indentation is mandatory for code but not for blank lines. Therefore any spaces in a blank line are unnecessary.
wm_eddie
A: 

TextMate breaks block collapsing if you use B, and I prefer A anyway since it's more "logical".

Lo'oris
Interesting. I just tried it with EditPadPro which also uses the level of indentation for code folding, and it handles empty lines well.
Tim Pietzcker