Tabs vs Spaces

Please compose all posts in Emacs.

Moderators: phlip, Prelates, Moderators General

Re: Tabs vs Spaces

Postby Agent_Irons » Fri Jun 12, 2009 5:01 pm UTC

Four seems like a decent tab size. Two is a bit small, eight is definitely too big, and 3 isnt a power of two.

However, I have yet to find someone unhappy with tabbing to the correct level of indentation, then using spaces to set alignment (I actually hate alignment. I also hate "spaces between operators and variables" though. I'm a rebel)
Agent_Irons
 
Posts: 213
Joined: Wed Sep 10, 2008 3:54 am UTC

Re: Tabs vs Spaces

Postby lulzfish » Tue Jun 16, 2009 5:23 pm UTC

I always have pointers as "ClassName * instanceName" because I love spacing things out.
but I stand by my original post, tabs are the only choice for indentation.
User avatar
lulzfish
 
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Tabs vs Spaces

Postby Phaedrus » Mon Jun 22, 2009 10:14 pm UTC

Phaedrus
 
Posts: 1
Joined: Sat May 31, 2008 7:13 pm UTC

Re: Tabs vs Spaces

Postby lurkersanonymous » Tue Jul 21, 2009 1:10 pm UTC

Surprised no-one has linked this yet.....

Image

I have ViM set to convert entries from the <Tab> key into 4 spaces for Python and Java. For WRAMP Assembly it is 8 spaces, but that is because the compiler gets upset if that is not the case.
This just in from the tether ball Court, Obama's friend said Ms. Clinton has cooties.
User avatar
lurkersanonymous
 
Posts: 12
Joined: Thu May 01, 2008 12:09 am UTC

Re: Tabs vs Spaces

Postby ash.gti » Tue Jul 21, 2009 5:56 pm UTC

I have started following the Google C++ style for C and C++ and their ObjC styles for ObjC.

http://google-styleguide.googlecode.com ... pguide.xml
and
http://google-styleguide.googlecode.com ... cguide.xml

Which is honestly pretty close to how I was doing things before, now I just stick to things like roughly 80 characters a line, and I shortened my naming schema a bit.
# drinks WAY to much espresso
User avatar
ash.gti
 
Posts: 404
Joined: Thu Feb 07, 2008 1:18 am UTC
Location: Probably a coffee shop.

Re: Tabs vs Spaces

Postby qbg » Tue Jul 21, 2009 8:25 pm UTC

I believe Factor forbids tabs in source files, so there is no competition there.
qbg
 
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: Tabs vs Spaces

Postby Phaedrus » Wed Jul 22, 2009 1:31 pm UTC

lurkersanonymous wrote:Surprised no-one has linked this yet.....

There is a case to be made for mixing tabs and spaces, though: use tabs for expressing the indentation level and spaces for lining things up. Or rather, set up your editor to format the whitespace in this way.

Code: Select all
int f(int x,
......int y) {
--->return g(x,
--->.........y);
}

That way, everyone can view the code with the amount of indentation they prefer simply by changing the tab size.
Phaedrus
 
Posts: 1
Joined: Sat May 31, 2008 7:13 pm UTC

Re: Tabs vs Spaces

Postby hotaru » Wed Jul 22, 2009 1:51 pm UTC

Phaedrus wrote:That way, everyone can view the code with the amount of indentation they prefer simply by changing the tab size.

except for those who prefer to eschew indentation in favor of alignment, like so:
Code: Select all
int f(int x, int y)
{ x = g(x);
  y = g(y);
  return h(x, y); }
Code: Select all
uint8_t f(uint8_t n)
{ if (!(
n&1)) return 2;
  if (
n==169) return 13; if (n==121||n==143) return 11;
  if (
n==77||n==91) return 7; if (n==3||n==5) return 0;
  
n=(n>>4)+(n&0xF); n+=n>>4n&=0xF;
  return (
n==3||n==6||n==9||n==12||n==15)?3:(n==5||n==10)?5:0; } 
User avatar
hotaru
 
Posts: 955
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Tabs vs Spaces

Postby Phaedrus » Wed Jul 22, 2009 2:21 pm UTC

Code: Select all
int f(int x, int y)
{>x = g(x);
->y = g(y);
->return h(x, y); }
Last edited by Phaedrus on Thu Jul 23, 2009 12:06 pm UTC, edited 1 time in total.
Phaedrus
 
Posts: 1
Joined: Sat May 31, 2008 7:13 pm UTC

Re: Tabs vs Spaces

Postby ash.gti » Wed Jul 22, 2009 8:55 pm UTC

hotaru wrote:except for those who prefer to eschew indentation in favor of alignment, like so:
Code: Select all
int f(int x, int y)
{ x = g(x);
  y = g(y);
  return h(x, y); }


I really gotta say, that does not, in my opinion, improve the readability of the code at all, simply because the brackets are seemingly thrown into a syntax that doesn't make sense. I can understand the logic behind it, but it really does not improve the situation in C or C like languages at all.
# drinks WAY to much espresso
User avatar
ash.gti
 
Posts: 404
Joined: Thu Feb 07, 2008 1:18 am UTC
Location: Probably a coffee shop.

Re: Tabs vs Spaces

Postby null1024 » Thu Jul 23, 2009 1:18 am UTC

MHD wrote:1 press on the tab key = 4 spaces.

PERIOD!


End of discussion. 2 spaces is too small, 3 spaces is weird, 5 is too much, 8 is way too much, and hard tabs suck, especially because you'd thing they were 8 actual spaces in one editor, load it up in another, and they'd be 6 or 4 visible spaces wide, and some of the lines wouldn't match.
Image
null1024
 
Posts: 38
Joined: Fri Jun 05, 2009 10:20 am UTC
Location: About 4.91E11 feet from the sun.

Re: Tabs vs Spaces

Postby qbg » Thu Jul 23, 2009 2:59 am UTC

Two spaces, maybe because I'm use to Lisp...
qbg
 
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: Tabs vs Spaces

Postby phlip » Thu Jul 23, 2009 3:17 am UTC

null1024 wrote:Hard tabs suck, especially because you'd thing they were 8 actual spaces in one editor, load it up in another, and they'd be 6 or 4 visible spaces wide

Why is this a bad thing? If someone who prefers to indent by 6 characters loads your file, they'll see it in their preferred 6 characters. And when they edit it (using hard tabs too, as presumably this would be a company style guide or something) and you load it, all their new code is shown indented by 4 characters, how you like it.
null1024 wrote:and some of the lines wouldn't match.

Only if you do it wrong.
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 7216
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: Tabs vs Spaces

Postby Phaedrus » Thu Jul 23, 2009 12:03 pm UTC

Tabs can accommodate everyone!

Code: Select all
// Tab size: 8             // Tab size: 4         // Tab size: 2

int f(int x,               int f(int x,           int f(int x,
......int y) {             ......int y) {         ......int y) {
------->return g(x,        --->return g(x,        ->return g(x,
------->.........y);       --->.........y);       ->.........y);
}                          }                      }

int f(int x, int y)        int f(int x, int y)    int f(int x, int y)
{------>x = g(x);          {-->x = g(x);          {>x = g(x);
------->y = g(y);          --->y = g(y);          ->y = g(y);
------->return h(x, y); }  --->return h(x, y); }  ->return h(x, y); }
Phaedrus
 
Posts: 1
Joined: Sat May 31, 2008 7:13 pm UTC

Re: Tabs vs Spaces

Postby Rysto » Fri Jul 24, 2009 4:15 pm UTC

Tabs for indentation, spaces for alignment sounds great in theory, but people inevitably screw it up and make a mess of the source. It doesn't help that most editors get this totally wrong.
Rysto
 
Posts: 1443
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Tabs vs Spaces

Postby Phaedrus » Fri Jul 24, 2009 5:10 pm UTC

Let the editor take care of the tabs and spaces for you. Vim has the Smart Tabs plugin, Emacs has Smart Tabs code for several languages, IntelliJ IDEA has a "Smart Tabs" option, and GNU Indent can be persuaded with -i99 -ts99 -l999. Works as part of the auto-indentation.
Phaedrus
 
Posts: 1
Joined: Sat May 31, 2008 7:13 pm UTC

Re: Tabs vs Spaces

Postby xulaus » Sat Jul 25, 2009 1:04 am UTC

I use tabs for others convenience, but have set my tab width to 1. Suddenly it doesn't matter which one I use, provided no-one else uses my code.
Meaux_Pas wrote:I don't even know who the fuck this guy is
User avatar
xulaus
 
Posts: 136
Joined: Thu Jul 03, 2008 11:09 am UTC

Re: Tabs vs Spaces

Postby kernelpanic » Mon Jul 27, 2009 1:11 am UTC

Tabs. But the awesome, auto-multiple of 4 tabbing that emacs has.
I'm not disorganized. My room has a high entropy.
Bhelliom wrote:Don't forget that the cat probably knows EXACTLY what it is doing is is most likely just screwing with you. You know, for CAT SCIENCE!

Image
User avatar
kernelpanic
 
Posts: 891
Joined: Tue Oct 28, 2008 1:26 am UTC
Location: 1.6180339x10^18 attoparsecs from Earth

Re: Tabs vs Spaces

Postby OOPMan » Tue Jul 28, 2009 8:00 am UTC

Spaces. Any decent editor has an Expand Tabs option which exists for all those who are aware just how much Tabs fuck everything up.
Image

Image
User avatar
OOPMan
 
Posts: 314
Joined: Mon Oct 15, 2007 10:20 am UTC
Location: Cape Town, South Africa

Re: Tabs vs Spaces

Postby phlip » Tue Jul 28, 2009 9:42 am UTC

OOPMan wrote:how much Tabs fuck everything up.
Such as?

What exactly to tabs fuck up, that isn't an example of the person doing the tabbing Doin It Rong (by which I mean failing at using tabs for indenting and spaces for alignment)?
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 7216
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: Tabs vs Spaces

Postby Amnesiasoft » Tue Jul 28, 2009 11:00 am UTC

Gotta go with phlip here, the only way tabs-for-indent/spaces-for-alignment can mess things up is if you're doing it wrong.
User avatar
Amnesiasoft
 
Posts: 2575
Joined: Tue May 15, 2007 4:28 am UTC
Location: Colorado

Re: Tabs vs Spaces

Postby OOPMan » Wed Jul 29, 2009 3:06 pm UTC

Tab = Arbitrary symbol that can be translate into X spaces where X is a random number between 1 and infinity

Space = An arbitrary symbol that indicates exactly 1, you guessed it, space!

Now look that happens when I open that file written by MacGuyver over there with mixed Tabs and Spaces. Alignment failure. If I wanted to sort through MacGuyvers poorly indented text and I would been an Journalism/Creative Writing tutor and he'd be yet another uselss BA undergrad.

Either uses spaces or uses tabs. If you're going to use tabs, use expandtabs. If you're not, get the hell of my lawn. This is not a democracy. You do not get to choose how many spaces your damn tab equals. Tabs are for socialists and other people who think that other people should be allowed to make ANY choices at all about ANYTHING. In my country, you use a space to indicate a space and that's it!
Last edited by OOPMan on Wed Jul 29, 2009 3:13 pm UTC, edited 1 time in total.
Image

Image
User avatar
OOPMan
 
Posts: 314
Joined: Mon Oct 15, 2007 10:20 am UTC
Location: Cape Town, South Africa

Re: Tabs vs Spaces

Postby Amnesiasoft » Wed Jul 29, 2009 3:13 pm UTC

You seem to misunderstand what we mean by usings tabs for indenting and spaces for alignment.

Code: Select all
int foo(int ..bar,
........float baz) {
--->ArbitraryFunctionName(3.141,
--->......................0, 0, 0);
--->return 0;
}


Now what happens when I change my tab size to 2?

Code: Select all
int foo(int ..bar,
........float baz) {
->ArbitraryFunctionName(3.141,
->......................0, 0, 0);
->return 0;
}


How about 8?

Code: Select all
int foo(int ..bar,
........float baz) {
------->ArbitraryFunctionName(3.141,
------->......................0, 0, 0);
------->return 0;
}


When you only use tabs to indent and only use spaces for alignment, it will work regardless of your tab size. Unless maybe you have word wrapping on...but if you have word wrap on, you're clearly doin' it wrong.
User avatar
Amnesiasoft
 
Posts: 2575
Joined: Tue May 15, 2007 4:28 am UTC
Location: Colorado

Re: Tabs vs Spaces

Postby OOPMan » Wed Jul 29, 2009 3:16 pm UTC

This isn't about tabs and spaces!

This is about choice!

Tabs = Choose your own Spacing!
Spaces = Use the Spacing I give you!

Any sane individual will go with the latter option because the former involves relying on other people, entities which can generally be shown to most likely to do the WRONG thing.

;-)
Image

Image
User avatar
OOPMan
 
Posts: 314
Joined: Mon Oct 15, 2007 10:20 am UTC
Location: Cape Town, South Africa

Re: Tabs vs Spaces

Postby Phaedrus » Wed Jul 29, 2009 5:15 pm UTC

OOPMan wrote:Any sane individual will go with the latter option because the former involves relying on other people, entities which can generally be shown to most likely to do the WRONG thing.

People make mistakes whether tabs or spaces are used; that's why auto-indentation was invented. (How could you else trust people to indent nested code consistently?)

The problem with spaces is there's no general agreement on how many to use, so the value used by the auto-indentation must be changed from project to project. Tedious. With tabs, the setting is always to emit one tab per indentation level, whether a tab is displayed as two columns or four.

Tabs are used consistently, but displayed differently; spaces are displayed consistently, but used differently.

And again: tabs for indentation, spaces for alignment. It is the auto-indentation's job to distinguish between the two, so you don't have to, just like you don't reformat huge sections of code by hammering the space bar.
Phaedrus
 
Posts: 1
Joined: Sat May 31, 2008 7:13 pm UTC

Re: Tabs vs Spaces

Postby lulzfish » Wed Jul 29, 2009 7:20 pm UTC

Tabs have the following clear advantages:

1 keypress to adjust indentation, not 4
75% less file space (for a test sample consisting of 100% tabs equivalent to 4 spaces each)
If you use spaces, you hate America. Not just the US, you hate all of America. Even Canada and Mexico and Brazil.

Tabs use ONE character to represent ONE logical entity: An indentation.
Spaces use 4 characters and require the use of adjustment macros just to change the indent size.
It's like using a package manager... automating the problem instead of fixing it.
User avatar
lulzfish
 
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Tabs vs Spaces

Postby hackers238 » Wed Feb 03, 2010 11:04 pm UTC

Always spaces. Set VIM to expand tab to spaces.

Also map the tab key to word completion (if you're typing a word) Add the following to your .vimrc:
Code: Select all
function! Tab_Or_Complete()
  if col('.')>1 && strpart( getline('.'), col('.')-2, 3 ) =~ '^\w'
    return "\<C-N>"
  else
    return "\<Tab>"
  endif
endfunction
:inoremap <Tab> <C-R>=Tab_Or_Complete()<CR>


Taken from here: http://vim.wikia.com/wiki/Autocomplete_ ... ping_words
hackers238
 
Posts: 3
Joined: Wed Feb 03, 2010 10:32 pm UTC

Re: Tabs vs Spaces

Postby achan1058 » Thu Feb 04, 2010 2:51 am UTC

Spaces, since I don't want my code to be indented 8 space because I loaded it into notepad, or at places where I can't change the tab indentation.
achan1058
 
Posts: 1791
Joined: Sun Nov 30, 2008 9:50 pm UTC

Re: Tabs vs Spaces

Postby lulzfish » Thu Feb 04, 2010 4:26 am UTC

achan1058 wrote:Spaces, since I don't want my code to be indented 8 space because I loaded it into notepad, or at places where I can't change the tab indentation.

http://portableapps.com/apps/developmen ... p_portable

Tis nobler in the mind to take arms against a sea of failure, and, by opposing, end it.
User avatar
lulzfish
 
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Tabs vs Spaces

Postby achan1058 » Thu Feb 04, 2010 4:32 am UTC

Except that you might be using a Thin Client that does not have workable USB, not does it allow you to run random programs. (Though I personally try to stay away from doing anything more than e-mail with those things.)
achan1058
 
Posts: 1791
Joined: Sun Nov 30, 2008 9:50 pm UTC

Re: Tabs vs Spaces

Postby Meteorswarm » Thu Feb 04, 2010 6:22 am UTC

achan1058 wrote:Except that you might be using a Thin Client that does not have workable USB, not does it allow you to run random programs. (Though I personally try to stay away from doing anything more than e-mail with those things.)


Have internet, will vi.
The same as the old Meteorswarm, now with fewer posts!
User avatar
Meteorswarm
 
Posts: 980
Joined: Sun Dec 27, 2009 12:28 am UTC
Location: Ithaca, NY

Both

Postby flying sheep » Mon Feb 08, 2010 5:13 pm UTC

tabs for indentation, spaces for alignment, (like emacs’ smrt tabs linked to above) so would be one of those who get beaten up:
Image
but imho indentation is mostly bad coding style:
for huge constructors use variables (like kaboutdata. in my example, they indented, but this is not necessary since the arguments are used in the order the variables are assigned)
and if you want to create big string lists use text files or databases, don’t mix data with code!
User avatar
flying sheep
 
Posts: 63
Joined: Sun Jan 31, 2010 12:35 am UTC

Re: Tabs vs Spaces

Postby rar222 » Tue Feb 09, 2010 10:38 am UTC

Here are the basic rules I follow at work :

Indenting C/C++/Java : 4 spaces
Indenting HTML/XML etc : 2 spaces (otherwise it's too verbose)

I've also worked on a 1M+ line, 20+ year old software package that is a combination of both C and C++. In it there are mixes of all coding styles, brace styles and indenting styles from an uncountable hoard of programmers using vi/vim/emacs/<inset editor or ide here>.

I've got to say the only way to ensure a measurable amount of readability is to banish all tab characters. At least with spaces it's always consistent. The number of files I found with mixed space/tab indenting was huge, and my general fix was to reindent entire source files using spaces only. Problem solved.
rar222
 
Posts: 11
Joined: Thu Feb 04, 2010 11:29 am UTC

Re: Tabs vs Spaces

Postby flying sheep » Tue Feb 09, 2010 7:51 pm UTC

swich tab and space and your last paragraph would read:
I've got to say the only way to ensure a measurable amount of readability is to banish all space characters. At least with tabs it's always consistent. The number of files I found with mixed tab/space indenting was huge, and my general fix was to reindent entire source files using tabs only. Problem solved.
and still be wrong, since you can retab everything just like you can respace everything.

python is a little bit more difficult (in both directions), but possible, too. your choice is a matter of personal taste.
User avatar
flying sheep
 
Posts: 63
Joined: Sun Jan 31, 2010 12:35 am UTC

Re: Tabs vs Spaces

Postby flying sheep » Tue Feb 09, 2010 7:55 pm UTC

but still tabs are better :P
lulzfish wrote:Tabs use ONE character to represent ONE logical entity: A [single level of] indentation.
Spaces use [a variable number of] characters and require the use of adjustment macros just to change the indent size.
It's like using a package manager... automating the problem instead of fixing it.
User avatar
flying sheep
 
Posts: 63
Joined: Sun Jan 31, 2010 12:35 am UTC

Re: Tabs vs Spaces

Postby rm2010 » Wed Feb 10, 2010 1:59 am UTC

Tabs. Have your editor decide how wide they should be. The only problem are people who refuse to spend the time configuring their editor.
rm2010
 
Posts: 12
Joined: Wed Feb 10, 2010 1:43 am UTC

Re: Tabs vs Spaces

Postby Pesto » Tue Jul 27, 2010 8:11 pm UTC

I'm coming in on the side of tabs. The current code base I work on is standardized to use four spaces, and our editors are set to expand tabs to spaces. I hate when I hit the tab key one too many times and I have to hit the backspace key four times to undo it.
User avatar
Pesto
 
Posts: 738
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: Tabs vs Spaces

Postby Thesh » Wed Jul 28, 2010 6:33 pm UTC

People who use spaces for indenting should be thrown into a burlap sack, beaten with sticks, hung in the office parking lot, and set on fire for all to see.

Spaces for indenting make keyboard navigation annoying.
Now, remind me why you denied your clan?
"To be more human than human... "
Tell me, you expert in human nature,
would you still apply for candidature?
User avatar
Thesh
Has the Brain Worms, In Case You Forgot.
 
Posts: 3746
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: California, Southern USA

Re: Tabs vs Spaces

Postby enk » Mon Aug 09, 2010 6:50 pm UTC

Pesto wrote:I hate when I hit the tab key one too many times and I have to hit the backspace key four times to undo it.


Hm, tried shift+tab?

I like to think of indentation as indentation, whether it's tabs or spaces. Use indent and dedent operations in your editor. Then you don't even have to think about tabs vs. spaces. Many editors use tab and shift+tab (typically, you'll need to select some text on the line(s) to make the tab key indent rather than insert a tab). For vim it is >> and << for the current line (and . to repeat) or V, then j/k to select some lines, then just > and < (again, . to repeat). Using = (or == for a single line) will indent correctly according to brackets. All good editors will do this for you. Except ofc in Python :P
phlip wrote:Ha HA! Recycled emacs jokes.
User avatar
enk
 
Posts: 754
Joined: Mon Sep 10, 2007 12:20 am UTC
Location: Aalborg, Denmark

Re: Tabs vs Spaces

Postby bytbox » Sun Oct 17, 2010 4:49 pm UTC

Tabs.

Why does everyone claim 8 is too much? Assuming the average length of a line is 40 characters (an overestimate, IME), and you want to avoid going over 80 characters (although for some lines, it might happen), that gives you 40 characters of indentation. 5 tabs. If you're nesting any deeper than that, you're screwed anyway. Whereas people who use 4 spaces think it's ok to do 10 levels of indentation, and people who use 2 (which appears to be the emacs default for some languages) think it's ok to do 20 levels...
bytbox
 
Posts: 56
Joined: Wed Aug 19, 2009 5:43 am UTC

PreviousNext

Return to Religious Wars

Who is online

Users browsing this forum: No registered users and 1 guest

cron