Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of stripping trailing white spaces?

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?

like image 899
Pran Avatar asked Aug 16 '10 10:08

Pran


People also ask

What is strip trailing whitespace?

Trailing whitespace is any spaces or tabs after the last non-whitespace character on the line until the newline.

How do I get rid of whitespace trailing?

Type M-x delete-trailing-whitespace to delete all trailing whitespace. This command deletes all extra spaces at the end of each line in the buffer, and all empty lines at the end of the buffer; to ignore the latter, change the variable delete-trailing-lines to nil .

Which function is used to remove all leading and trailing whitespace in string?

You can use the STRIP function to remove both the leading and trailing spaces from the character strings.

Which function used to remove all leading and trailing whitespace in string in Python?

Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.


1 Answers

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 :-).

like image 137
sleske Avatar answered Sep 20 '22 00:09

sleske