LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 02-24-2010, 05:28 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
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?
 
Old 02-24-2010, 05:39 PM   #2
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 385

Rep: Reputation: 33
Tabs for indenting code, and spaces for aligning. I think problems arise when different viewers have different spacings for their tabs.
 
Old 02-24-2010, 07:30 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,232

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
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.
 
Old 02-24-2010, 09:17 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
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.
 
Old 02-24-2010, 09:18 PM   #5
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,263

Rep: Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085
That's why we have "pretty code-reformatters."

The only language where indentation actually matters is Python.
 
Old 02-24-2010, 09:25 PM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
Old 02-24-2010, 09:32 PM   #7
mattca
Member
 
Registered: Jan 2009
Distribution: Slackware 14.1
Posts: 321

Rep: Reputation: 52
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.
 
Old 02-24-2010, 11:50 PM   #8
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Great great. Thanks for your input everyone!
 
Old 02-25-2010, 12:07 AM   #9
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
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.
 
Old 02-25-2010, 12:13 AM   #10
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by sundialsvcs View Post
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.
 
Old 02-25-2010, 03:51 PM   #11
mattca
Member
 
Registered: Jan 2009
Distribution: Slackware 14.1
Posts: 321

Rep: Reputation: 52
Quote:
Originally Posted by graemef View Post
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?
 
Old 02-25-2010, 07:13 PM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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!
 
Old 02-25-2010, 07:23 PM   #13
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
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.
 
Old 02-26-2010, 01:52 AM   #14
fpsasm
LQ Newbie
 
Registered: Feb 2010
Location: UK, Leeds
Distribution: PCLinuxOS, Fedora Core, SuSE,ubuntu
Posts: 20

Rep: Reputation: 1
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..
 
Old 02-26-2010, 06:52 AM   #15
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by fpsasm View Post
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).
 
  


Reply

Tags
programming, spacing, style


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Basename and spaces: Directory has a space in its name Cyberman Programming 1 03-22-2009 05:54 AM
What is the unix command to trim multiple spaces into single space? manohare Linux - General 4 03-02-2009 07:25 AM
Replacing Tabs (^T) with spaces.... visitnag Linux - Newbie 3 05-04-2008 03:30 AM
tabs to spaces in emacs, not working in tcl mode ratm1286 Linux - Software 3 05-08-2005 05:15 PM
emacs - convert auto indent tabs to spaces? BrianK Linux - General 1 04-15-2004 10:14 PM


All times are GMT -5. The time now is 12:23 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration