Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
02-24-2010, 05:28 PM
|
#1
|
|
Member
Registered: Nov 2009
Posts: 144
Rep:
|
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?
|
|
|
|
02-24-2010, 05:39 PM
|
#2
|
|
Member
Registered: Jul 2004
Distribution: Ubuntu
Posts: 385
Rep:
|
Tabs for indenting code, and spaces for aligning. I think problems arise when different viewers have different spacings for their tabs.
|
|
|
|
02-24-2010, 07:30 PM
|
#3
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,947
|
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.
|
|
|
|
02-24-2010, 09:17 PM
|
#4
|
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,375
Rep: 
|
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.
|
|
|
|
02-24-2010, 09:18 PM
|
#5
|
|
Senior Member
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 4,548
|
That's why we have "pretty code-reformatters."
The only language where indentation actually matters is Python.
|
|
|
|
02-24-2010, 09:25 PM
|
#6
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
02-24-2010, 09:32 PM
|
#7
|
|
Member
Registered: Jan 2009
Distribution: slackware + ratpoison
Posts: 253
Rep:
|
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.
|
|
|
|
02-24-2010, 11:50 PM
|
#8
|
|
Member
Registered: Nov 2009
Posts: 144
Original Poster
Rep:
|
Great great. Thanks for your input everyone!
|
|
|
|
02-25-2010, 12:07 AM
|
#9
|
|
Member
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207
Rep:
|
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.
|
|
|
|
02-25-2010, 12:13 AM
|
#10
|
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,375
Rep: 
|
Quote:
Originally Posted by sundialsvcs
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.
|
|
|
|
02-25-2010, 03:51 PM
|
#11
|
|
Member
Registered: Jan 2009
Distribution: slackware + ratpoison
Posts: 253
Rep:
|
Quote:
Originally Posted by graemef
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?
|
|
|
|
02-25-2010, 07:13 PM
|
#12
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
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!
|
|
|
|
02-25-2010, 07:23 PM
|
#13
|
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,375
Rep: 
|
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!
Last edited by graemef; 02-26-2010 at 03:27 AM.
|
|
|
|
02-26-2010, 01:52 AM
|
#14
|
|
LQ Newbie
Registered: Feb 2010
Location: UK, Leeds
Distribution: PCLinuxOS, Fedora Core, SuSE,ubuntu
Posts: 20
Rep:
|
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..
|
|
|
|
02-26-2010, 06:52 AM
|
#15
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
Quote:
Originally Posted by fpsasm
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).
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:52 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|