Whitespace: The Silent Killer

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

The problem, at least as I see it, is that it is an awfully bad hack to start sticking lines of code into markers. The whole thing is so utterly cryptic that it’s a wonder you can type it in at all. It’s an example of bad technology (from poor syntax), not of problems with whitespace. The whitespace issues are secondary (the symptom of the disease).

If the technology sucks, one sure sign is that little changes can cause mystifying effects. That type of embedded instability makes it hard to deploy consistently, which makes it very stressful to use. If it wasn’t for the Internet (and easy access to answers) we’d no doubt avoid these types of crappy technologies, and they’d die natural (and much deserved) deaths.

We should really start a “just say no to super-fugly technologies” campaign. They seem to linger far too long these days.

Paul.

Seeing as how nobody has attempted to bring the joy of Spark into this thread I will have a go.

There are view engines for MVC that suck less, handing the integration of code and markup much more cleanly than the multiline beesting nastyness of the default view engine.

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.

Invisible trailing whitespace often indicates randomly indented code too.

Both of these things (cut-n-paste shortcutting know-nothing quick fixes, and semi-random indentation) cause different problems, but have some of the same “leading indicators”, including trailing whitespace.

Throw this in your .vimrc, problem solved:

autocmd BufWritePre *.cs :%s/\s+$//e
autocmd BufWritePre *.aspx :%s/\s+$//e

I’m afraid I’d have to argue against any extra whitespace (trailing or leading) when dealing with HTML generating code.

I know that bandwidth isn’t a major concern for many people, but when generating the HTML in a loop, please remember that every bit of extraneous whitespace is an extra byte (or two) of data that must be transmitted that has no purpose to the end-user (it just makes the generated HTML easier to read).

The example above would generate 24 bytes of “end-user useless” whitespace per iteration of the loop. If the loop executed 10 times per page view, that’s 240 bytes. 5,000 page views x 240 bytes = 1,200,000 bytes (1.14 MB)

I would at least consider having a process that would remove the leading whitespace in production code, or alternately, a process that would add the whitespace to the source dump of code for testing.

I fail to see the problem in the snippet you provided. Unless it is important in the look of the generated HTML.

Well, a long time ago, I have set the option in my text editor, SciTE, to strip trailing whitespace, and I happy with this setting. That’s an alternative to your options (ie. a built-in feature of the editor).
And I don’t have the problem of Barry Kelly: it is smart enough to keep the indentation of the last line with indentation, and to keep the caret position when running over empty lines.

In Eclipse, I use AnyEdit Tools to take care of these extra spaces.

Funny, my first thought was “what’s wrong?”… and then as I read on, I realized I’d subconsciously assumed that, had I gotten my hands on that code, I would’ve instinctively deleted those spaces. That stuff drives me nuts too.

There are html optimizers for web servers that can be employed on the job (although general value additions is quite negligible). Also I found following CodeProject article trying to do similar stuff.

http://www.codeproject.com/Articles/38067/Compress-Response-And-HTML-WhiteSpace-Remover.aspx

Oh. My. GOD! You care more about meaningless whitespace than using semantically correct markup. What a noob!

Haha, so true!

If white-space causes such issues and this many comments, the programming world indeed has gone backwards…

Check out Perl::Tidy, and to a greater OCD extent, Perl::Critic. It turns out your language can AUTO-FORMAT ITSELF ANY WAY YOU WANT IT TO if it were a little bit smarter. You want cuddled elses but your colleagues don’t? No problem, just Tidy it to the group’s standard when you commit (which Eclipse will do for you) and Tidy it back on update. Works for all whitespace and all punctuation, plus comments, array formatting, etc.

Oh, and if you really want to see some whitespace coding horror, check out the Acme::Bleach novelty module on CPAN. It replaces your source code with a series of non-printable whitepaces which, when interpreted, produce the same run-time as the original code.

Only downside to that /\s+$/ search:
What about languages that let you specify blocks of text? That whitespace may be useful for display-formatting purposes. Suddenly that simple regex gets a lot nastier (if it can even be done in regex).

Granted, that likely means you’re doing something wrong, but sometimes adding a space or two to align things is the easiest fix.

Otherwise, I completely agree, and have done similar things for a while. But I must admit, my first shock-reaction was “OMG spaces not tabs!” Spaces are a royal pain, as they lock-in the layout of your code. Use tabs, and IDEs can display them however you like, and usually auto-align things correctly, where spaces explicitly break auto-formatting behavior.
Yes, some languages freak at tabs. Clearly they’re exceptions.
Maybe, some day, we’ll get elastic tabstops in regular editors. They rather nicely eliminate repetitive tabs / spaces.

and that from a freaking coder who thinks two whitespaces is good for users to use as markup language

Okay, I’m now forcing my VS to indent code with 5 whitespaces instead of 3

I leave whitespace visible but set the color to a very low contrast with the back-ground–e.g., light, light gray against white. I can see it quickly by squinting but it doesn’t get in the way when I’m concentrating on the code.

If only I could set whitespace color in Eclipse :stuck_out_tongue: .

Did we all get in to this industry because we are ‘control freaks?’

Really? I mean … really!!!

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

I humbly submit that you’re not one of us either…