Death to the Space Infidels!

Tabs. I’m going to try the Tabs + Spaces example.

Haven’t you folks heard of the old fashioned pretty printer? Google it…or code beautifier, which seems to be the new fangled term for it.

Once you have a pretty printer that works for the language you’re using, just configure it and make it part of your source get process. Stop wasting time arguing about tabs vs. spaces and start using your time productively…arguing about variable names or method names.

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. I can still arrow through code, and it jumps 5 spaces at a time, if you were to watch, you would swear I had tabs on, but in the end, they are spaces.

This allows formatting that does not shift around, which I like. Some SQL statements can get ugly, and I like them to line up for readability, spaces are the only way I know to do that.

Does anyone know in TextMate how to go from one cuddle format to the other? I like the first, but often times I get code in the second. These are my personal projects, and I am swiping examples off the web, and want the brackets on one line.

int main() {
return 0;
}

versus

int main()
{
return 0;
}

(Almost) everyone is talking like it’s the 80s. Computers aren’t scarce resources and one of the key philosophies now is NEVER DO SOMETHING THE COMPUTER CAN DO.

Don’t use a code formatter because it lets you keep coding styles consistent among the team.

Use the code formatter because it’s one less thing that you, the very expensive hairless monkey sitting at the keyboard, has to do. At least do it at the end of each ‘sprint’, along with running static code analyzers (e.g., ‘findbugs’) and the like.

BTW I’m old school Unix, although not quite as old school as a former coworker who still used ‘ed’. But today I use proportional fonts (verdana) instead of courier. It may be due to language since Java has far longer method names than C’s function names.

In Eclipse,

ctrl+alt+space (tabs - spaces)

or

ctrl+alt+tab (spaces - tabs)

Walla!

Since everyone just has to weigh in on this…

There are several issues that have been raised in the comments here that I feel like addressing.

One is the amount to indent, which isn’t really about tabs vs. spaces, except people have said, more-or-less, if you use tabs you can see it whichever way you want.
Good old KR C promoted indenting by 1 tab, where a tab is equivalent to (up to) 8 spaces. That’s too wide! you say, What if I need to print it? you say. The argument was that if it was too wide to print – that is, indented too far – then your code needed restructuring anyway. You were doing too much in-line to end up with an indent that wide and you should refactor some of that into functions.
Now indents of 4 are so common now that I stick with that myself, but I’d still apply the argument to people who say they need an indent of 2. If things are getting too wide with an indent of 4 so you need to use 2 then something is more fundamentally wrong with your code than the way you use whitespace.

This easily leads to the tabs vs. spaces debate; again people have said you can see it how you personally prefer it if you use tabs – see it as 2 or 4 or 13 if you like. Except – I use multiple tools on a source code file; the IDE, sometimes a plain editor, VIM, other non-editing tools. Somewhere along the way, like, maybe =printing=, WILL treat your tabs as 8 space tabstops, and now your code is indented deeper than you thought. Also, somewhere along the way, someone will have indented or added extra indentation using spaces (as Harvey pointed out). Those don’t line up anymore because they assume a tab stop of X (where X 8)

My take is – it doesn’t matter what you want tabs to be, they are 8.
Even if you make your whole system consistent so that tabs always cause an indent of 4 (or 2 or 13), some fool will open your file on their system and all bets will be off. The only 100% safe way is to use spaces

BTW, what some people were wishing for – have the editor just present it the way you like it, even maybe including variable and function names – is called Source Code In Database (SCID) and there have been / are systems that do it.

So it comes down to, for me

  • indent levels are 4
  • tabs are 8
  • pressing the tab key gives you 1 indent level
  • which is 4 spaces (never tabs)

No extra keystrokes just because I’m using spaces. Shift Indent Level left or right commands are in the IDE/editor so no crying about I have to delete N spaces instead of just one tab
Indenting is always the same no matter who’s looking at it on what machine using which tool.

That’s what I do, with the reasoning behind it.

Stephen P.

(and Adam V – not picking or anything, but do you also trim trailing whitespace and use only Unix style LF, not CRLF pairs, for line endings, all to save disk space? I personally loathe the way IDEs leave trailing whitespace on lines and leave lines that include whitespace-only because you hit enter-enter to get a blank line. Doesn’t matter much for code or plentiful disk space but when you end up with HTML that’s 30% bigger due to that crud it does affect your bandwidth)

spaces tabs

Mainly, why press a button 4 times when you can press it once?

Tab key, but use an editor that converts tabs to spaces.

Nobody I’ve met in person disagrees with this logic.

Anyone who uses 8-character-wide tabs is a madman and anyone who uses 2-character-wide indenting is a lunatic.

Using 4-character-wide indentation means that you are alright and don’t need to be beaten about the head.

As an ASCII conservationist, clearly the only ecologically sound solution is to use tabs. The third-option arguments about alignment are suprious distraction - such superflous alignment is utterly unnecessary and wasteful.

P.S. Congratulations on generating 5000 comments with a tabs vs spaces blog post. If only people cared as much about serious things…like CamelCase vs bsHungarian!

@TM
Anyone who uses 8-character-wide tabs is a madman and anyone who uses 2-character-wide indenting is a lunatic.

Thank you for sharing your empathic and well reasoned thoughts. It must be very nice to live in a world where your personal preference always is just perfect.

I always set my editors to view space/tab characters, so it does get on my nerves at times, but I’ve learned to keep my mouth shut and wait until project standards converge.

Another time first :smiley:
The third option will be interesting, as it enables me to see the break line clearer, sometimes it’s confusing.

And then there is the editors that simply convert a tab keystroke into 4 spaces. Voila, argument avoided.

editors that simply convert a tab keystroke into 4 spaces. Voila, argument avoided.

Yeah, but I like 2 spaces. 4 is just stupidly wide. So I’ll reformat and check in… ok? :slight_smile:

I get furious when I see 4 spaces used instead of a tab. But I’m not sure why. It’s not something to get emotional over, but I do.

That is – reformat it, normally without changing what the code does, just changing the variable names, function names, but mainly moving things around to the way they like it.

This guy seems to be complaining about a lot less trivial changes than tabs, spaces and the location of curly brackets. He’s complaining about someone cleaning his code, which if done properly, leads to much more readable code. After reading Clean Code recently, I’ve taken to following the Boy Scout Rule and look for any bit of code in need of cleaning in any source file I happen to have otherwise checked out.

As the person doing the cleaning has the authority to do so and is presumably senior to the complainer, the complainer should take the opportunity to learn if his code can be improved.

I agree with using the spaces instead of tabs otherwise you get wild variations with the tabs depending on code editor. That said, I still use tabs instead of spaces. Can’t drop the habit. Worse… I put the first curly bracket after the method name on the same line. Geez, I better quit while I can!

A fourth option may be: Let everyone format things however they choose to format them, but insist that they run a code-beautifier on the code before submitting it. I believe that this would yield some good results…would it not?

I personally prefer tabs. If I want some whitespace, I’d rather hit one key rather than the mashing space a few times. I also like the spacing of tabs. I don’t find it to be too wide or too narrow. I think a single or double space is not enough though. It makes it harder to distinguish formatting with larger chunks of code.

Idon’tlikeusingwhitespace.It’sawasteofcomputermemory.