Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Trailing whitespace is enough of a problem for programmers that editors like Emacs have special functions that highlight it or get rid of it automatically, and many coding standards require you to eliminate all instances of it. I'm not entirely sure why though. I can think of one practical reason of avoiding unnecessary whitespace, and it is that if people are not careful about avoiding it, then they might change it in between commits, and then we get diffs polluted with seemingly unchanged lines, just because someone removed or added a space.

This already sounds like a pretty good reason to avoid it, but I do want to see if there's more to it than that. So, why is trailing whitespace such a big deal?

share|improve this question

closed as primarily opinion-based by MichaelT, jwenting, Snowman, Ixrec, durron597 Sep 24 at 23:52

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

Trailing whitespace is indeed commit noise. Can't think of any other reason. –  Yannis Nov 24 '11 at 18:04
A good diff tool should be able to ignore trailing (and leading too, if you want) whitespace. After all, Emacs can do it, why not your diff tools? –  FrustratedWithFormsDesigner Nov 24 '11 at 18:11
Navigating to end of string with 'End' button could be messy with lots of trailing whitespace. –  Iaroslav Kovtunenko Nov 24 '11 at 18:15
I think you are asking the question the wrong way. The other way around is : « what are advantages of trailing white spaces ? ». The answer become pretty obvious when asking the right question ;) –  deadalnix Nov 24 '11 at 19:23
Maybe you should try programming in Whitespace‌​. *8') –  Mark Booth Nov 24 '11 at 22:40

8 Answers 8

up vote 52 down vote accepted

Reasons that it's important to me:

  • When I hit the "End" key, I expect the cursor to go to the end of the line (depending what editor I'm using and how it's configured) and I expect to be able to start typing right away. If the cursor gets there and I have to delete 3 or 4 characters, it's annoying. Especially if it's inconsistent throughout the file(s).

  • When I have string literals that span multiple lines, trailing whitespace can make the output string look incorrect when it's used.

While not strictly programming, whitespace can seriously mess up data entry, if there's trailing/leading in a file that will be parsed and used as input to something else. This happens the most when a clean, generated input file gets dirtied by someone editing it in Excel or something and then trailing whitespace (and other formatting issueS) can creep in.

share|improve this answer
Point 1 is a problem with your editor, not with the trailing white space. –  Marjan Venema Nov 24 '11 at 18:57
@MarjanVenema: Yes, an editor could be designed to go to the last NON-whitespace, but then you'd never know about the trailing whitespace (unless it was indicated some other way - also I'd probably find it weird that END doesn't go to end-of-line, which is what I'm used to). Which would be a problem with multi-line string literals. So then the editor would have to know that in a multi-line string literal, END should go to the last character, including whitespace. The editor I'm using is not that clever. –  FrustratedWithFormsDesigner Nov 24 '11 at 19:01
I would add selection with mouse is difficult to do without selecting extra spaces and that could matter if it's for a cut paste. Or that it generate noise when commit (eventually confilcts !) for nothing. No very big deal, but a lot of little anoyances. It gives the feeling that the previous dev was lazy and expect you to leave in his/her mess. –  deadalnix Nov 24 '11 at 19:20
Point 1 is precisely why I prefer having a particular style of trailing whitespace: blank lines being indented with the code they separate. This allows new code to be inserted immediately without having to indent in. –  Xiong Chiamiov Jul 14 '13 at 22:49
@MarjanVenema Less pedantry, more practicality. Who cares whether it's a shortcoming of the editor? Our tools will always have shortcomings. Learn to deal with them. As Frustrated pointed out, that problem is hard. This problem has a simple to implement solution: trim trailing whitespace. I'd rather editor designers/programmers spend time on problems without solutions. –  jpmc26 Nov 14 '14 at 19:07

I really hate trailing whitespace, but the exact reason is a bit vague.

I guess the origin of that feeling is not in programming, but in the desktop publishing field. Have you ever got a document typed by someone else that needed to be typeset into a publication? In all modern publications, especially when using columns, it is custom to have sentences follow each other sequentially within one paragraph, not starting a new line for each sentience. Well, when there is trailing whitespace, it takes a lot more effort to get it right (lots of search and replace actions that eliminate double spaces, etc...).

Other reasons (in programming), not good reasons, I know, but they bother my subconscious psyche in such aggravating intense manner that they compel me to eliminate it:

  • It takes more storage space than necessary
  • The parser will have to skip an extra character for no good reason when compiling
  • Some editors might add an extra blank line when WordWrap is on and the trailing space doesn't fit

Yes, yes I know! I know, these are junk reasons. I'm not a perfectionist, but... well maybe I am?

The last reason I can think of is inconsistent cursor movement. It feels like the cursor is hanging in thin air at the end of a line, every step to the right may cause it to aether drop or to hover farther to an unknown extent, it just feels unsteady (like those invisible or disappearing blocks where Super Mario used to jump on).

Probably I can be diagnosed with trailspacefobia?

share|improve this answer
Not my field, but it does seem strange to me... what kind of typesetting system is it you use there in desktop publishing, that doesn't ignore trailing/multiple spaces by default? I thought the way LaTeX and most programming languages do it was standard everywhere, except in consumer-level WYSIWYG junk. –  leftaroundabout Feb 28 '12 at 18:23
Adobe InDesign (does that also fall under consumer-level WYSIWYG junk?). It will ignore trailing spaces, yes, but replace all linefeeds with spaces in a paragraph and you'll end up with double spaces (which are not ignored), then an extra replace is needed to eliminate those. –  Louis Somers Feb 29 '12 at 14:23
"Desktop publishing" is not something you do with a "typesetting system". –  rakslice Nov 26 '13 at 23:06

A lot of these answers almost touch on the reason it's bad for me, but basically: It "breaks" text editors. My experience is with vim.

Vim is designed so that common actions map to letters so cleanly that you don't even have to think about what letter or key combination to hit. Various hotkeys allow the cursor to jump around the text so quickly that it takes only a couple keystrokes to get it to wherever you want. Even things like folding blocks of code is fast, since you can hit END or $ to jump to the end of the line, where the cursor should overlap { or } or ( or ) or something - there's no need to break your thought flow to get an idea onto the screen.

But then comes along some trailing whitespace, and the cursor's movements are no longer predictable. Your typing process is being interrupted because something you can't see is affecting where the cursor goes, so you have to break your train of thought to send it where it should be.

Ever notice how annoyed people get when they're really focused on a task and someone interrupts them? Yeah, finding trailing whitespace when it's least expected is exactly like that.

As a sidenote, I've also noticed that people who don't care about trailing whitespace are the ones who use the mouse for navigation, and tend to be slower typists because of it...

share|improve this answer
As a sidenote, typing speed has got nothing to do with whether you use your mouse for navigation or not. Some navigation is perfectly suitable for a mouse. ;p –  Steven Jeuris Nov 26 '11 at 3:26
If you use vim you can quickly remove all trailing whitespace: %s/ *$// –  Giorgio May 16 '14 at 21:52
@Giorgio I know, but I can't just do it anytime because it counts as a change in version control –  Izkata May 16 '14 at 22:49
@izkata: True. I reformat files I have to change before checking them in but this is a lost battle if other developers keep checking in files with trailing whitespace. –  Giorgio May 17 '14 at 9:13

I recently spent a day looking for a bug that turned out to be an unexpected trailing whitespace in data.

share|improve this answer
I think it is a bug; code processing human-generated input should discard trailing whitespace, and often leading whitespace should also be discarded. –  kevin cline Nov 24 '11 at 19:25
@kevincline - It should be at least. If it's not visible on the screen or on the printout, I don't wanna think about it. –  Rook Nov 24 '11 at 23:51
Because you spent a day looking for a bug, trailing whitespaces are a big deal? Please edit your answer to be more general than that. Add some background, argumentation, ... I personally never had trailing whitespace problems, but I don't use that as a conclusion that they aren't a problem either. –  Steven Jeuris Nov 25 '11 at 12:26
@Steven Trailing whitespace caused a bug which was difficult to track down. Seems like a good answer to me. You can imagine how some data parsing code could break in that situation. –  Will Sheppard Nov 20 '14 at 10:24

Besides the obvious problem that it breaks parsing in certain cases? As another poster has noted it can cause subtle and difficult to trace errors. Whitespace is not always insignificant. In some cases, trailing whitespace can significantly change the meaning of a line of code or data.

In most cases whitespace is there to format the code for human readers. Trailing whitespace may indicate several things including:

  • An incomplete statement;
  • A missing comment;
  • A mistaken edit; or
  • Sloppy editing.

Two of these may result in incorrect funtioning, and another may make it more difficult to comprehend the code.

share|improve this answer

When I select program source code in order to copy and paste it (or delete it) I find it a bit annoying to see all the irregular extra white space at the end of lines: since I have to read the code while I am selecting it, the trailing white space is an unneeded noise. Example (dots represent the white space):

if (fp)........
    prinft("File is NULL\n");

This example is artificial, but I have seen a lot of code that looks like this.

share|improve this answer
Can the downvoter please drop a note as to how to improve this answer? Thanks. –  Giorgio Sep 26 at 6:01

There are programming languages that are sensitive to end-line whitespace. For example, a TCL script will give an error if there is a whitespace at the end of a line.

share|improve this answer

Surprisingly, the most obvious answer is missing: trailing whitespace can and will produce difficult to find bugs.

The most obvious situation is multiline strings. Python, JavaScript and Bash are few examples of the languages which can be affected by this:



  File "", line 1
SyntaxError: EOL while scanning string literal

which is somehow cryptic and difficult to solve if the editor is not configured to display whitespace characters.

While syntax highlight can help avoiding such cases, it's even easier to not having the issue in the first place by not letting whitespace at the end of the lines. This is why some style checkers will raise a warning when encountering trailing whitespace, and some editors will trim them automatically.

enter image description here

Illustration: syntax highlight can help avoiding trailing whitespace in situations where it can lead to bugs, but don't rely just on it.

Another context, briefly mentioned in a previous answer, is data stored in files.

For instance, CSV files which contain trailing whitespace may cause data inconsistency which is also very difficult to detect: standards-compliant parsers will trim the whitespace (the standard indicates that leading or trailing whitespace is irrelevant, unless delimited with double-quotes), but some parsers may misbehave and keep the whitespace as a part of a value.

Other custom formats may specifically consider that whitespace is the part of the value, leading to consistent but still difficult to debug situations.

share|improve this answer

protected by MichaelT Sep 23 at 11:48

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.