DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

View Poll Results: Which is best to use in source code - Tabs or Spaces?
Tabs 7 28.00%
Spaces 18 72.00%
Voters: 25. You may not vote on this poll

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 18th June 2008
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Default Tabs or Spaces?

Hello,

Which is best to use in source code - tabs or spaces?
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote
  #2   (View Single Post)  
Old 18th June 2008
drhowarddrfine drhowarddrfine is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 377
Default

Some time ago, I read that some apps do things differently when using tabs vs spaces. YAML, a file layout method, counts indent spaces which are significant. So some people, me included, always have tabs converted into spaces.
Reply With Quote
  #3   (View Single Post)  
Old 18th June 2008
lvlamb's Avatar
lvlamb lvlamb is offline
Real Name: Louis V. Lambrecht
Spam Deminer
 
Join Date: May 2008
Location: .be
Posts: 221
Default

Spaces! Yeesh! Ever opened source in different editors?
Then, try to debug code if as tab character was used for a regexp:

tr " " "\
" <infile >outfile

Unless I have to import data in spreadsheets or databases, in which case I prefer tabs.

Well ... not always either
__________________
da more I know I know I know nuttin'
Reply With Quote
  #4   (View Single Post)  
Old 18th June 2008
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,056
Default

Quote:
Originally Posted by lvlamb View Post
Spaces! Yeesh! Ever opened source in different editors?
+1

Some editors/IDEs like geany has an option to convert tabs to spaces every time you save a file, I also generally always use spaces instead of tabs, this way you are sure that code will be formatted the way you wanted to
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
  #5   (View Single Post)  
Old 18th June 2008
roddierod's Avatar
roddierod roddierod is offline
Real Name: Rod Person
VPN Cryptographer
 
Join Date: Apr 2008
Location: Pittsburgh, Pa
Posts: 437
Default

I use tab to spaces as well.

I have one ide I use for python that even though it looks like when you hit TAB you get the same indentation. When at the column number in the status bar, one column will read as 15 and the column on the line below will read as 17. So when you open it in another editor its all screwed up.
__________________
"The basic tool for the manipulation of reality is the manipulation of words. If you can control the meaning of words, you can control the people who must use the words." -Philip K. Dick
Reply With Quote
  #6   (View Single Post)  
Old 18th June 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Banned
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Default

Wow, How is tabs losing? a single tab indentation takes "1" byte compared to the alternative of 4/8 space characters.

It also helps make files look uniform in text editors... if any of you looked at BSD source, you would see they whole heartedly agree.
Reply With Quote
  #7   (View Single Post)  
Old 18th June 2008
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Tcpdump Spy
 
Join Date: Apr 2008
Location: Netherlands
Posts: 2,243
Default

Tabs for code ... always, it just works a lot better across different editors, people can set their own tabwidth, it's easier to move the cursor.
I never understood why some people use spaces for code ....

For text/config files that other people need to use I use spaces for obvious reasons.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #8   (View Single Post)  
Old 18th June 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

i imagine people who have voted for spaces have configured their editor to enter a set number of spaces in the file when a tab key is pressed. But whats the advantage to that?

Last edited by ephemera; 18th June 2008 at 07:44 PM.
Reply With Quote
  #9   (View Single Post)  
Old 18th June 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

i use a combination of tabs and spaces.
mod8/mod4 tabs for indentation and spaces for alignment
Reply With Quote
Old 18th June 2008
stukov's Avatar
stukov stukov is offline
Real Name: Jean-Michel Philippon-Nadeau
Package Pilot
 
Join Date: May 2008
Location: Sherbrooke, Qc, Canada
Posts: 167
Default

Spaces help code to look more uniform from platform to platform. It's hard to keep the tabs/space convention when you are working with a few developers working on different platforms/editors too.

I guess what is more important than choosing between tabs or spaces is sticking to it and making sure everyone follows the convention!
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Reply With Quote
Old 18th June 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Default

Well, this is something I am only mildly opinionated on believe it or not !!!

When it comes to things like tabs or spaces, where to put the brace, etc of coding style. I don't really care what _other_ people do as long as they _do_ it consistently.


I'm accustomed to writing code in a manor that I could write rule-sets based on my formating style to modify things if necessary, and where possible in a style that can hold up to my changing tastes.


When it comes to tabs or spaces for indentation, I use both but in different ways.


I prefer tabs when individual indentation levels are 'thick', for example I typically use an 8-space tabstop setting for C or C++ code. For that I find tabs easier to work with then spaces.


But when the indents are smaller (e.g. 2-4 spaces) I'll normally use spaces, if it's a moderate level (4sp) I'll pick either tabs or spaces for a given language and stick with it.

For example, I use a 4 space tabstop for Perl but 4 spaces for Python.



The thing that makes me some what different, is when I use tabs for indents -- I use spaces for whitespacing.

Code:
void
foo_funct() {
        char *src, *dst;
        int reallylong;
        long int arglist;
        size_t size;

        ...

        while ((var = bar_func(src, dst, size,
tab     |  spaces              reallylong, arglist)) != whatever) {
                if (var == something) {
                        do_whatever(var);
                }
        }
}
The idea is, the tab per indent level can be any thing but in order to align things, such as the the second line of bar_func()'s arguments. Tabs to indent to the same level as the above, then spaces to align the remainder.


That way I can change the tabstop from being 2, 4, 6, 8, 10, 12, whatever is desired and the code will still be 'logically' aligned rather then out of order.


The way I write changes from time over the years, I do however take sometime when using a language a lot. To sit down and play with formating and find "what works for me". Some of my idiosyncrasies in style are connected to my ways of thinking but most have a solid reason behind it, that I feel is worth while if ones going to do it consistently.


Note:

where logically aligned is defined as how I feel it is logical to align it.

I also have an 80-90x25-35 cols by lines editor to use most times and I limit lines to < 70-80char by nature.


Generally I do find spaces easier to work with when using most editors 'autoindent' capabilities.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Old 18th June 2008
halber_mensch's Avatar
halber_mensch halber_mensch is offline
Real Name: halber mensch
Port Guard
 
Join Date: Jun 2008
Location: Sapulpa, OK
Posts: 14
Default

well, being that I'm coding more in python and tal/dtml these days, I knee-jerked spaces.. because the levels of indentation make tabs unfeasible and python source tends to cause fits when tabbed and not spaced. That being said, this question is highly subjective.
__________________
perl -e "eval pack(q{H*}, join q{},qw{7072696e74207061636b28717b482a7d2c717b34393 23036333631366532303666366536633739323036313733373 33735366436353230373936663735323036353738373036353 63337343635363432303734363836393733323037343666323 03632363532303631323036633639373437343663363532303 66436663732363532303635366537343635373237343631363 93665363936653637326530617d293b})"
Reply With Quote
Old 18th June 2008
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Default

Hello,

In vim, you can convert tabs to spaces - that is when you press tab it inserts x number of spaces (x based on other settings) - with:
set expandtab or set et


Does anyone know how to do this in (n)vi?
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote
Old 18th June 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Default

Code:
:set shiftwidth=4

And indent lines with ^T or >> instead of 'tab' -- autoindent will respect this in OpenBSDs nvi *if* the indent is equal to sw but not ts, it will however use tabs to fill it up (e.g. two 'shiftwidths' gets a tab).


I don't think nvi will let you remap the tab key but one could always modify their terminal/console so pressing tab sends the same thing as control+t I guess.


PS

if you leave tabstop set to default 8 while using shiftwidth spaces to indent, you will be able to tell when unwanted tabs creep into your file.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.

Last edited by TerryP; 18th June 2008 at 09:51 PM.
Reply With Quote
Reply

Tags
vim

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:55 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick