Whitespace: The Silent Killer

November 9, 2009

Ever have one of those days where everything you check into source control is wrong?

Also, how exactly is that day is different from any other? But seriously.

Code that is visible is code that can be wrong. No surprise there. But did you know that even the code you can't see may be wrong, too?

These are the questions that drive young programmers to madness. Take this perfectly innocent code, for example.

code-whitespace-invisible.png

Looks fine, doesn't it? But hold on. Wait a second. Let's take another, closer look.

code-whitespace-visible.png

OH. MY. GOD!

If you're not a programmer, you may be looking at these two images and wondering what the big deal is. That's fine. But I humbly submit that, well, you're not one of us. You don't appreciate what it's like to spend every freaking minute of every freaking day agonizing over the tiniest details of the programs you write. Not because we want to, you understand, but because the world explodes when we don't.

I mean that literally. Well, almost. If one semicolon is out of place, everything goes sideways. That's how programming works. It's fun! Sometimes! I swear!

We got into this industry because, quite frankly, we are control freaks. It's who we are. It's what we do. Now to imagine, to our dismay, that there's all this stupid, useless whitespace at the ends of our lines. Stuff that's there, but we can't see it. Well, those are the nightmares OCD horror movies are made of. I have a full-body itchiness just talking about it.

Depending on how far down the rabbit-hole you want to go, there's any number of things you could do here:

  • Have a post-build step, perhaps something with a regular expression like \s*?$ in it, that auto-cleans extra spaces checked into source control
  • Execute a local macro which removes whitespace from ends of lines
  • Have a special rule to highlight extra spaces
  • Run your IDE in whitespace-always-visible mode, or toggle it frequently

OK, fine, so maybe the world won't explode if there are a few extra bits of whitespace in my code.

But all the same, I think I'll go back and make extra double plus sure no more of that pesky whitespace has accumulated in my code when I wasn't looking. Just because I can't see it doesn't mean it's not out to get me.

Posted by Jeff Atwood
239 Comments

I think it's a great idea to really get and fight whitespaces. Here is my solution as an Xcode user script:

http://on-pro.tumblr.com/post/244987553/whitespaces

Thanks Jeff!

nikan on November 18, 2009 4:35 AM

What I hate is copying and pasting code and there is a ` instead of a ' somewhere!!! one time it took me a freaking hour to find what was the problem with the quotation!!!

dedektif on November 18, 2009 5:18 AM

What I hate is copying and pasting code and there is a ` instead of a ' somewhere!!! one time it took me a freaking hour to find what was the problem with the quotation!!!

dedektif on November 18, 2009 5:22 AM

yea white spaces are definitely gay for programmers. i know because i iz one. a suggestion is to have a script built into your checkin routine that forces all code to limit spaces between words to 1. if ur gay and dont know how to do this or leik your language doesnt support regex then there is an easy way to. create a recursive function that does a search and replace - replacing double spaces "__" with single spaces "_". o yea and it helps to be a little drunk when writing recursive code.

madmax on November 18, 2009 8:17 AM

What I hate is copying and pasting code and there is a ` instead of a ' somewhere!!! one time it took me a freaking hour to find what was the problem with the quotation!!!

momo on November 18, 2009 9:52 AM

I'm glad I don't program in whatever god-forsaken language this is. Because whitespace that screws up the program is just moronic.

Scott M on November 19, 2009 1:46 AM

I hate unnecessary whitespace at the end of lines too!

It seems completely irrational take make it such a big concern, but who really cares about rationality!? We know it rashes us, and we know the their nagging existance will not be silenced until they are removed

007 casus on November 19, 2009 6:04 AM

@Dave: "I pad all my code to the 80th column with spaces. Cheaper than medication."

72 columns is enough - you have to leave room for the girl to add the line numbers so you can merge the decks.

DavidR on November 20, 2009 8:14 AM

Uncredible, I have read the post about the bad codes of flickr, yahou., reddit etc. And now that one. You whould work for me :-)

Stefan on November 20, 2009 8:29 AM

Myabe its just the example you gave, but its no big deal really, get a life.

Michael on November 24, 2009 3:43 AM

If it's my file, I press Ctrl+K+D sometimes, and that usually gets rid of them + the other formatting annoyances. If I modify somebody other's file, I can mess up the svn patch by doing that, so sometimes when I notice the whitespaces in the patch file, I go back to the file and delete them. Anyway it's annoying.

csuporj on November 25, 2009 4:54 AM

> At least in corporate "enterprise application" code,
> the problem with trailing whitespace is that it often
> indicates cruddy, cut-n-paste coding by cut-rate,
> one-size-fits-all, lowest-bid consultants.

This whole discussion is ridiculous enough, when it's a tool problem that can be solved with proper editors. But, then, blaming consultants? I know of many lamo-waiting-for-retirement-even-tho-they-are-only-in-their-30s-programmers that write the worst code on the planet.

Let's be nice. Let's get good tools.

bak on November 27, 2009 11:05 AM

My old programming teacher always wrote the most incomplete and syntax violating examples in class - his excuse? "I let the parser find my typing errors"

Sam Johnson on November 30, 2009 3:45 AM

I have a full-body itchiness just talking about it.
write well,thanks for post.
so maybe the world won't explode if there are a few extra bits of whitespace in my code.

valves manufacturer on November 30, 2009 6:50 AM

very good blog, I come here every day,thanks very much for post.

sanitary fittings on November 30, 2009 6:52 AM

Ask very well,No surprise there. But did you know that even the code you can't see may be wrong, too?

ball valves on November 30, 2009 6:54 AM

what it's like to spend every freaking minute of every freaking day agonizing over the tiniest details of the programs you write0

butterfly valves on November 30, 2009 6:57 AM

Seriously - 8000 comments and only one person mentioned that this was a) horrible markup and b)just plain old bad code. Never ever mix markup and c# code. That should be an asp repeater bound to a data source. Who cares about the whitespace...

Derek on December 1, 2009 10:20 AM

Here's how I keep my whitespace under control in emacs:

(setq-default indent-tabs-mode nil
require-final-newline t)
(setq whitespace-check-indent-whitespace nil)

(require 'whitespace)
(global-whitespace-mode t)
(add-hook 'before-save-hook
'whitespace-cleanup)

Jack on December 2, 2009 4:12 AM

I m sure sure I use other editors for python, one showing me every tab, I tend to do the same for YAML files.

sinyal kesici on December 3, 2009 1:42 AM

thanks admin
information is the most beautiful treasures

sex video on December 3, 2009 1:42 AM

I use Visual Studio 2005 and every now and then I use this procedure to bulk-remove trailing white space from the code:

* Press "CTRL + SHIFT + H" to open "Replace in Files" dialog
* Find what = ":b+$" (colon, small b, plus, dollar)
* Replace with = "" (empty string)
* Use = "Regular expressions"
* Look in = ""
* Look at these file types = "*.asp;*.css;*.js"

Works nicely. But until you get used to this procedure, remember to backup your files.

Salman on December 4, 2009 10:22 AM

Myabe its just the example you gave, but its no big deal really, get a life.
I agree

toshiba laptop batteries on December 7, 2009 4:12 AM

very great blog, I come here every day,thanks very much for post.

laptop batteries on December 7, 2009 4:16 AM

thanks admin
Are you really cool

tarsus on December 7, 2009 11:51 AM

Removing trailing whitespace on save is always a good thing. Even if there's no functional difference, it's nice to be able to compare versions in your SCM without having to worry about whitespace. Now, don't even think about opening the "space vs. tab" can of worms!

Simon on February 6, 2010 11:22 PM

CTRL+K, CTRL+D baby.

Jefferson Taylor on February 6, 2010 11:22 PM

I tried Acme::Bleach once and it destroyed the code. Maybe it was because it was Perl 5.004 on HP-UX or something.

I also loved that the Delphi IDE automatically removed trailing whitespace.

I guess I must be more hardcore OCD than Jeff, because I often go through the code and check for trailing whitespace manually! Though I find it's only a problem if I copy/paste.

Trailing whitespace is boring. Tabs vs spaces is the real horror!

John Ferguson on February 6, 2010 11:22 PM

@a_m0d:
> In Python, EOL whitespace can be relevant!
> if condition1 and \...
condition2
> Python will complain about that trailing whitespace after the
> line-continuation character!

That's not Python specific, and has nothing to do with the Pythonesque kind of whitespace-significance (that is indeed beginning-of-line only). It's just a consequence of the fact that, if you want to escape a character, you have to put that character immediately after the backslash. E.g. if you're in a string and you want to insert a quote mark*, you might write \"; \[space]" wouldn't work. So if you want to escape an end of line, of course you have to have the backslash write before the end of line; otherwise you're just escaping a random space.

So that's not really an instance of actual significant whitespace any more than the fact, if you have a variable "foo", you can't refer to it as "fo o", means that that language has 'significant whitespace.

Simon on February 6, 2010 11:22 PM

It worked. Comparing the old and new code, I found ONE SPACE at the end of the line that didn't work.

aion items on February 6, 2010 11:22 PM

In 1924, George Mallory, when he was asked why he wanted to climb Mount Everest, said "Because it's there." I suppose most still did not understand why. Similarly, if you didn't understand the pain of this article, you probably never will. :-)

Carleton on February 6, 2010 11:22 PM

Just be glad your boss doesn't inist on using tabs instead of spaces! *le sigh*

On a side note, you'd think that being an OCD freak you'd have spotted the following:

"Also, how exactly is that day is different from any other? But seriously."

is=any?

Jon Cage on February 6, 2010 11:22 PM

@misteraidan

"I recently forced all the programmers on my team to make sure their VS options had tabs NOT SPACES..."

This is evil. Could you say what team you run so I can avoid it.

Anon on February 6, 2010 11:22 PM

I don't see what all the fuss is about.

Voice of reason on February 6, 2010 11:22 PM

I know the answer, I'm hired? :)

Ion Todirel on February 6, 2010 11:22 PM

Whitespace is like the virtual camouflaged predator waiting to viciously shank you through the guts while you're staring at in its invisible face.

All the while Linus, standing by with his little pet git says "serves you right dumb-s***, I told you you weren't as smart as me."

I hate you whitespace.

Evan Plaice on March 27, 2010 4:40 PM

i love when copying from one app and pasting to another blatantly destroys all sanity in formatting, whitespace in particular. in the following example, i ended up with 'random' excess spaces, but none of the occasional extraneous newlines, when copying from a web browser and pasting into my im client as is. the end result was this:

  I     don't see  what      all the     fuss  is about. Voice of reason on February  6, 2010 11:22 PM         I know the answer, I'm hired? :) Ion Todirel on February  6, 2010 11:22 PM  

copied from above. note the trailing whitespace at the end. also, it's not only text which was actually indented which was thrashed in the vortex of hell's anus, but poor words like "I" and "fuss." as horrible as my typing habits are, also note, amusingly, that the evil demon sharting out whitespace characters decided to take a nap when he saw that someone had an answer, and didn't mess around with that smilie, as if to say, "i'm bad, but not Chuck Norris bad." then he turned around and screwed the proverbial pooch on "February 6", a 2nd time.

Apparently February is cause for computer-related demonic concern. Satan loves February more than i love pancakes, and you are all doomed to a hell of broken parsers. To hell i say! And they don't have regular cupcakes in hell! Only cupcakes of DOOM! Doom, doom, doom...

538930242 on September 29, 2010 3:21 PM

btw by extraneous newlines i mean that in other instances newlines also appeared to flaunt the ability to propagate themselves in inconvenient places. here have a random
$'\n'
i'm all out of cookies.

538930242 on September 29, 2010 3:27 PM

for further amusement, view my profile on this site, and see that the text is stripped of all newlines, even ones explicitly invoked by an html break tag, and worse, the pre tags were stripped and formatting of the most crucial parts (and flavors) was stolen, along with all of my remaining innocence. i shall take refuge in Cookie Monster. All hail big blue furry glutton! (the crowd thereby chants, "Hail! Hail!", and all the world is saved, until they make a sequel to this apocalyptic mayhem. But fear not! Pajama Hero Nemo, aka Little Nemo: Dream Master, is on guard!)

538930242 on September 29, 2010 3:33 PM

«Back

The comments to this entry are closed.