views:

59

answers:

3

I got into the habit of removing trailing white spaces from my source file. In fact, I have my editor to do this automatically. I got in this habit by using git; it created a habit that I adhere to.

My question relates to the fact that I cannot justify this behaviour. I can understand that in some fields, such as web designers, it may impact their final result. For programmer though, what do we gain out of it? Can't we just leave it in?

+7  A: 

I also like to trim whitespace. There is no technical requirement to do it, but it has some advantages:

  • Trailing whitespace is irritating in many editors when the cursor changes lines, as you may end up in the "trailing whitespace area" of the line when you go from a longer to a shorter line, requiring extra keystrokes to get to the part you want to edit
  • It may cause your editor to show a horizontal scrollbar that would otherwise not be necessary, which in turn forces you to scroll to the right to make sure you are not missing text

Most of all, however, there is a huge benefit (IMHO) to use consistent formatting throughout the source code (spacing, indentation, brace style...). This makes the code easier to read, and avoids large diffs from reformattings (if it's always corretly formatted, no need to reformat).

Therefore I would recommend letting a formatter run automatically whenever you save (or at least for every commit). That way, trailing whitespace can be eliminated as a side effect :-).

sleske
Amongst the mentioned reasons, I think you make a good point regarding consistency in order to reduce large diffs.
Pran
+3  A: 

You don't say what language you're using, but in C-alikes, trailing whitespace affects macros.

Consider:

#define FNORD() \
   something complicated here

The \ only works if it's the last character on the line --- so whitespace after it will cause it to break.

David Given
+1 Good point and the same concept can be applied to other situations. For instance, in ruby you can use the \ to have a string on multiple lines.
Pran
Perl and sh/bash also use a backslash to continue a line, and would suffer from the same problem. The same goes for "here documents" in Perl and sh/bash.
sleske
Of course, if you actually *want* trailing whitespace in a here-document, you don't want to strip it. But fortunately I don't have to program in bash...
sleske
+1  A: 

Mandatory link to a Jeff Atwood post.

Whitespace: The Silent Killer

Marco Mariani