Here’s some thoughts on an aspect of coding that can be frustrating and annoying all out of proportion to its importance: tabs vs. spaces.
It’s one of those issues that coders often don’t think about much any more until you’re trying to work with code from a project that either didn’t share your philosophy or has already had a lot of code in conflicting styles added. IDEs can often aggravate this by being so darn powerful and convenient in how they auto-format code, it’s often not thought about until late in the project when mixed styles have already started creeping in.
The solution, of course, is project-wide code style conventions from the very beginning. But which convention to use? In an ideal world, the solution would be tabs for indentation, spaces for alignment. Tabs for indentation allows for every developer to set their own indent level – no more arguments about how two spaces per level looks cramped, or four spaces per level wastes space. Spaces for alignment allows code to be attractively formatted independently of tab size. It’s the best solution by far, and yet it rarely works out in the long term. There are some reasons for this:
- Too few developers think about code formatting at all, much less the details of code beautification.
- The tabs for indentation and spaces for alignment convention isn’t perfectly supported by all automatic formatting tools. Because not nearly enough attention is paid to formatting in the first place, an automatic formatting convention is the best hope for many projects to keep code uniformly readable.
- On long term projects, developer churn should be assumed. Sooner or later (hopefully later, but you never know), there will be coders who don’t meet your exacting standards for software development skills. If a project is really a survivor, the odds are good that there will be people with commit access that have very little development access. When the XML config file or that one piece of scripting is being edited in Notepad, you can pretty much forget consistency.
So, back to the root dichotomy: tabs or spaces? To which I say: spaces only and forever. The reasons why are all the reasons listed above; spaces require no thought or planning on the part of the developer, “convert everything to spaces” is an easy rule to implement in all IDE formatters (and Ctl-Shift-O before check-in is as easy a rule as it gets), and spaces are hard for even a monkey with Notepad and commit access to seriously screw up. If it were me, writing code that would never be seen by anybody else, I’d choose tabs for indentation and spaces for alignment, but in a business environment that’s not something that should ever happen – you write the code, and format it, for the client and the developers who are to come after you, not for yourself.