LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Spaces vs. Tabs, and a specific question about space indenting. (https://www.linuxquestions.org/questions/programming-9/spaces-vs-tabs-and-a-specific-question-about-space-indenting-791359/)

golmschenk 02-24-2010 05:28 PM

Spaces vs. Tabs, and a specific question about space indenting.
 
Which do you prefer and why?

Also, I remember someone telling me once that while using space indentation use either 4 spaces or 2 spaces. Don't use 3 spaces. Anyone know why they would say not to use 3 spaces?

vxc69 02-24-2010 05:39 PM

Tabs for indenting code, and spaces for aligning. I think problems arise when different viewers have different spacings for their tabs.

chrism01 02-24-2010 07:30 PM

1. I think it dates back to typewriters, but the default spaces-per-tab used to be 8; still often seen in text docs, Word etc.
2. Personally I use 4 spaces-per-tab for coding & I think that's the most common number you'll see.
3. for me 2 spaces-per-tab would be too condensed for easy reading, especially if you're indented several times. Visually, 4 would line up better/more obviously.
4. No reason i know of not to use 3, but as per note 2., that's very unusual.
5. vxc69 makes a good point about different people using different nums.
If you're in a team, try to get get people to agree (hah) on a std, even if they choose a num you don't like.

graemef 02-24-2010 09:17 PM

I try to always use spaces rather than tabs, which is made easy by configuring my editor to convert tabs to spaces. That way the code will retain the same "look" regardless of which application is used to view it. And for each indentation I use three spaces. But if you are coding on a team it is much better to use the same system as everyone else.

sundialsvcs 02-24-2010 09:18 PM

That's why we have "pretty code-reformatters."

The only language where indentation actually matters is Python.

jschiwal 02-24-2010 09:25 PM

IMHO, another reason fewer spaces are used to indent code is because we don't have the wide paper printers we used to with the green and white striped paper. I use `:set sw=4 sts=4' in vim for four spaces for indentation. For a language like LISP with all the parenthesis, 2 spaces for indentation my be better.

mattca 02-24-2010 09:32 PM

I cut my teeth on unix programming in C and C++. I use tabs with 8 spaces each.

Quote:

Anyone know why they would say not to use 3 spaces?
To my ears, that sounds like asking what the colour green smells like :)

Quote:

The only language where indentation actually matters is Python.
Well.. if by "matters" you mean "required by the language", then yes. Computers don't care, as long as the syntax is correct. On the other hand, I think most people who deal with code on a regular basis would say it matters quite a lot.

golmschenk 02-24-2010 11:50 PM

Great great. Thanks for your input everyone!

Dan04 02-25-2010 12:07 AM

I use '\f' for indentation and '\v' for alignment. That way I can claim more "lines of code" :)

But seriously, I prefer to indent with spaces: 4 in Python and 3 in curly-brace languages. At work, we have to use tabs, but that's caused us trouble with Doxygen not putting the tab stops in the same places as the editor. With spaces, you don't have to worry about that.

graemef 02-25-2010 12:13 AM

Quote:

Originally Posted by sundialsvcs (Post 3875840)
That's why we have "pretty code-reformatters."

But why bother if it is not necessary? Since I used to embed a lot of code inside text documents I found it easier to do away with the tabs, and that became habit for me.

mattca 02-25-2010 03:51 PM

Quote:

Originally Posted by graemef (Post 3875959)
But why bother if it is not necessary?

Because code needs to be read by human beings?

Or am I misunderstanding you? Are you referring to doing away with tabs, or with formatting altogether?

MTK358 02-25-2010 07:13 PM

Tabs for indentation, spaces for alignment. That's the only way that makes sense.

That way, I can even change the tab size and the code still looks fine.

And I absolutely can't stand it when code is indented with spaces, especially when editors default to indenting with spaces!

graemef 02-25-2010 07:23 PM

I think you misunderstood me.

In my first post I said that I always use spaces rather than tabs. I do that because I find it easier and provides me with a more consistent approach. I used to write articles and course notes that included a snippets of code; by having no tabs in the code I found that the formatting was an easier job when using such tools as a word processor.

Certainly when it comes to reading the code I find that the presentation is important and I am very particular about how it looks. After years of being fussy about how my code looks it comes naturally, at least my own particular style does!

fpsasm 02-26-2010 01:52 AM

Tabs for indent ( personally, I think that the 1TBS (1 True Bracket Style) is the best out there)

Spaces for clarity (eg, x=2, x = 2,) or for vertical allignment.

I like ts = 2 and 4, anything higher is a pain in the... back.. (especially in alot of loops, ts=2 is better)

Im a huge fan of hungarian notation (e.g: numYear, is a integer (hence the num) of years (in the name)

Camel case makes long variables easy to read..

MTK358 02-26-2010 06:52 AM

Quote:

Originally Posted by fpsasm (Post 3877409)
Tabs for indent ( personally, I think that the 1TBS (1 True Bracket Style) is the best out there)

Spaces for clarity (eg, x=2, x = 2,) or for vertical allignment.

I like ts = 2 and 4, anything higher is a pain in the... back.. (especially in alot of loops, ts=2 is better)

Im a huge fan of hungarian notation (e.g: numYear, is a integer (hence the num) of years (in the name)

Camel case makes long variables easy to read..

I agree completely (except I don't use Hungarian Notaton, not sure whether it really makes sense for me).


All times are GMT -5. The time now is 05:15 AM.