Re: Tabs or Spaces?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Four spaces are used by a lot of open source projects, including Apache
and PHP, so I've been trying to convert, myself.

In general, spaces just make it easier to do multi-line formatting with
exact precision, like this:

if ($foo ||
    $bar ||
    $boo)
{
    ...
}

If this statement was also nested, you can see where tabs might become a
problem. With tabs, you can always make a habit of indenting any extra
lines one more tab than the original, but that doesn't always line up so
nicely (e.g., $bar may or may not be directly below $foo). You can also
simply repeat the same number of tabs then switch to spaces for the
alignment:

<tab><tab>if ($foo ||
<tab><tab><space><space><space><space>$bar ||
<tab><tab><space><space><space><space>$boo))
<tab><tab>{

But that just gets ugly. :-)

If you ever have a difference of more than a single tab, the alignment can
look radically different, so this should never be used, even if it makes
things look nice with your personal tab settings. In general, any
formatting that depends upon tab settings is going to look really ugly
somewhere.

Tabs are convenient, because they're a single character. You can backspace
to quickly remove an indentation, and a single keystroke can be used to
add an indentation. However, I've learned that a few simple editor
settings combined with some adjustments to my editing habits can overcome
these slight inconveniences. When I hit the tab key, four spaces appear,
and I can use vertical selections to unindent things quickly regardless of
whether the indentations use spaces or tabs. I bet some people have their
editors configured to remove four spaces when you backspace, assuming the
four characters to the left of the cursor are all spaces. If anyone has
done this for vim, please share. :-)

Anyway, those are some random thoughts from someone in the middle of
converting from tabs to spaces. I've basically decided to switch for two
reasons: improved precision over formatting and consistency with other
major open source projects. This doesn't mean spaces are best for
everyone, but that's my reasoning.

Chris

=====
Chris Shiflett - http://shiflett.org/

PHP Security - O'Reilly     HTTP Developer's Handbook - Sams
Coming February 2005        http://httphandbook.org/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux