« November 2004 | Main | January 2005 »

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 | Comments (3) | TrackBack