Hidden version of Whitespace Best Practice

I think most people agree that trailing spaces are not good practice. Many editors will display it for you or automatically disable it.

Consider this Python function as a simple example:

Whitespace Example Screenshot

Additional spaces on lines 11 and 13 are incorrect. What I'm interested in is line 10. If an empty line inside the control unit that does not change the indent has spaces?

Most editors that I used will hold the cursor at the indent level of the previous line, so creating an empty line without leading spaces requires additional formatting. What is the best practice? Should line 10 have spaces or not?

+4
source share
4 answers

I will try to answer your question by sticking to your Python example, quoting their style guide.

From PEP-8 :

Method definitions within a class are separated by a single blank line.

From Wikipedia (blank line) :

An empty line usually refers to a line containing null characters (not counting the characters at the end of the line); although it can also refer to any line that does not contain visible characters (consisting only of spaces).

If you consider the definition of Wikipedia, you may ask why null characters are preferable.

For one, it’s easier, even if autoindent is enabled in your editor, you just fill your file with extra bytes for no good reason.

Secondly, the regex for an empty null character string is also simpler, '^ $' usually has something like '^ \ s * $'.

As the other answers pointed out, it makes no sense to perform wise to put spaces. Without good reason for this, I would say that it is best to abandon this and keep it simple. Can you imagine a situation where a line of a null character will be processed differently than a line with some spaces? I would not like to program in this language. Putting in spaces seems baroque to me.

+3
source

When it comes to code execution, it makes absolutely zero difference; the practice that I saw most in python is the one who has spaces, but I don't think anyone can reasonably say that one is objectively better than the other.

+5
source

As @PinkElephantsOnParade writes, it doesn't matter for execution. Thus, it is solely a matter of personal aesthetic preference.

I myself found that my editor displays trailing spaces, since I think trailing space is a bad idea. Thus, your line 10 will be highlighted and constantly looks in my face. Nobody wants this, so I would say that line 10 should not contain spaces. (Which, by chance, as my emacs editor handles this automatically.)

+2
source

To each his own, but one way to look at it: the style of the code is human readability. Thus, the final white space is a problem only if it extends the length of the line preceding the pre-existing (self-imposed) limit (for example, 80 char).

On the other hand, if you constantly display white space in your editor, and it matters to you, I personally will keep it there, as it would (for some, at least) more effectively, the space is present; if you decide to add code to this line at some point, you won’t need to add extra white space.

+1
source

All Articles