Whitespace: The Silent Killer

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

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.

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!”.

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 :slight_smile:

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

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.

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!

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

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.

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.

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.

Well, at least we saw code in this posting.

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

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)

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.

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®;
}
return Rows;
}

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.

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®;
}
return Rows;
}

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 …”

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®;
}
return Rows;
}

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