« Scoble and Randy Holloway on marketing | Main | Buying and driving a car in New Zealand »
December 09, 2004
Spaces are evil, ish
Craig Andera says that Tabs Are Evil and wants developers to indent with spaces instead. Now Craig is the last person I'd want to stoke a holy war with, but spaces are even more evil.
If you use, say, 4 spaces for indentation, then how do I skip back to the next higher indent level? I press cursor-back four times. Maybe that doesn't sound too painful by itself, but now suppose I'm at the beginning of a line that's four times indented (not unusual in C#: namespace, class, method, loop, that's four). Cursor-forward sixteen times? Or the IDE has given me a blank (empty) line at the same four-indent depth and I need to get to the right indent level. Type sixteen spaces? Thanks, but no. My fingers can count four tabs. They can't count sixteen spaces or cursors.
Yes, consistently applied, spaces give you 100% predictable indentation. But, consistently applied, so do tabs. Where spaces are useful is for lining up text across lines where the lines are at the same level of indentation, for example where you want to split a parameter list across lines, and line up the parameters against each other and the end of the method name.
The problem comes when coders don't understand the difference between indentation and lining-up. Indentation is used to express logical structure. Lining-up is (usually but not always) for working around the finite width of the monitor, and is appropriate only within an indent level. (The parameter list of a method would never span levels of logical structure.) If you use spaces to indent, you inconvenience yourself and others. If you use tabs to line up, it all goes bang when the code is moved to Craig's machine.
The correct guideline is therefore: Use tabs for indentation, and spaces to line things up.
December 9, 2004 in Software | Permalink
TrackBack
TrackBack URL for this entry:
https://www.typepad.com/services/trackback/6a00d8341c5c9b53ef00d834718e4669e2
Listed below are links to weblogs that reference Spaces are evil, ish:
Comments
> Cursor-forward sixteen times?
No, tab four times. But since it's inserting spaces, you're all set - no worse than using tabs.
> Yes, consistently applied, spaces give you 100% predictable indentation. But,
> consistently applied, so do tabs.
The problem is that people don't have the discipline to use things consistently. They do have the discipline to check an editor setting (mostly).
> If you use, say, 4 spaces for indentation, then how do I skip back to the next higher
> indent level? I press cursor-back four times. Maybe that doesn't sound too painful by
> itself, but now suppose I'm at the beginning of a line that's four times indented (not
> unusual in C#: namespace, class, method, loop, that's four).
How about a macro (say, control-shift-backspace) that is equivalent to backspace four times? Does that address that concern?
Maybe if tabs were visually distinct from spaces (scintilla has this ability), then consistent application would be a realistic goal.
But hey, if you use tabs *consistently*, that makes me happy enough. It's the mixers who should be lined up and tickled. ;)
Posted by: Craig at Dec 10, 2004 2:21:11 AM
"Maybe if tabs were visually distinct from spaces (scintilla has this ability), then consistent application would be a realistic goal."
I guess this might help, though personally I find "visible" representations of whitespace irritating and would turn them off under usual circumstances. Subjective, of course: some people love them and even turn them on "show whitespace" in word processors ***shudder***.
I think the problem is as much in the mind as in the tools: many coders tend to think of code layout as a purely visual thing. So when they see "bugs" in the layout, they fix them so they look right rather than being structurally right.
Then again, as many people have pointed out in the past, what we really want is a tool that groks (and versions) code as an abstract syntax tree and presents it to each individual dev for reading or editing in their personal preferred layout style. This whole issue, along with bracing style and how many spaces after a full stop, should have gone the way of the typewriter by now.
Posted by: Ivan Towlson at Dec 10, 2004 3:37:20 AM
I occasionally wish for the ability to insert tables or grids, a-la Word, into source code comments. There is certainly a lot of potential for real breakthroughs in the area of code and comment presentation - I just wish the breakthroughs would hurry up and get here!
Posted by: Bruce at Dec 10, 2004 9:41:24 AM