Death to the Space Infidels!

The rational way to format, I think, is to use tabs. Then people who like 2-space indents can set tabs to 2 spaces, I can set them to 4 spaces, etc.

The problem is that every stinking editor out there has its own way of handling tabs and its own commands for setting them up, as Jeff pointed out. Invariably someone (possibly me) will mess up the formatting without really trying. So now I generally use spaces (4 for most indents). Still, it’s kind of a shame.

All right, I’m a dreadful code-cleaner. I’m constantly being confronted with hideously complex 100-line procedures, and I have to rewrite them just to understand the stupid things. My mind doesn’t work in spaghetti logic.

In my defense, I usually get these projects after the original culprit is long gone (possibly fired). And I leave behind clean, comprehensible, documented code. But I’ve learned to force myself to ask is this really worth it?

@Goran: It’s still an issue partly because a lot of shops don’t use modern IDEs. The job I just left, a lot of code was written with vi. An old version of vi. Also if different people use different IDEs, things can get messy.

I prefer tabs, with a width of 4. I don’t like a tab being replaced with spaces either, because when I accidentally tab once too many, I have to hit backspace 4 times to clear the spaces.

As for braces, I prefer

void main()
{
return 0;
}

It’s all academic anyway really, whenever I get someone elses code and I don’t like how it’s formatted, I simply hit CTRL-A, CTRL-K, CTRL-F to have visual studio reformat the code according to my preferences.

The universal assumption in all the discussion about tabs vs spaces seems to be that your text is always viewed in an editor where you can configure your tab width preference. But what about command line output, like more, grep, etc. This happens to be a predominant use case for me on any project, and tabs cause all kinds of chaos for that. Long live spaces!

I would just like to point out the contradiction between the name of this post and the comments at the end. Is it that you can’t make up your mind yourself or just got so confused in what you were writing that you couldn’t pick a side?

I’d like to be all nice and liberal and say that it doesn’t matter. However, I work on a team where everybody uses a different editor (either the one they worked on before they joined up, or the one their assigned buddy taught them). The fact is that not all editors support the basic indentation rules, be they tabs of 4 or 8 spaces, or just plain spaces. Some people customize their tabstops, others don’t.
I set my editor to indent with spaces, which makes my code easy for anybody else to read, but when I read other peoples’ code the indentation is impossible to read because they use different tabs to get the indentation right on their editor. Sometimes I just break down and reformat the whole file because otherwise it sucks too much.

To those who don’t want to hit the space bar many times, with any decent editor (vim in my case) should indent your code for you as you type, so there’s no need to hit the tab key or press the space bar 4 or 8 times per indent level. That’s a non-issue .

Wow, lot of comments, as expected on such topic (that, and placement of braces…).

About the third option: there was (I hope the past is right here!) a choice of using tabs to indent by multiples of 8 positions, then use 4 spaces to fine adjust at the end. The worst of two world, IMHO.

There is also people forgetting to adjust properly their settings, so you end up with lines indented with spaces and others with tabs.
Personally I use tabs in my personal projects, and whatever the current convention is on the projects I work on.

I prefer tabs because they are compact, flexible (as LKM above points out), because I use mostly editors allowing me to adjust the width of tab (so that’s a non-issue), because some dumb editors force me to hit backspace or left arrow n times to un-indent or go back of one level. Of course, my editor is smart enough to do what I tell it, to insert (or remove) tabs (of right size) or correct amount of spaces when I indent or un-indent.

I admit when I paste code in a Web page, I replace my tabs with two spaces (small amount to avoid horizontal scrolling, awful in a Web page, particularly with lot of lines) but it is quick and painless, without risk of mistake, the reverse is harder! And output on terminal is terrible too.

About savage code formatting: I admit I guilty. But that’s always to make code conforming to in-house rules, to fix above mentioned mix of tabs and spaces (or bad indenting because of dumb auto-merge), follow our rules of naming (or fix typos / bad English sometime), etc.
As you point out, when a portion of code is unexpectedly formatted, it is a distraction: we see more the clunky formatting than the intended logic!

@Sleazey: BeyondCompare (shareware, Windows) lets you create sets of rules that specify what changes are important (marked red), less important (yellow) and non-important (not shown). You can then set it to use that particular set of rules to apply to certain file extensions (i.e. whitespace is important in my templates, but not in my code)

Jeff, you’re an absolute noob, you know that? Yeah, let’s all stop using tabs for indentation, because there are broken editors which support it poorly. Dumbass.

Then again, what to expect of people who write stuff like:

RidiculouslyDescriptiveClassName foo = new RidiculouslyDescriptiveClassName;

…and like it?

holy crap, you mean to tell me the tab key does more than change the input focus on forms? wow! people use this key when writing code? i’ve never heard of such a crazy thing.

We all have these superior compile-while-you-edit (i.e. intellisense) style IDEs and editors in languages like Java and C#.

So why are we still storing code in text form? Why can’t we compile the damn thing into a code DOM and store the DOM tree as-is??

I means… we don’t have bold or italics or embedded excel table in code files, right? And then we can have all variable renames and extract method actions and whatnot as something recordable in source control.

And when we have that… naming consistency becomes easier to enforce/check.

I know it’s not gonna be that easy… but we have had the ability to compile into byte code or an intermediate form in lightning speed (think classic VB) since… years?

This reminded me of the Real Programmers comic for some reason:
http://xkcd.com/378/

This reminds me of the never ending debate over curly braces {}…

I’m one of those:
Curly braces should be on a separate line as God intended…
type of programmers.

Tabs well, they are easier to deal with than spaces…

  1. In Visual Studio, this is a complete non-issue. Wherever I work, if the style of the code is bugging me then I just hit Ctrl-K Ctrl-D. Done! Both the tabbing/spacing and the braces are all lined up the way I like. If someone else likes a different style, they can just do the same to get it their way.

  2. What’s wrong with a proportional font? Code (and everything) is so much easier to read that way. Plus, more fits on the screen horizontally. And because of #1, it’s completely irrelevant anyway. I mean, if you are still lining stuff up for dot matrix reports or something, then that’s one thing, but in a modern Windows environment printing on laser printers why would you continue to use hard-to-read Courier?

Not only an interesting post, but also a great title for a 1950s sci-fi short story!

Unfortunately anyone using spaces to format their code self-evidently has an important part of their brain missing so I can’t read this blog any more…

Vim…freaks!

LOL

Ya know, it’s amazing the source control programs are not smart enough to detect formatting when comparing files…

G-Man

I actually don’t understand the reason for using spaces.

Because many programmers are not smart enough to reconfigure their editors! Really.

I use Textmate, and have it set to 5 spaces per tab. But I feel as though I am in tab mode, it just lays down spaces.

Yes, but one of your coworkers sets it to 2 and another sets it to 3, etc, and you end up with a scrambled mess. Real tabs would give you exactly the same thing. And, tabs would let each of your coworkers do whatever they want.

5 spaces is actually pretty goofy. And gives a good reason to use real tabs!

======

No one cares (anymore) that tabs save space.

======

You can avoid religious wars by mandating running a pretty-printer (code formatter) on all code.

This reminds me of the never ending debate over curly braces {}…

The KR brace-at-line-end formatting was done to save -paper- when publishing books. It wasn’t necessarily intended as a formatting-commandment. I find it hard to scan code for blocks with the braces at the end of lines.

If spaces are the same as tabs, why not just use tabs?

The real effect of using spaces is to -force- people do deal with one’s own goofy preferences for no reason. It’s pure pettiness!

Tabs let everybody use their own preferences without stepping on other people’s preferences.

‘Anyone who uses 8-character-wide tabs is a madman’

Not if you program in 68030 assembly language on the Amiga (which is one of my hobbies). 68000 family instructions vary in length, and some are seven characters long, so eight character wide tabs are needed. It’s a bit much for higher level languages, of course.

As has been said before, use editors which handle the whole tabs/spaces issue for you, and concentrate on writing code rather than formatting.