I <3 Steve McConnell*
Coding Horror
programming and human factors
by Jeff Atwood

Nov 9, 2009

Whitespace: The Silent Killer

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.

[advertisement] JIRA 4 - Simplify issue tracking for everyone involved. Get started from $10 for 10 users.

Posted by Jeff Atwood    View blog reactions
« Preserving Our Digital Pre-History
Parsing Html The Cthulhu Way »
Comments

Dude, Jeff, calm down.. it's just a bit of space.

stuckinphp on November 9, 2009 1:10 AM

$foo =~ s/.*?\s+// if it bothers you that much.

I prefer languages where whitespace doesn't matter to the compiler - C, Perl, Lisp...

Paul N on November 9, 2009 1:38 AM

If the problem is the extra bytes sent over the network, perhaps the ASP template engine has an option to strip the whitespaces on the fly ? Another option is compressing the response, I don't know how to do it in ASP but with servlets is easy to do, and the browser takes care of the unzipping automatically. This is convenient for very heavy pages of course. In any case, I prefer the readability provided by the extra spaces.

lluis on November 9, 2009 1:39 AM

Different material. I will try this. Thanks so much :)

GORGO on November 9, 2009 1:42 AM

If that code ends up getting downloaded, like with JavaScript, HTML, or CSS, the extra white space wastes download time, and costs money. I bet the world could save hundreds of dollars by taking out the trailing spaces.

Kirk Cerny on November 9, 2009 1:43 AM

Imagine how it is for those of us who spend a lot of time in Python world, where the whitespace isn't just an OCD obsession, but can actually modify the functionality of code!

Rizwan Kassim on November 9, 2009 1:51 AM

The Delphi IDE has a neat setting that automatically throws away trailing spaces.

Angus Glashier on November 9, 2009 1:59 AM

"Run your IDE in whitespace-always-visible mode, or toggle it frequently"

A friend of mine calls it "Pac-man mode". All of a sudden you see all of these little dots that need to be gobbled up...

Paul Gordon on November 9, 2009 2:03 AM

> Python world, where the whitespace isn't just an OCD obsession, but can actually modify the functionality of code!

But isn't EOL whitespace irrelevant to Python as well?

Jeff Atwood on November 9, 2009 2:05 AM

@Rizwan : +1 ! We want code to be space sentitive!

Or at least a "Trim Trailing and save" option in Visual Studio as we have on Notepad++

Laurent on November 9, 2009 2:05 AM

Actually, it should be a feature of the IDE. The solution is just a plugin away :-)

Just like in Vim : http://vim.wikia.com/wiki/Remove_unwanted_spaces (let the holy wars begin...)

Steve Schnepp on November 9, 2009 2:06 AM

eclipse also has a "remove trailing whitespace" option.

Stroboskop on November 9, 2009 2:07 AM

I know exactly what you mean. Carefully crafted source code, nicely tucked away in your repository, and then someone with TextMate comes along and automagically adds random whitespace all over the place. Byebye useful diffs!

Stefan on November 9, 2009 2:09 AM

You seem like the perfect candidate to start using the Whitespace language.

http://compsoc.dur.ac.uk/whitespace/

Andre on November 9, 2009 2:10 AM

Eclipse can format your code and/or remove trailing whitespace on save. I think that's the optimal solution.

thSoft on November 9, 2009 2:14 AM

Whitespace.
You can't see it,
it must not matter.
If it does,
Tool broken.

captcha: Danner peet

Dopey on November 9, 2009 2:16 AM

OMG! Whitespace! Makefiles just popped in front of my eyes. Tabs and all that.

Sam on November 9, 2009 2:17 AM

People who don't care about whitespace, don't have to deal with merging code. I make sure everyone I work with has to fix their own whitespace merge conflicts. Sometimes they even learn to set their IDE up correctly.

The next question is; should we extend this to automated code formatting? Personally I am not willing to take that step yet, because I don't like any automated code formatters, and more importantly all the code I write is formatted correctly. :)

Mike Miller on November 9, 2009 2:17 AM

Diffing for no reason is a mortal sin.

Braden on November 9, 2009 2:18 AM

Aside of looking bad, trailing whitespace (or inconsistent tab/space policies or line endings for that matter) can really screw merges in revision control systems and can turn your "blame" (or how that function is named in your version control system) session into a world of hurt because seemingly identical lines actually aren't.

Since I moved over to git, I got really picky about this as the git workflow makes you merge much more often as creating branches is so easy. Also, git is really picky about whitespace too and warns you very loudly if you screw up.

Philip

Philip Hofstetter on November 9, 2009 2:18 AM

I always highlight bad whitespace in vim.
Search for "RedundantSpaces" here:
http://www.pixelbeat.org/settings/.vimrc

Pádraig Brady on November 9, 2009 2:20 AM

@Jeff:
it is. Only "leading" whitespace has any meaning in Python.
Anyway, I set up my Vim to treat whitespace at EOL as highlight as error. Nice.

zgoda on November 9, 2009 2:20 AM

White space has rights too!

Steve on November 9, 2009 2:20 AM

As soon as I found an editor that trims whitespace at the end of all lines, had a sane autoindent/autocomplete/codecolor. My sanity thanked me.

Ólafur Waage on November 9, 2009 2:21 AM

Back in the days of RCS, we developed code in Windows and Unix environments with their differing conventions of newlines. When Windows guys saved a file, they replaced every Unix newline with Windows newline, and vice versa. It was horrible mess: you could never tell which line actually were modified.

Then me migrated to CVS, and due the problems, we also deprecated use of Windows newlines. There was even a trigger that prevented committing files that contained them. dos2unix was to be used every time before commit.

Everything was fine for a few months, CVS making our lives easier, until one day the commit kept failing even though dos2unix was used conscientously.

It appeared that during the realier years, the newline struggle somehow corrupted the files, and it was unrecoverable. Upon every commit, CVS became confused as to what newlines to use and sometimes it reverted them to the Windows newlines. I don't know why, and I don't bother to know why, but this actually happened.

So, why on earth, newlines do matter in plain textfiles when it comes to revision control? You could still store them in whatever format they are, and get them outthe same, but every comparison related operation could ignore the differences sooooooo easily.

Jani on November 9, 2009 2:22 AM

As Sam alluded to, be glad you're not hacking make files. Putting in four spaces where you meant to put in a tab can ruin your day.

Python is easier to deal with. Just set your editor to always insert spaces and you're fine. What you see is what you get. But make files depend on the distinction between tabs and spaces. Hard to imagine that decades later programmers still put up with this.

John on November 9, 2009 2:23 AM

For Visual Studio, the magic shortcut is Ctrl-K,D (Format document). Note that this will not remove extra whitespace at the end of comments, so you might also wish to do the regular expression replace (find :b+$ and replace with empty string).

lini on November 9, 2009 2:24 AM

Hello, I am not one of you but still I enjoy reading this blog. I am looking at your example code and wondering what the big deal is, since I am not a programmer. May I ask where is the bug in the whitespace? Does it make the browser crash? Or does the program behave differently? I have read the comments and I understand that this whitespace can trigger false positives when comparing two versions of a same file using a source control tool. But what is the effect when running the code?

NotAProgrammer on November 9, 2009 2:39 AM

I actually prefer having trailing whitespace in my lines, for a specific scenario: to maintain indentation level. Consider this:

....for (int i = 0; i != 10; ++i)
....{
........DoSomething();
........
........DoSomethingElse();
....}

I prefer the above to this:

....for (int i = 0; i != 10; ++i)
....{
........DoSomething();

........DoSomethingElse();
....}

Barry Kelly on November 9, 2009 2:43 AM

"- Run your IDE in whitespace-always-visible mode, or toggle it frequently"

Or use an IDE that always displays _trailing_ whitespace (and tabs, if indentation mode is set to spaces).

oliver on November 9, 2009 2:44 AM

Unless the VotingImages and CommentText functions render "" tags, the code has an actual bug.

Josh on November 9, 2009 2:57 AM

Oh no, you don't HTML Encode your comments, just strip the offending strings -- anyways that should be "TD"

Josh on November 9, 2009 3:00 AM

I agree... OH. MY. GOD! If I ever saw this in my code I would kill the mofo who did it. Luckily I've not had that problem... yet.

OCD Follower on November 9, 2009 3:02 AM

We have one developer here (only one) who doesn't run with Visual Studio's "Auto-Format on Save" feature enabled. You can always tell when he was the last person to check in a file. Well, you can tell when you're diffing your own changes at least.

Tullo on November 9, 2009 3:06 AM

@NotAProgrammer: Besides space issues (the output will be a few bytes longer), there's nothing wrong. Except that IT IS *HORRIBLE*. It makes you feel like the person who did it should die an horrible fiery death of a thousand burning suns. Twice. A day.

Martinho Fernandes on November 9, 2009 3:11 AM

Trailing whitespace and tabs (bird tracks in Visual Studio) should definitely be removed from the end of lines. Sometimes I think my colleagues are trying to encode Morse code with a combination of tabs and spaces.

Daniel Ballinger on November 9, 2009 3:39 AM

In Python, EOL whitespace can be relevant!
Try the following piece of code out:

if condition1 and \...
condition2
....block of statements

Python will complain about that trailing whitespace after the line-continuation character!

a_m0d on November 9, 2009 3:41 AM

And I was just deleting some whitespace from end of my lines today... Damn you, copy and paste! ;-)

Incidentally, has anyone done studies into the levels of OCD within software development? I have a mild form (need to check the house is locked a few times before leaving), and some code foibles.

Mike Kingscott on November 9, 2009 3:46 AM

I'm surprised! I use Kate for editing and I always thought that *every* decent editor/IDE had a "remove trailing spaces" option.

Angel on November 9, 2009 3:49 AM

> "Now to imagine, to our dismay, that there's all this stupid, useless whitespace at the ends of our lines."

There are no(*) whitespace-at-the-end-of-the-line issues in Perl.

(*) Except within literal texts, patterns (e.g. regular expressions) and formats.

Yes I'm very smug about it ;)

More seriously one thing I hate about end-of-line whitespace is this issue:

for my $variable (some_array) {
# many
# lines
# of
# code
}

Frequently in code from other people there will be a whitespace after the opening brace, which makes it impossible to click your mouse at the end of the line and jump to the closing brace - instead I find my cursor stuck in the end of some annoying useless whitespace.

Thankfully I can remove all end-of-line whitespace in my editor with a single command, so it's not such a big issue as you make it out to be. I just have to remember to do it :)

Solidstate on November 9, 2009 4:01 AM

Barry: why not let your IDE insert those spaces for you? It's what it does best, after all. It will also create the spaces for you on a new line.

remmelt on November 9, 2009 4:02 AM

Stack Overflow: How to automatically remove trailing whitespace in Visual Studio 2008?
http://stackoverflow.com/questions/82971/

Daniel Ballinger on November 9, 2009 4:03 AM

Git treats trailing whitespace as a *bad thing*.

Then, when you try to commit code that was written on Windows (CR/LF) it thinks you've got trailing whitespace.

Then you have to add -n on the end of the commit command, particularly if you're trying to commit someone else's Ruby plugin.

I think Macs add CR/LF or not and don't care.

Francis Fish on November 9, 2009 4:04 AM

The problem here is not the whitespace, it is the fact that there are two different languages weaved together with different rules for whitespace (one of which is C# code and one of which is HTML). In the HTML the whitespace means one thing, in the code it means something else (it is insignificant). The programmer has to deal with two different syntaxes in the same file! If the data and code were expressed in the same syntax, this wouldn't be an issue.

If you were using a web framework where the HTML is expressed as code, this issue wouldn't arise. Consider a table in the seaside web framework or a framework for one of the lisp dialects.

html table:
[html tableRow:
[html tableData: 'cell1'.
html tableData: 'cell2'.
html tableData: 'cell3'].
html tableRow:
[html tableData: 'cellA'.
html tableData: 'cellB'.
html tableData: 'cellC']]

(:table :border 0 :cellpadding 4
(loop for i below 25 by 5
do (htm
(:tr :align "right"
(loop for j from i below (+ i 5)
do (htm
(:td :bgcolor (if (oddp j)
"pink"
"green")
(fmt "~@R" (1+ j)))))))))

In these examples you couldn't have the whitespace issue since it is all expressed as code in a single language (and would have to respect the syntax and whitespace rules for one language). This is much more elegant that the template method of mixing HTML & code.

Martin Wickett on November 9, 2009 4:10 AM

A long time ago I wrote an e-lisp function for Emacs to remove trailing whitespace and I call this when I save. Unfortunately sometimes I have to avoid calling this function when editing a file for checking back into version control.

(defun buffer-whitespace-remove-trailing ()
"Function for removing all trailing whitespace in document"
(interactive)
(save-excursion
(beginning-of-buffer)
(while (re-search-forward "[ ]+$" nil t)
(replace-match "" nil nil)
)
)
)

It doesn't handle tabs, and that's deliberate, because in some languages (like Make) tabs are significant.

I, too, would like to know the issue with the above code; all I see is that a line containing several spaces will be interspersed between blocks. Is this so? Did I miss something?

Anonymous on November 9, 2009 4:22 AM

I'm not alone in my freakness.

Josue Gomes on November 9, 2009 4:25 AM

One doesn't need to highlight whitespaces. It's enough to highlight end-of-line.

Daniel Sobral on November 9, 2009 4:30 AM

More than 30 comments, and nobody's mentioned markdown?

Only Joking on November 9, 2009 4:33 AM

I'm sorry, but if your code is affected by trailing whitespaces, that's a problem with the language. We have processors today that are millions of times faster than the big iron of old where processing every byte during a compile took time.

It is a complete waste of a programmer's time chasing down something as ridiculous as that. Terribly inefficient use of resources.

David on November 9, 2009 4:35 AM

It's funny. Lately at work i have been working a lot with Joomla and VirtueMark, they have all this extra white-space. We are talking whole lines with nothing but 20 spaces on them and it's not to keep things indented. drives me nuts...

Samuel on November 9, 2009 4:43 AM

Or pick it up when you diff it before commiting to source control. Picks up other invisible changes like tabs and BOMs also.

David Wilcock on November 9, 2009 4:43 AM

Or pick it up when you diff it before commiting to source control. Picks up other invisible changes like tabs and BOMs also.

David Wilcock on November 9, 2009 4:44 AM

This is where it is nice to have an IDE that automatically removes EOL whitespace.

TM on November 9, 2009 4:51 AM

You need a vacation. Period. Space.

Mr. Orange on November 9, 2009 4:53 AM

Wow, I was seriously beginning to think that I was the only one who obsessed about this.

David Seng on November 9, 2009 5:07 AM

Could someone explain to me why those extra spaces kill the JavaScript?

I mean, they wouldn't cause a problem in C, and that's got one of the worst syntaxes ever foisted on programmers - apparently.

C Programmer on November 9, 2009 5:07 AM

Could someone explain to me why those extra spaces kill the JavaScript?

I mean, they wouldn't cause a problem in C, and that's got one of the worst syntaxes ever foisted on programmers - apparently.

C Programmer on November 9, 2009 5:07 AM

And you really need to fix this double posting bug.

C Programmer on November 9, 2009 5:09 AM

Actually, that are two issues. First, trailing whitespace is just unclean. Aggressive removal causes spurious merge conflicts. (Nothing compared to windows svn users committing dos mode files, though.) Unfortunately eclipse does either sprinkle trailing whitespaces (ugly) or removes *all* of them, even those that were there previously, causing conflicts. git has the decency to only complain *new* trailing whitespace.

Whitespace in HTML/XML is a completeley different issue. The extra spaces in the example don't matter functionally (AFAICS), but make the resulting HTML look bad. On the other side, HTML/XML indenting is formally a bad idea anyway, exactly because whitespace is significant in XML. Only only wonder once why an XML parser returns lots of document data when the document is if the say-nothing (no apparent marked-up text) X'M'L type.

Andreas Krey on November 9, 2009 5:16 AM

Tabs are nice, but you are never sure are there some spaces or tabs, until you check. Also some tools handle indentation trimming differently than others, which is confusing.

Silvercode on November 9, 2009 5:23 AM

Or... just take a deep breath, a little yoga, and move on with your life. I know, it's not that easy. But readers, please don't mistake an obsession with whitespace with being a good coder (Jeff, I know you aren't making that argument).

I've been coding for years. Wrote a couple of books on the subject. And this has never, ever bothered me. I run the code cleanup plugin in the IDE occasionally and am absolutely fine.

Will on November 9, 2009 5:25 AM

I also dislike terminal whitespaces, but I didn't see the point of saying 'OH MY GOD!' I tried for a long time to see in which way those spaces would break the code. That would be the only thing that would cause such an outrage.

As I said I also dislike them, and I delete them whenever I get the opportunity, but with source control diffs that can ignore whitespace I don't see it like a big problem and have learned to live with it (especially in a bigger team where some are not that concerned about it).

Radu on November 9, 2009 5:28 AM

I think that caring about extra [trailing] whitespace is a little bit OCD-wise. But I'm guilty as charged :P

On the other hand, I find that a little bit contradictory: yes, it's wasted bandwitdth, but so is the one spent on tabbing tags. I suppose the *correct* OCD solution would be to strip *all* of the superfluous whitespace.

Boro on November 9, 2009 5:31 AM

I guess that's why my editor has a function to strip white space off the ends of lines. Ultraedit 32. But I don't have a key assignment for it. Not that there's much that I need to do with that, it's usually the data that I use that on.

Bryan Price on November 9, 2009 5:43 AM

GitX (http://gitx.frim.nl/) highlights end-of-line whitespace, so as long as I'm paying attention, I can catch it before it's committed.

Stephen on November 9, 2009 5:48 AM

At first I honestly did not see what was wrong, but after re-reading and learning of the mistake, I felt dirty, violated and wrong.

Off with the extra spaces.

Patrick on November 9, 2009 5:48 AM

Check out the built in macro in VS Samples->VSEditor->FixLineEnds - might just be what you are looking for!

Brian Schmitt on November 9, 2009 5:50 AM

What's the big deal? It might be a remnant of removed comment.
Completely harmless and irrelevant.

Azarien on November 9, 2009 5:50 AM

I'm just cheering that you indent with spaces rather than hard tabs. That way when I try to maintain your code, it looks consistent no matter which tools I use.

Chris Noe on November 9, 2009 5:51 AM

I couldn't agree more!
What you need in your settings is, replace tabs with spaces, and a tab spacing of 2 or 4 (I used to use 4, but with web pages that can push it too far out).
Tab spacing of 8 is very 1970's assembler!
Otherwise, the formatting depends on what tools you view the source in, which can seriously mess up the alignment.

Bruce Hatton on November 9, 2009 5:55 AM

I let emacs silently remove trailing whitespace on every save:

(add-hook 'before-save-hook 'delete-trailing-whitespace)

Yes, my diffs are occasionally larger than necessary, but that's not my fault, it's the guy before me who left all that garbage in the file.

And once I had a multi-line C macro in a header file with CRLF line endings. Compiling on Solaris, only the CR was escaped, leaving LF intact and breaking the macro. Took several pairs of eyes before we figured out why the macro was breaking the compilation in strange ways.

Martin on November 9, 2009 6:00 AM

Trailing whitespace breaks our product builds all the time because C compilers don't treat it portably in multiline #defines. Some compilers object to space after the continuation characters '\\', and others let it fly, allowing a build break on a different platform build. This topic causes a suprising amount of grief. Some people configure their editors to remove it, but since everybody doesn't do so, it can easily muck up version control systems that have merge capability.

Peeter Joot on November 9, 2009 6:02 AM

"Imagine how it is for those of us who spend a lot of time in Python world, where the whitespace isn't just an OCD obsession, but can actually modify the functionality of code!"

this is why whitespace is evil.
Think of all the wars that could have been avoided if python didn't require tabs!

Eric on November 9, 2009 6:06 AM

Weird how Jeff thinks he speaks for all programmers.

Joey on November 9, 2009 6:16 AM

I don't like to be filling my screen with even more characters, but one handy trick is to set the 'visible whitespace' character color to your background color.

Most of the time it's invisible, but select the text and it pops out.

ieb on November 9, 2009 6:25 AM

People, it is time for all of you to discover the Tab-key.

securityhorror on November 9, 2009 6:25 AM

"this is why whitespace is evil.
Think of all the wars that could have been avoided if python didn't require tabs!"


Python doesn't require tabs -- it requires tabs OR spaces. The number of spaces is irrelevant so long as it is consistent.

Python and whitespace is NOT evil because it demands you pay attention to levels of indentation (GOOD) thus the readability of the code, to humans and to the parser, is guaranteed. Debugging is therefore very easy.

Trailing space is irrelevant. Any programming paradigm that still pays attention to space or tabs after a statement (unless of course its quoted) is a ridiculous hack and should be consigned to the dustbin.

Craiggybear on November 9, 2009 6:26 AM

I know I'll get flamed for this, but VB5 and QuickBASIC both remove whitespace at the end of lines AND they also convert tabs to spaces.

Yes, I actually use both languages to do serious work.

Matej Horvat on November 9, 2009 6:33 AM

Ctrl+A, Ctrl+E+C in Visual Studio takes care of those pesky, non-conforming outsider characters along with other formatting gaucherie.

John Q. Public on November 9, 2009 6:33 AM

So this non-visible code that is wrong -- where are the unit tests that demonstrate the failure?

Hugh Brown on November 9, 2009 6:34 AM

To those that say that there's no problems with trailing whitespace because the language can handle it, there's an awful lot of other things that languages can handle just fine that we generally avoid doing. I see it as an issue of productivity, as the existence of trailing whitespace makes the End key pretty useless.

SL on November 9, 2009 6:34 AM

I am neither a C# programmer nor do I have experience with ASP (or ASP.NET). So can someone explain the issue here?

In my naive view, because the whitespace is not within ASP tags (), it will just be between the table start tag and the tr start tag, but this would not be a reason to say "Oh my god!".

GodsBoss on November 9, 2009 6:35 AM

Jeff,

Your whitespace is the least of your problems when it comes to that code:

1. you're missing the tag.
2. Why are you even marking up comments as tables at all? It's *not* tabular data.

I find it funny that you reacted to the whitespace but completely missed the above. I certainly wouldn't leave my markup in your hands :)

Andy on November 9, 2009 6:35 AM

Configure your IDE / editor to hightlight trailing spaces. Configure your diff tool or your version control system to highlight whitespace errors like trailing space.

Enable pre-commit hook that checks that you do not add trailing whitespace (and other important checks, like committing file with merge markers).

If you work cross-platform and end of line character matter (CRLF vs LF vs CR) configure your version control system to do the conversion automatically.

Captcha: stopgaps those

Jakub Narębski on November 9, 2009 6:36 AM

meh.

The td got stripped out.

"Your comments: (no HTML)" yet I have to write using HTML entities? C'mon, Jeff. You need to fix that.

Andy on November 9, 2009 6:39 AM

I always used to think coding with visible whitespaces was annoying, but then I changed their color so they're just barely visible.

Now I can see them all the time but they're not in my way! WIN-WIN!

weazl on November 9, 2009 6:40 AM

This entire post was about the extra whitespace at the end of the first line? Seriously?

Brad on November 9, 2009 6:44 AM

For those who are confused "trailing" means after the line (referring to the first line).

In case anyone wonders why that is a big deal. Imagine trying to add code to the end of the line. You hit the "end" key and your cursor flies off to netherland. Ok. Not too big a deal. Now imagine that you need to add something to the end of 300 lines that all have extra white space and you have 5 minutes.

Yeah. Now it's a big deal.

Practicality on November 9, 2009 6:51 AM

Source Insight can remove trailing whitespace.

Trailing spaces once cost me 3 days. It was a Windows 2003 sysprep.inf, configuring multiple network adapters. The INF looks basically like this:

name=Adapter1
[Adapter1.Settings]
setting1=value

You can imagine what trailing spaces after "name=Adapter1" would do...


Also recently, I copied some vbScript samples from the web, and got weird "compile" errors, which were probably caused by incorrect CRLFs in those samples. I don't know why they happened.

Jonathan on November 9, 2009 7:01 AM

If this really gets under your skin, it seems like occasionally highlighting a few lines in the IDE with a click and drag would show the trailing whitespace. If you don't like the spaces, delete them. "Problem" solved... Some things just don't need to be over-thought. Not every problem demands a software/tool-based "solution."

A repository can hold things other than code, too. You can't just assume that trailing spaces never matter. Someone put them there. Might be they had a reason.

Maybe I'm just not getting it. Big deal.

Big Deal on November 9, 2009 7:11 AM

Well, at least we saw code in this posting.

captcha: foot blastoff (keep that gun pointed away from my foot!)

AC on November 9, 2009 7:17 AM

I use vim and I use the following to detect leading/trailing whitespace chars.

" Borrowed from http://docs.google.com/View?docid=dfkkkxv5_65d5p3nk
if v:version >= 700
set list listchars=eol:\ ,tab:>-,trail:.,extends:>,nbsp:_
else
set list listchars=eol:\ ,tab:>-,trail:.,extends:>
endif

This shows '[tab]foo bar ' as --->foo bar.. (with specific coloring to differentiate from real chars)

Justin on November 9, 2009 7:17 AM

There are a few comments here that ask something along the lines of "why is this wrong? The compiler/interpreter/browser can still handle it, it doesn't break any tests."

We don't write our programs for machines, we write them for the next programmer.

Paul on November 9, 2009 7:18 AM

While not directly related to whitespace issues, I think what Martin Wickett made a pretty good point above. It feels dirty to me to have two different languages (C# and HTML) "weaved together".

I know that's normal for ASP.NET MVC, but is there a better way?

Would html helper methods or user controls just move the problem to a different file without improving anything?

This seems far LESS readable to me:

...

public List CommentRows(object Model)
{
List Rows = new List();
foreach (var json in Model) {
HtmlTableRow r = new HtmlTableRow();
r.id = "comment-" + json.id;
r.class = "comment";
r.Cells.Add(VotingImages(json));
r.Cells.Add(CommentText(json));
Rows.Add(r);
}
return Rows;
}

Zack on November 9, 2009 7:21 AM

Right, don't forget to set your editor to strip all trailing white spaces. Matter closed. Now, can we stop congratulating ourselves for our freakishness? This thread is really starting to freak me out.

Parenthesis on November 9, 2009 7:21 AM

While not directly related to whitespace issues, I think what Martin Wickett made a pretty good point above. It feels dirty to me to have two different languages (C# and HTML) "weaved together".

I know that's normal for ASP.NET MVC, but is there a better way?

Would html helper methods or user controls just move the problem to a different file without improving anything?

This seems far LESS readable to me:

%= Html.Table(CommentRows(Model)) %%gt;

...

public List CommentRows(object Model)
{
List Rows = new List();
foreach (var json in Model) {
HtmlTableRow r = new HtmlTableRow();
r.id = "comment-" + json.id;
r.class = "comment";
r.Cells.Add(VotingImages(json));
r.Cells.Add(CommentText(json));
Rows.Add(r);
}
return Rows;
}

Zack on November 9, 2009 7:23 AM

Aww come on, any UNIX user is going to tell you that spaces are more portable!

I was going to make a bumper sticker that said "Every time you use spaces instead of tabs to indent your code you kill the earth a little more because your compiler's parser has to work harder" , but could not find a way to shorten it.

"Indent with tabs ... or this kitten gets it!" seemed more practical, but would offend too many people.

I may settle on a T shirt that says "hg revert ..."

Tim on November 9, 2009 7:25 AM

While not directly related to whitespace issues, I think what Martin Wickett made a pretty good point above. It feels dirty to me to have two different languages (C# and HTML) "weaved together".

I know that's normal for ASP.NET MVC, but is there a better way?

Would html helper methods or user controls just move the problem to a different file without improving anything?

This seems far LESS readable to me:

%= Html.Table(CommentRows(Model)) %gt;

...

public List CommentRows(object Model)
{
List Rows = new ListHtmlTableRow%gt;();
foreach (var json in Model) {
HtmlTableRow r = new HtmlTableRow();
r.id = "comment-" + json.id;
r.class = "comment";
r.Cells.Add(VotingImages(json));
r.Cells.Add(CommentText(json));
Rows.Add(r);
}
return Rows;
}

Zack on November 9, 2009 7:25 AM

Seriously, most of you just need to chill. Get yourself a merge tool that can ignore white space differences and forget about this non-issue. There are many, many things more important to your productivity and your company's bottom line.

And yes, I am a programmer

John on November 9, 2009 7:26 AM

sorry about the multipost. I'm giving up on trying to get the formatting right.

Zack on November 9, 2009 7:26 AM

More comments»

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Content (c) 2009 Jeff Atwood. Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved.