You are not logged in.

#1 2010-07-15 14:14:17

rb
Member
From: Argentina
Registered: 2010-05-07
Posts: 143

Questions about tabs, spaces and indenting

I tried to find some information about this in the forum, but I didn't succeed (perhaps I failed at the search). My apologies if this a trivial topic, but as I'm in the learning process of programming (I started with Python and php myself, and Pascal at college) this is a new world to me.

Yesterday I was editing a txt file in Vim, and I encounter a few issues trying to print it. That's when I learned that tabbing all the way thought to format some text maybe is not a good idea. Then a guy in #Vim directed me to use spaces instead of tabs for indenting and formating the text / programs that I write, as the tab indenting is -almost- only needed by makefiles. Also he showed me than in Python programming it is suggested to use spaces as well.

I googled a bit and I found sites like this, with pros and cons about both styles. Also I found a Vim plugin (Smart Tabs) that at first sight seems quite nice, like it have the best of both worlds.

So my question here is: what should I do? Choose one style and stick with it for ever? Choose according the language? Mix up both? Without any experience on this subject it's hard to know the right way, as it seems that both styles have (strong) advantages and disadvantages.

Also, if is not too much no ask, what tab size should I work with? 8 seems a bit large, although seems to be pretty standard for some languages.

Currently I've this on my .vimrc (the relevant part, of course):

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab

set autoindent
set smartindent

Thanks in advance for any advice.


Sorry for my English. Feel free to point out my errors.

Offline

#2 2010-07-15 14:17:59

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: Questions about tabs, spaces and indenting

I just always use 4 spaces. you can't go wrong with that in any editor.


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#3 2010-07-15 14:25:16

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Questions about tabs, spaces and indenting

> I just always use 4 spaces. you can't go wrong with that in any editor.
+1
... unless your team agreed on another settings.

Offline

#4 2010-07-15 14:27:00

Cyrusm
Member
From: Bozeman, MT
Registered: 2007-11-15
Posts: 1,053

Re: Questions about tabs, spaces and indenting

take a look into adding some "autocmd" lines into your .vimrc
This will automatically set vim options based on the type of file you happen to be working on.

here's an example I found, seems pretty full featured.
http://github.com/bryanforbes/dotfiles/ … ster/vimrc

look at lines 57-101.  I'm pretty sure this is what you are looking for.


Hofstadter's Law:
           It always takes longer than you expect, even when you take into account Hofstadter's Law.

Offline

#5 2010-07-15 14:28:24

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: Questions about tabs, spaces and indenting

karol wrote:

> I just always use 4 spaces. you can't go wrong with that in any editor.
+1
... unless your team agreed on another settings.

Then you change your team tongue

Last edited by Inxsible (2010-07-15 14:28:32)


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#6 2010-07-15 14:30:47

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Questions about tabs, spaces and indenting

> Then you change your team
I never thought one can be so dogmatic wrt tab-spaces dilemma. ;-)

Offline

#7 2010-07-15 14:31:04

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Questions about tabs, spaces and indenting

Yes, I think to "choose one style and stick with it" is the most important thing.

I always use two spaces to indent because I like the way it looks. Besides makefiles, I don't use any language that forces me to use either tabs or spaces, so that's not a problem.

I really like this quote:

Linus Torvalds wrote:

If you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

(from Wikiquote) After reading that, I don't think about indentation as much anymore. smile

Sorry for my English. Feel free to point my errors.

"Sorry for my English. Feel free to point out my errors." ^_^

Offline

#8 2010-07-15 14:31:56

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: Questions about tabs, spaces and indenting

karol wrote:

> Then you change your team
I never thought one can be so dogmatic wrt tab-spaces dilemma. ;-)

Double entendre.

Either make your team change the way they work...or actually change your team big_smile


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#9 2010-07-15 14:42:10

rb
Member
From: Argentina
Registered: 2010-05-07
Posts: 143

Re: Questions about tabs, spaces and indenting

Thanks you all for your replies, that was quick smile

I think I settle with 4 spaces indent. One final question: this should be commented in the source code (or be noted in any other way for that matter), or is totally irrelevant to someone else?


@drcouzelis: Thanks for pointing that out tongue


Sorry for my English. Feel free to point out my errors.

Offline

#10 2010-07-15 14:52:10

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Questions about tabs, spaces and indenting

> One final question: this should be commented in the source (...)
Cyrusm showed you a nice set of autocmds - autocmds are one of the ways people do it.

Offline

#11 2010-07-15 15:05:29

rb
Member
From: Argentina
Registered: 2010-05-07
Posts: 143

Re: Questions about tabs, spaces and indenting

Oh, what I meant is that if I need to state in the source code that I'm using 4 spaces instead of tabs so anyone that read that source code is aware of that, or if that is unnecessary.

I'm sure I didn't explained myself, sorry about that.


Sorry for my English. Feel free to point out my errors.

Offline

#12 2010-07-15 15:07:38

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Questions about tabs, spaces and indenting

rb wrote:

One final question: this should be commented in the source code (or be noted in any other way for that matter), or is totally irrelevant to someone else?

Most projects have a HACKING file that explain their code standards.


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#13 2010-07-15 15:17:40

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Questions about tabs, spaces and indenting

> I'm sure I didn't explained myself, sorry about that.
You did and I think I understood you right.

autocmd BufRead,BufNewFile *.hs set et ts=2 sw=2 softtabstop=2

This one line may be enough. You put it in the haskell files you work on and they serve as meta-information about the formatting.

Offline

#14 2010-07-15 15:18:34

rb
Member
From: Argentina
Registered: 2010-05-07
Posts: 143

Re: Questions about tabs, spaces and indenting

Thanks. That is what I tried to ask tongue

I'll check some github projects for that file, that way I can learn how to do it. Thanks again to all of you smile


Sorry for my English. Feel free to point out my errors.

Offline

#15 2010-07-15 16:23:55

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: Questions about tabs, spaces and indenting

rb wrote:

Oh, what I meant is that if I need to state in the source code that I'm using 4 spaces instead of tabs so anyone that read that source code is aware of that, or if that is unnecessary.

I'm sure I didn't explained myself, sorry about that.

I just tell my team to not mess with my code. I really do. big_smile


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#16 2010-07-15 16:41:56

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Questions about tabs, spaces and indenting

rb wrote:

'll check some github projects for that file, that way I can learn how to do it. Thanks again to all of you smile

Here is an example from our very own pacman.git: http://projects.archlinux.org/pacman.git/tree/HACKING


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#17 2010-07-16 08:58:15

Watermel0n
Member
Registered: 2010-03-10
Posts: 60

Re: Questions about tabs, spaces and indenting

Why would anyone use spaces?
Here's the ultimate advantage of tabs: Everyone can set his own preferred tab width.
Introducing Peter and Sven. Peter likes a 2 space indent because then a lot of code fits onto the screen. Sven prefers a 8 space indent because then the indenting is more visible. So what do they do? Exactly, they use tabs to indent. Sven sets his tab width to 8 spaces and Peter sets his to 2. Problem solved.

Offline

#18 2010-07-16 11:52:40

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Questions about tabs, spaces and indenting

Watermel0n wrote:

Why would anyone use spaces?

Because inevitably someone will use a combination of spaces and tabs for indentation and alignment and then everyone is confused.  Tabs are fine, spaces are fine.  Just don't use both.


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#19 2010-07-16 12:01:48

rb
Member
From: Argentina
Registered: 2010-05-07
Posts: 143

Re: Questions about tabs, spaces and indenting

Watermel0n wrote:

Why would anyone use spaces?
Here's the ultimate advantage of tabs: Everyone can set his own preferred tab width.
Introducing Peter and Sven. Peter likes a 2 space indent because then a lot of code fits onto the screen. Sven prefers a 8 space indent because then the indenting is more visible. So what do they do? Exactly, they use tabs to indent. Sven sets his tab width to 8 spaces and Peter sets his to 2. Problem solved.

In my very short experience, using tabs screws up the text formatting when is opened in different editors. Generally not an issue if the line starts with tabs chars, but when there're tabs after some text (like <tab><tab><text><tab><text>). As I'm not a programmer myself, I don't know how ofter do you insert tabs after the text (maybe for comments or align some variables values or such?)


Sorry for my English. Feel free to point out my errors.

Offline

#20 2010-07-16 12:53:40

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Questions about tabs, spaces and indenting

The reason I don't like tabs is because, even though Vim might know to render them as 4 spaces, cat/grep/sed/awk/perl and friends don't.

Offline

#21 2010-07-16 17:03:58

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 858
Website

Re: Questions about tabs, spaces and indenting

The problem I have with tabs is just that this:

int
some_long_function(some_really_long_declaration_that_forces_a linebreak,
<tab><tab><tab>    other_parameter name)
{

will look totally different in another editor with a different tab size:

int
some_long_function(some_really_long_declaration_that_forces_a linebreak,
<longtab><longtab><longtab>    other_parameter name)
{

EDIT: I guess it would be okay if you always used spaces for alignment like this, but most editors will insert tabs for you when you try to indent that line, possibly without you even noticing.

Last edited by tavianator (2010-07-16 17:05:37)

Offline

#22 2010-07-17 01:14:31

valium97582
Member
Registered: 2010-06-19
Posts: 126

Re: Questions about tabs, spaces and indenting

Or better, switch over to Whitespace. Then you will not confuse yourself mad.

I always used to indent code with tabs, but after reading that I will try to use spaces-only.


I'm also known as zmv on IRC.

Offline

#23 2010-07-17 03:56:10

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Questions about tabs, spaces and indenting

rson451 wrote:
Watermel0n wrote:

Why would anyone use spaces?

Because inevitably someone will use a combination of spaces and tabs for indentation and alignment and then everyone is confused.  Tabs are fine, spaces are fine.  Just don't use both.

And there it is. There is truly nothing left to be added to this discussion that hasn't been discussed to death for many years.

rson451, thanks.

Offline

Board footer

Powered by FluxBB