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? |
Tabs for indenting code, and spaces for aligning. I think problems arise when different viewers have different spacings for their tabs.
|
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.
|
That's why we have "pretty code-reformatters."
The only language where indentation actually matters is Python. |
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.
Quote:
Quote:
|
Great great. Thanks for your input everyone!
|
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. |
Quote:
|
Quote:
Or am I misunderstanding you? Are you referring to doing away with tabs, or with formatting altogether? |
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! |
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! |
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.. |
Quote:
|
All times are GMT -5. The time now is 05:15 AM. |