Whitespace: The Silent Killer

Gool old Notepad2:
Auto strip trailing blanks option (File, Line Endings, Default)

zzz… I thought this post was going somewhere - but then it didn’t. I was wondering ā€˜what am i missing’ and when the big reveal was some whitespace I died a little.

@lolatwood - funny :slight_smile:

KDevelop has a setting to strip all trailing whitespace on saving. However this won’t be visible in the IDE until you close and reopen the file. I like the idea of a code cleaning bot that runs via commit hook/cronjob and tidies up with simple rules like trailing whitespace, extra line break at the end of each text file, tabs to spaces etc.

Srsly kdiff3 can ignore whitespace. I’m sure any decent diff/merge tool can. Who cares?

To Nick: any difftool can ignore whitespaces I guess too, unfortunately ignoring whitespaces that aren’t at the end of lines can hide important changes and thus should perhaps not be done.

I might be wrong, but isn’t \s*?$ a non-greedy match to any number of trailing whitespace? Won’t this always match exactly 0 characters of whitespace? Shouldn’t it be: \s*$

What whitespace at the end of the lines?

signed: the VB guy

(I know, I know… in the example above, also for VB guys the whitespace would not be trimmed)

I submit that an important part of being a nerd is knowing which things to obsess over and which things to let go. To paraphrase Dave Chappelle, pick your spots.

If you’re using a language in which whitespace is not significant, I recommend choosing something else to obsess over.

It is nice to know I’m not alone in my Code Whitespace OCD.

Locking? Surely you are joking, mr PRMan. I thought no one used locking version control since about the time fire was discovered…

Ctrl+E,D

Guess I’m not one of you … Thank goodness too 'cause otherwise I’d have to have myself committed for being overly anal.

Other than the fact that whitespace, like case (…yes I said CASE…why should GROSSPAY, grosspay, GrossPay, or grossPay mean different things or be scoped differently? Why? WHY!? Ok…I got off on a bit of a tangent. Sorry.) shouldn’t matter, it can cause problems.

I remember beating my head for two days over a piece of code that refused to compile. I’d stare at the line and tweak it one way or the other to no avail. Out of desperation I deleted the entire line and retyped it from scratch. It worked. Comparing the old and new code, I found ONE SPACE at the end of the line that didn’t work.

It’s hard to debug something you can’t see.

May be worth saying too that most companies (and here I’m thinking those situation where there’s more than one person committing to the repository) enforce the removal of trailing whitespaces on their code guidelines documentation.

This is also, as far as I know, a common guideline among open source projects.

I’m surprised however as to why Jeff ignored the setting on most editors used for coding (modern and old alike) that automatically remove these on save. This setting alone removes the need for his three proposed solutions.

But assuming such an editor feature wasn’t present or couldn’t be present for some reason, it’s worth saying that most diff tools can also ignore trailing spaces at our request.

I’ve recently switched to tab formatting in designer HTML in Visual Studio, and the reason is simply payload size of the page. Using Jeff’s example, the the 48 space characters (not including the offending trailing white space on Line 2) could be replaced with 6 tab characters (assuming the Visual Studio default formatting setting of 1 tab == 4 spaces) and still retain the same visual layout. That amounts to a 12.5% reduction in size just on formatting. How’s that for OCD?

I’d like to minify all my .as?x files, like I’m doing with all CSS and JavaScript files, as part of a build step, but haven’t built/found a tool for that yet.

What I find most shocking about this is that anyone gives a sh*t. Nothing better to do, I guess.

Just thought my captcha was amusing:

heinie but

Sounds to me like you are complaining about the symptoms, not the problem.

Here are the problems as I see them:

  1. You are using a language with ASP-style syntax. Didn’t you get the memo that those suck? They eventually trend toward unmaintainable.

  2. You are using a merging repository with 10 developers. You aren’t writing the freaking Linux kernel!!! You don’t need merging, it just causes all kinds of problems. Tried and true locking is the best solution when you can easily divide the work between modules. I love locking, because then I can just set Visual Studio to conform the code to the way I like, and the next guy the same. Ctrl-K Ctrl-D and it’s all good.

  3. Was your diff tool written in 1990? Because WinMerge has been ignoring whitespace, blank lines and CR vs CRLF for at least that long. Seriously, there are diff tools that can’t do this in 2009? I would have never guessed.

  4. You are using an IDE which doesn’t do code cleanup for you. Set your style and have the IDE conform the code to it for you. Nice and consistent.

  5. Somebody checked in a bunch of spaces at the end of a line.

You left out one option regarding what to do to solve the whitespace problem. When I code, I use the Home and End keys ridiculously often. If you’re like me, it’s quite simple to see when you have some trailing spaces - chances are you’ll see it by chance when you’re just navigating the code! A lot of IDE’s have customized it by default to make the Home and End keys ignore whitespace - so Home will take you to the start of code -following- the indentation, and End takes you as far as the start of trailing spaces. Usually double-tapping Home or End will take you to the -real- start or end, though, so I’ve become rather accustomed to that as well.

Even at the start/end of scriptlets, we are inserting a new lines which are unnecessary. That costs us few bytes.