Spaces or tabs in your code

Linode Staff

For those coders/developers out there: spaces or tabs?

-Chris

26 Replies

Definitely spaces. It gives you more flexibility to line your code up, without having to mix tabs and spaces.

I was a 4-space softtab (my ~/.vimrc had sts=4) until I read the coding style guidelines from some guy named Linus Torvalds:

> Chapter 1: Indentation

Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.

Rationale: The whole idea behind indentation is to clearly define where a block of control starts and ends. Especially when you've been looking at your screen for 20 straight hours, you'll find it a lot easier to see how the indentation works if you have large indentations.

Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

In short, 8-char indents make things easier to read, and have the added benefit of warning you when you're nesting your functions too deep. Heed that warning.

Read the rest of the very short document at http://pantransit.reptiles.org/prog/CodingStyle.html

Cliff

Same here, I use vim with the space option, more flexible and from what I know, using tabs may be a problem in some languages.

@c1i77:

I was a 4-space softtab (my ~/.vimrc had sts=4) until I read the coding style guidelines from some guy named Linus Torvalds:

> Chapter 1: Indentation

Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.

Rationale: The whole idea behind indentation is to clearly define where a block of control starts and ends. Especially when you've been looking at your screen for 20 straight hours, you'll find it a lot easier to see how the indentation works if you have large indentations.

Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

In short, 8-char indents make things easier to read, and have the added benefit of warning you when you're nesting your functions too deep. Heed that warning.

Read the rest of the very short document at http://pantransit.reptiles.org/prog/CodingStyle.html

Cliff

Definitely spaces. Makes the code look the same in every editor you edit with and you don't have to mess with setting tab stops first.

I have much respect for Linus Torvalds, but his guidelines are for use in the Linux kernel, and using C as the language.

Also, the editors I use all have indentation highlighting (dotted lines, or whatever) to enable very quick and easy distinction of code segments.

–deckert

Until recently, I have used tabs, but am beginning to move to spaces a) because other people are now looking at/using my code b) it is specified for Drupal module contributions and c) when I have about 10 levels of indent, it's nice not to have to everything wrapping to the next line.

And I use vim, BTW.

Spaces, because TABs may not cut'n'paste properly so code can end up as a mixture of both.

(Yes, this can cause problems with sendmail and syslog which use TAB as the seperator….)

Tabs all the way, because I add code more than anything else, and it's simply faster and easier. On the occasion that I need to cut and paste (not often since I usually yank and paste in vi), I just use a regex to re-tab the lines.

I agree with Mr. Torvalds on this one, and I don't have the 80-column problem since I use a minimum of 120 columns in my terminals, often more. :)

I do most of my coding in python, where the mixing of tabs and spaces causes havoc, and the majority opinion points toward spaces.

Tabs only.

Nothing is more aggravating then having to delete individual spaces to make things readable when a single stroke to remove that tab would be quicker.

That and tab is one keystroke :)

And no I'm not using those spawns of Satan called vi or emacs or any of their bastard children like vim

I use tabs exclusively at the beginning of each line and spaces to align code at any point after the first non-tab character. The advantages of tabs at the beginning of a line are being overlooked in this thread, i think. The size of a tab can be redefined so when you are reading code that is indented umpteem times, you can set it to 2 or 3, and when reading code that doesn't use many blank lines, you can increase it to 8 or so to speed up visual scans of code. As long as you use the spaces to format tables or arrays or etc. then you won't get the wierd unaligned column problems that often accompany resizing tabs.

I've been programming mostly in straight C for 28 years and the answer is spaces, spaces, spaces…

EXCEPT you sure better be careful on those hand full of ascii configuration files (makefile is probably the best example) that require tabs only for white spaces. I shutter to think how many total man hours have been lost through the years due to this one issue.

Hal Williams

I prefer tabs, but I can deal either way. In Python I used to use spaces more, but now it's mostly tabs.

I hate it when they get mixed. Ewwww…

Spaces.

I use two spaces for most things.

I use four for php - but I usually write php in a nice gui editor (bluefish) - in vi in a 80 character wide xterm, 2 spaces is plenty.

spaces, definitely spaces.

my ${HOME}/.vimrc uses a 4 spaces soft tab stob. which in my opinion works a lot better than a tab :)

I'm with JWZ on this one: http://www.jwz.org/doc/tabs-vs-spaces.html

Definately tabs.

Why? Because it's neater. It takes bloody ages to delete a whole load of spaces, whereas only a few seconds to delete around 3-4 tabs.

Both!

I code in SQL, and TABs are an absolute must to line things up. In PL/SQL code, however, i use a space for indentation so the code does not go off the side.

A kind of rule would be: space when it is block indentation, tab when it is clause indentation.

Tabs AND Spaces.

I use tabs at the front of lines to indicate indent level, while spaces are used after the code in a line starts to line up things.

8-char tabs, all the time.

1) I usually work with PHP, which doesn't care about tabs or spaces.

2) I always use a GUI editor, 1680 pixels wide.

I spend most of my time in Python, so 4 spaces it is.

http://www.python.org/dev/peps/pep-0008/

I use 4 spaces because we (that is, the team I work with) follows the Zend Framework coding guidelines.

But I think using tabs makes a lot more sense. That's what tabs are for, after all: aligning things. And it's nice to be able to define a tab size that suits you, regardless of what the others are using.

Definetely spaces. The reason is simple, "a single space" always has a fixed width, whereas "a single tab" is a vague concept whose width depends entirely on the user. So, for interoperability causes and stuff like that, I say spaces.

Those are too attached to tabs can use soft tabs anyway.

I'm a Rubyist, so a size 2 soft tab for me.

SPACES! Tabs are so annoying and there is nothing worse than when some dude edits your code with Visual Studio or something and starts inter-mixing tabs and spaces. Even worse when they have their tabstop set at 8 chars.

Even if you like using the tab key configure your editor to enter "4 spaces" for a tab. All good editors can do this. And if you use vim… this command is your friend:

:retab

:-)

@cmelbye:

I'm a Rubyist, so a size 2 soft tab for me.

+1

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct