LinuxQuestions.org
Review your favorite Linux distribution.
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 12-03-2011, 04:57 AM   #1
r.osmanov
LQ Newbie
 
Registered: Feb 2011
Distribution: openSUSE, Ubuntu, Gentoo
Posts: 17

Rep: Reputation: 3
Question What is the best indentation/alignment style?


Hello.

What are the most compatible, portable etc. values for:
- tabstop
- soft tabstop
- shiftwidth
- whether to expand tab(to spaces)
?

For instance, the following are my settings in different projects for Vim:
Code:
# vim: noet ts=4 sts=4 sw=4
# vim: noet ts=8 sts=4 sw=4 ci pi
# vim: et ts=4 sts=4 sw=4
The problem is that it isn't printed correctly when
- using ex. cat command to show code in terminal
- printing on paper
- many other places like pastebin services etc.
- user left the default 8-space wide tab

It should also somehow depend on language. Well, what's about PHP, C/C++ and
JavaScript, for instance?

The lead of our team insists on tabs. But I wonder how should I align? To
install plugins, align manually(!), or not align at all :/ ? Or to accept, say,
Linux kernel coding style with 8-space wide tabs?

Regards.
 
Old 12-03-2011, 07:18 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
There is no 'best' style (and wars have started over this question).

The most portable setting is to use 8 space tabs, because that has been the default for so long in computing, but many people don't find it to be the most readable style. It is rare in kernel code to have deeply nested indentation, so it is a reasonable choice there.

The option of converting to spaces solves the tab problem by getting rid of them! It works fine if everyone on a project is content (or forced to) use the same spacing for tabs, which is why it is a common solution.

My personal preference is to leave the tabs in place (but to use tab purely for indentation semantics, ie, only ever at the start of a line, and with the soft tabstop the same as the tabstop). This means that others can choose their own preferences for how the indentation is viewed. As you have pointed out, there is extra work when formatting for printed or web output, because the decision has been deferred. But is is not such a big deal, just a matter of using expand when you need to produce such output.

Neither of these methods is bad; what is bad is when a team fail to agree and some use tabs and others don't...

There are some languages where it becomes really important to have consistency, like Occam and Python.

Last edited by neonsignal; 12-03-2011 at 07:30 AM.
 
Old 12-03-2011, 08:41 AM   #3
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by r.osmanov View Post
What is the best indentation/alignment style?
There's no such thing. It is subjective/matter of preference.

My personal preference is to use tabs (tab character not emulated with spaces) with proportional fonts in GUI text editors that supports "elastic tabs".

However, some opensource projects insist on using spaces for identation.

Quote:
Originally Posted by r.osmanov View Post
The lead of our team insists on tabs.
Then the best idea would be to use style everyone uses. Unless you're working on specific module nobody except you ever touches.

Quote:
Originally Posted by r.osmanov View Post
But I wonder how should I align?
You have to decide that yourself.
You can convert between spaces and tabs using sed or perl using "global replace". Changing from tabs to spaces is easier than changing from spaces to tabs (because somebody always uses odd number of spaces for identation, and after search/replace you end up with mix of tabs and spaces which is not good).

Last edited by SigTerm; 12-03-2011 at 08:43 AM.
 
Old 12-03-2011, 01:41 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Quote:
Originally Posted by r.osmanov View Post
The lead of our team insists....
Then the answer to all of your questions is ask your team. It's common for programming teams to have a coding standard that spells all this out.

Quote:
It should also somehow depend on language. Well, what's about PHP, C/C++ and
JavaScript, for instance?
It is very common for vim users to have different settings for each language they use. For the technical details of how to set that up, see my answer here:

http://programmers.stackexchange.com...on/84935#84935

Last edited by dugan; 12-03-2011 at 01:43 PM.
 
Old 12-04-2011, 08:56 AM   #5
r.osmanov
LQ Newbie
 
Registered: Feb 2011
Distribution: openSUSE, Ubuntu, Gentoo
Posts: 17

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by dugan View Post
Then the answer to all of your questions is ask your team. It's common for programming teams to have a coding standard that spells all this out.

It is very common for vim users to have different settings for each language they use. For the technical details of how to set that up, see my answer here:

http://programmers.stackexchange.com...on/84935#84935
I don't ask what should I do with our team. The question is "What is the best way to indent/align?".

I know these vim settings. The question is not concering use of vim, but what should a programmer use for
tabstop, soft tabstop, shift width and whether to expand tab.
 
Old 12-04-2011, 09:09 AM   #6
r.osmanov
LQ Newbie
 
Registered: Feb 2011
Distribution: openSUSE, Ubuntu, Gentoo
Posts: 17

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by neonsignal View Post
My personal preference is to leave the tabs in place (but to use tab purely for indentation semantics, ie, only ever at the start of a line, and with the soft tabstop the same as the tabstop). This means that others can choose their own preferences for how the indentation is viewed. As you have pointed out, there is extra work when formatting for printed or web output, because the decision has been deferred. But is is not such a big deal, just a matter of using expand when you need to produce such output.
Thank you for the reply.

I'd also note, that if I set
# vim: noet ts=4 sts=4 sw=4
(actually my preference), and some other programmer prefers 2- or 8-space wide tab:
# vim: noet ts=2 sts=4 sw=4
# vim: noet ts=8 sts=4 sw=4
or any other than 4, the output of the following will not be aligned:
Code:
$abcasdfsdfsd   = 1233;
$sd             = 34;
So the ability to adjust tab width doesn't work.
 
Old 12-04-2011, 09:39 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Quote:
Originally Posted by r.osmanov View Post
IThe question is not concering use of vim, but what should a programmer use for
tabstop, soft tabstop, shift width and whether to expand tab.
You have been answered in every follow-up, including mine: there is no single setting that's ideal for every language and every project. And if you don't like that answer, well, I'm sorry, but it's not my problem.

Quote:
- whether to expand tab(to spaces)
Well, this one actually has an answer. Every coding standard in existence recommends spaces instead of tabs.

Last edited by dugan; 12-04-2011 at 09:52 AM.
 
0 members found this post helpful.
Old 12-04-2011, 09:48 AM   #8
r.osmanov
LQ Newbie
 
Registered: Feb 2011
Distribution: openSUSE, Ubuntu, Gentoo
Posts: 17

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by dugan View Post
You have been answered many times, including once by me: there is no single setting that's ideal for every language and every project.

In the case of Python, the recommended settings are spelled out in the official documents:

http://www.python.org/dev/peps/pep-0008/
Well, I see it's encouraged to use spaces instead of tabs:
Quote:
For new projects, spaces-only are strongly recommended over tabs.
I also discovered that in many popular teams tabs are just prohibited
(Zend Framework, WebKit, GNU, PEAR, Drupal at least).

Okay, let's assume a new PHP + JavaScript + XHTML stuff project, say, CMS.
What tab settings would you choose for PHP files, for instance?

Quote:
Originally Posted by dugan View Post
Well, this one actually has an answer. Every coding standard in existence recommends spaces instead of tabs.
UPDATE: Just saw the update in your post. As it turns, you recommend spaces, implicitly however.
Thank you. Just what I was asking for - your opinion

Last edited by r.osmanov; 12-04-2011 at 09:57 AM. Reason: answer for prev post update
 
Old 12-04-2011, 10:00 AM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Here is my .vimrc file, in case you're wondering:

https://raw.github.com/duganchen/dotfiles/master/.vimrc
 
Old 12-04-2011, 10:16 AM   #10
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
I personally favour tabs because you can create a lot of inconsistent styles with space-based indenting depending on the editor you're using. With tabs, you can simply configure the tab width and you're set. With spaces, you HAVE to stick to a fixed number of spaces for indentation and remember to do it for all editors you use before you start coding. The confusion over how many spaces to use 4, 6 or 8 or whatever is transparent with tab-based indentation (everybody can simply configure their editors the way they want, without affecting others) while in space-based indentation it has to be enforced strictly in team based projects.

Last edited by vharishankar; 12-04-2011 at 10:19 AM.
 
Old 12-04-2011, 10:38 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
No "best", no right answer, only choices and it is sane to comply with whatever conventions are in use. Where there are coding standards the should be followed.

In Ruby, two space indentation is strongly encouraged and is used in all the examples in O'Reilly's "The Ruby Programming Language" and in every style guide I have seen.

I don't Python but understand indentation standards are mandatory or it wont run. The official style guide says to use four spaces but "For really old code that you don't want to mess up, you can continue to use 8-space tabs"

Spaces are generally but not universally preferred over tabs so the code looks as intended regardless of the editor's tabstop setting. FWIW I code bash using tabs for convenience and periodically run :%s/^I/ /g

EDIT: that only makes sense when using an editor that used fixed-pitch characters; the idea of using a proportional font in a code editor feels very strange.

Last edited by catkin; 12-04-2011 at 10:41 AM.
 
Old 12-04-2011, 05:43 PM   #12
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by r.osmanov View Post
I'd also note, that if I set
# vim: noet ts=4 sts=4 sw=4
(actually my preference), and some other programmer prefers 2- or 8-space wide tab:
# vim: noet ts=2 sts=4 sw=4
# vim: noet ts=8 sts=4 sw=4
or any other than 4, the output of the following will not be aligned:
Code:
$abcasdfsdfsd   = 1233;
$sd             = 34;
Which is precisely why I said that my preference is to have tabs "only ever at the start of a line". The editor I have traditionally used enforces this; in vim it is a self-imposed constraint. It is quirks like this that make it easier to just standardize on space expansion.
 
Old 12-06-2011, 11:43 AM   #13
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
If you are writing completely new code, use whatever works for the language and programmers that will use the code. If adopting code previously written by others, use the style chosen by the original author(s).
I prefer to not use tabs unless required (Makefiles), and would never use a proportional font in a source code editor. Four columns is a reasonable indentation for most languages that I use. In assembler, I would probably use eight-column indentation, or multiples of eight columns. I prefer comment blocks to be left-aligned for efficient block-copy-paste editing.
--- rod.
 
Old 12-08-2011, 11:28 AM   #14
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
I agree with the general advice, "just be consistent within your own development group".

I prefer two-space indentation. But my peers aren't happy with that, so we settled on a different standard, at least for our shared projects.
 
  


Reply

Tags
formatting, style, vim


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
[SOLVED] Vim indentation style comments MTK358 Linux - Software 3 07-15-2011 01:34 PM
e.g., BSD style (Slackware) vs. SystemV style startup scripts haertig Slackware 5 01-03-2009 10:52 PM
Compiling kernel Debian style or Native style ? Raynus Debian 1 06-16-2008 06:56 AM
How to know if I am using mbox-style or maildir-style? Niceman2005 Linux - General 1 09-23-2005 12:08 PM
VIM-style wrapping to OpenOffice style schmmd Linux - Software 1 12-21-2004 06:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:57 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration