What is a trailing whitespace?

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


Few months ago, it was raised that PR submitted code tend to have trailing whitespace changes in code diff along with other “real” code changes. This whitespace change is a diff where the change was removing one or more trailing whitespaces from the existing code. We then noticed that the editors we were using were automatically removing trailing whitespaces that were already existed in the code when the file get saved.
The agreed outcome was:

  • Disable this automatic feature
  • If must put removal of trailing whitespace changes to its own commit

Current Situation

Then it appeared that trailing whitespaces are slowly leaking into the code with new commits. Possibly mainly due to disabling the automatic trailing whitespace removal feature in the editors. See below example on how a new trailing whitespace could end up in a plain text file (e.g. .rb, .coffee). Let’s say the editor does not remove trailing whitespaces automatically on file save.

  var foo;
  # when you press ENTER after the (;), you will have two spaces to keep the indentation due to the editor’s behavior. The indentation reference will be the line it was earlier on. So following line has unnecessary trailing whitespaces as you freely moved to type var bar
  var bar;

Since you can’t see whitespaces (because of the nature of character), your code looks clean. But is it? Then why would editors worry about it? It seems like something to concern.
See this article on what Emacs offers to handle trailing whitespaces: http://www.gnu.org/software/emacs/manual/html_node/emacs/Useless-Whitespace.html

What could possibly go wrong having some few trailing whitespaces?

  • Jumping to END of the lines will put the cursor after the trailing spaces in the editor. But you didn’t see that you would end up there 🙁
  • Line wrapping in the editor would behave weirdly because of extra spaces (yeah they are also characters!)
  • String literals that span multiple lines can make the output look incorrect
  • Increase the file size unnecessary. Not a big deal, but still why?
  • The parser has to skip an extra character (or hundreds) depending on how many spaces you left out. (Only if the parser could talk, we would know how it feels…)

what we can do?

  • Have a Git hook to stop committing with trailing whitespaces
  • Have your editor show trailing whitespaces mode, so you see what you are committing 😉
  • Think Bold: Remove all existing trailing whitespaces in your code and re-enable automatic trailing whitespace removal feature in your editor.

References/Interesting Thoughts