Spaces vs. Tabs, and a specific question about space indenting.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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.
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.
I cut my teeth on unix programming in C and C++. I use tabs with 8 spaces each.
Anyone know why they would say not to use 3 spaces?
To my ears, that sounds like asking what the colour green smells like
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.
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.
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!