Indentation: Tabs vs. Spaces

This is one of these archaic question.

Short answer: Spaces, and only spaces.

Long answer:

Origins
The tab key on the keyboard seem to have first been introduced with typewriters and then with computers. The original idea was not about indenting complex code structure. A long time ago, some people started to use tabs for indentation because of the disk space it was saving.

Concept
The concept is fine though: allow everyone to decide how much he likes it to be indented. I have ran into situations where I wanted a line to be precisely below a specific character. It is then very easy to just eyeball it and not having to count the amount of tabs and spaces.

Consistency
For me, consistency is key. Spaces are universal. Every computer and viewer understands what a space is, and how much space to allocate for it. When I'm on a different machine and just want to bring up some code in Notepad or TextEdit, I don't want it to look different to me.

Mix
I find that users using tabs rarely use tabs 100% of the time. You end up getting a bizarre mix of tabs and spaces. Spaces are constant and predictable. Consistency is out the window when a file has half tabs, half spaces. A file like that is a complete mess to look at with anything except the original tab equivalents (and changing tab settings is too much effort).

I guess that someone who's viewing my code might hate me with my fixed spaces, but at least he won't have to adjust his viewer.

General Preferences
Most of the projects I worked on/with (open source or not) uses spaces only for indentation. I was very surprised to see that propel coding standard is using tabs only. I also searched the web for polls and surveys to find out what the community preferred in general. I was shocked to see that most people preferred is using tabs.

My Dilema
I am working on a project right now which did not have any coding standards. Half the files are in DOS format and half the files are in UNIX format. Pretty much every file uses a mix of tabs and spaces. I am bringing order to chaos.

My Solution

I will be using dox2unix and emacs in order to fix up the format and replaces tabs. I thought about using expand and indent but I do not think they would have understood PHP structure.

Converting all files to UNIX format is very simply. Just run this command:

find . -name '*.php' -exec dos2unix {} \;

Removing tabs and replacing them with spaces and then indenting properly is a bit trickier. I already configured my .emacs file to understand the coding standard that I want. All I had to do was run this:

find . -name '*.php' -exec emacs -q -batch {} -eval '(progn(load-file "~/.emacs")(php-mode)(setq c-basic-offset 4)(indent-region (point-min) (point-max))(save-buffer))' \;

This would find all the .php files and execute emacs that would remove all the tabs and replace them with spaces and re-indent all lines according to the standard that I have set.

SVN

You can ask SVN to force al lthe text files to be in UNIX format for people using Linux/Unix and in DOS format for windows and in MAC format for.... well... mac people :)

How? SVN Properties my dear!

Make sure to set all the text files to have the property svn:eol-style set to the value "native" (without the quotes). Do not add this property to binary files like images. Only on source files like .php

This takes care of all the existing files but... what about new files being added? It's a pain to train all developer to remember to set this property whenever he or she adds a new source file. They will certainly forget...

But wait! SVN has a solution for that too!
Make sure that all developers changes the svn config file to enable auto props.
See the enable-auto-props config option and the [auto-props] section of the subversion/config file.

enable-auto-props = yes

[auto-props]
*.php = svn:eol-style=native
*.phtml = svn:eol-style=native
*.tpl = svn:eol-style=native

Voila! You are all set!

References:

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • You may post PHP code. You should include <?php ?> tags.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options