Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
19  
Trailing whitespace is indeed commit noise. Can't think of any other reason. –  Yannis Rizos Nov 24 '11 at 18:04
12  
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
4  
Navigating to end of string with 'End' button could be messy with lots of trailing whitespace. –  Iaroslav Kovtunenko Nov 24 '11 at 18:15
8  
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
5  
Maybe you should try programming in Whitespace‌​. *8') –  Mark Booth Nov 24 '11 at 22:40

6 Answers 6

up vote 35 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
3  
Point 1 is a problem with your editor, not with the trailing white space. –  Marjan Venema Nov 24 '11 at 18:57
10  
@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
    
Going to the last non-white space character is what I like. And I don't have a problem with multi-line string literals as Delphi (my main language) doesn't support them. However, when I would need to check for that, I'd use hightlighting as most editors will show the trailing whitespace when you highlight one or more lines. An editor that does not hightlight trailing whitespace, or that hightlights beyond the last actual character (white space or otherwise) of a line would get my boot pretty darn quickly... –  Marjan Venema Nov 24 '11 at 19:18
2  
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
    
@MarjanVenema > Sorry, but coding isn't about what you prefers. You may prefers thoses stuffs, and good for you. But you are not alone in the world and other devs have to work with you. So what THEY prefers has to be taken into account. –  deadalnix Nov 24 '11 at 19:21

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
1  
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 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 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 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
7  
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. –  ldigas Nov 24 '11 at 23:51
1  
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

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)........
{....
    fclose(fp);.
}
else
{.....
    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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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