RE: [PEAR-DEV] tabs and spaces (was: VIM config in PEAR files?)
| From: | Brent Cook | Date: | Tue, 16 Apr 2002 13:22:34 +0000 |
| Subject: | RE: [PEAR-DEV] tabs and spaces (was: VIM config in PEAR files?) | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-5561@lists.php.net to get a copy of this message | ||
> On Mon, 2002-04-15 at 11:23, Dave Mertens wrote:
>
> Tabs have variable rendering size, so using spaces only is the only
> predictable way of formatting code. Did you ever see what happens to
> indentation in "cvs diff" output on code that uses a mix of tabs and
> spaces? Or in "less"? Or in any other program using a different tab
> width? It's all messed up, and that's why we have this standard.
If this argument for tabs does not convince you, nothing will. It is the
most rational one that I can argue.
The reason why things become strange with tabs is due to tabs being used
improperly. A big source of confusion is when people confuse indenting
with formating, or use the tab key as a faster spacebar.
Take this code block:
if ($wha_hwa) {
horse ();
switch ($dog) {
case 'mutt':
bark();
break;
}
}
}
Using tabs for indenting here breaks nothing from editor to editor. Even
if your tab width is set to '4', '2' or '16', the code will look like
you
expected it to.
Now, this is where people normally screw up tabs:
$bob = array ( 'cats' => 'hats',
'dogs' => 'jackets',
'mice' => 'bleah'
);
The last three lines were not part of an indented code block. Therefore,
spaces should have been used. Notice that the width of "$bob = array (
'cats" is a fixed number of characters no matter your tab settings. Even
in the top line, spaces were used to format 'cats', so why use tabs for
the rest of the code. If tabs are used here, there is no guarantee from
editor to editor that code will end up remotely where it should be. Try
viewing this email width different tab widths to see what I mean. It only
works with tab=8.
So, in this case, use spaces to _format_ the code:
$bob = array ('cats' => 'hats',
'dogs' => 'jackets'
'mice' => 'bleah'
);
Now, if I wanted to indent this code, I would use a tab at the beginning
of each line:
$bob = array ('cats' => 'hats',
'dogs' => 'jackets'
'mice' => 'bleah'
);
The same goes for comments:
function whap() {
// a tabbed comment
turtle(); // Now use spaces. Lie on the ground
a++; // Listen to the sound
}
Spaces are used to format the comments so that they are a uniform distance
away from code on the right, while the top comment uses indenting. Use
tabs where you just want to indent, use spaces where formating must be
exact. Never use tabs on the right-side of a piece of code.
Both tabs ans spaces have their uses. While using spaces only is the
simple lowest-common-denominator solution, it does result in a minimal
increase in code size, extra difficulty in reindenting code, and forces
you to work with the author's tab-size preferences. With the above method,
we have the best of both worlds, allowing user-defined indenting while
preserving formating.
The only problems arise when people don't follow a consistent tab/space
convention. I even slip sometimes with my code (my preferred tab width is
2). I catch this when I look at the code on another machine and notice a
freaky line, like this:
/**
* comment comment
* comment comment
* comment comment
*/
Here, I used a tab at the beginning of each line. For every line after the
first, I used a single space to format to the first one. Whoops, typed two
tabs on one. Easily overlooked with tab width=2. Its an easy fix though,
and well worth the effort.
- Brent