View Full Version : Tabs or Spaces?
JMJ_coder
06-18-2008, 02:23 PM
Hello,
Which is best to use in source code - tabs or spaces?
drhowarddrfine
06-18-2008, 02:46 PM
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.
lvlamb
06-18-2008, 03:03 PM
Spaces! Yeesh! Ever opened source in different editors? :eek:
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
vermaden
06-18-2008, 04:00 PM
Spaces! Yeesh! Ever opened source in different editors? :eek:
+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 ;)
roddierod
06-18-2008, 04:59 PM
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.
BSDfan666
06-18-2008, 05:39 PM
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. ;)
Carpetsmoker
06-18-2008, 07:52 PM
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.
ephemera
06-18-2008, 08:34 PM
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?
ephemera
06-18-2008, 08:46 PM
i use a combination of tabs and spaces.
mod8/mod4 tabs for indentation and spaces for alignment
stukov
06-18-2008, 09:26 PM
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!
TerryP
06-18-2008, 10:04 PM
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.
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.
halber_mensch
06-18-2008, 10:15 PM
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.
JMJ_coder
06-18-2008, 10:29 PM
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?
TerryP
06-18-2008, 10:48 PM
: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.
vBulletin® v3.7.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.