Re: Tabs vs. Spaces

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 05/24/04


Date: Mon, 24 May 2004 16:37:38 -0400 (EDT)


On Mon, 24 May 2004, The Mike wrote:
>
> Sorry for bothering you with such an old subject. However, I've
> recently read a corresponding disussion and I'm wondering once again:
> What's the fuzz about it? AFAICS it's quite simple: if I use spaces,
> everyone who reads my code has to suffer from my indentation style. If
> I use tabs everyone will be able to view the same code with any
> indentation depth she likes.

 ...and will *still* suffer from your indentation style, because
indentation style affects much more than some silly ASCII control
code. For example, my indentation style is to have 4 spaces represent
one indentation level of braces, with 2 spaces indicating a logical
indentation level that is not braced, thus:

    if (foo) {
        bar;
        if (baz)
          quux;
        quuux;
    }
    else {
        quuuux;
    }

This style obviously loses its elegance (YMMV;) when you convert every
4 consecutive spaces to two consecutive spaces (i.e., entab the source
code and view it on a viewer with 2-space tabs). The code becomes even
less readable when entabbed and viewed on a viewer with 8-space tabs;
the inner line stretches all the way across the screen!

        if (foo) {
                bar;
                if (baz)
                        quux;
                quuux;
        }
        else {
                quuuux;
        }

Now imagine a *real* snippet in place of that "quux," and you see the
problem. Line-wrapping editors (such as MSVC++, IIRC) just exacerbate
it.

  Source code is not meant for mechanical consumption; it's meant
for human consumption. It should be formatted so that a human reader
will find it agreeable. This means the elimination of platform-specific
oddities like hard tabs. C source code is not HTML; if you want to
give control over to your reader's browser, you're programming in the
wrong language.

  Another problem, of course, is that ASCII doesn't have a "tab" character
in its email-safe subset (32-126 decimal). So of course you should never
try to send tabs over a plain-text medium like Usenet or email; that's
just asking for trouble.

  [Python, a language in which indentation is supremely important,
defines "tab" to mean 8 spaces even though the indentation level of
Python code is 4 spaces. Thus Python users steer clear of hard tabs
in general, and apparently avoid this particular holy war altogether.
It is a perfect example of how to avoid the C newbie's confusion of
indentation and tabbing.]

> It's often said that changing the tabsize could have unwanted
> sideeffects. How could that be? If I enter a <Tab>, a literal Tab
> will be inserted. It's up to the program (lpr, editor ...) to
> interpret the TAB. How could I possibly influence these programs?

  You can't. So that means that by that single keypress, you've
surrendered all control over how you want your text to be viewed.
Programmers in general love control, especially over their own
products. Thus, programmers in general hate tabs.

  As a final note, GNU indent already exists. If you really want
random tabs in your text, go download it and fiddle with the settings.
There's no reason to use tabs in production code today.

HTH.
-Arthur



Relevant Pages

  • Re: Request for comment on my tiny learning project: dfighterdb (mytree)
    ... -In most of your code I looked at, your closing braces line up with ... -Your code uses tabs for indentation. ... The cast is unnecessary. ... I don't do the indentation myself. ...
    (comp.lang.c)
  • Re: Coding Style: indenting with tabs vs. spaces
    ... While I respect you opinion about tabs, I find tab indentation the most ... Tabs become "logical indentation". ... isn't forced on anotherone's editor. ... any patches to the Linux kernel will have tabs used correctly. ...
    (Linux-Kernel)
  • Re: Using TABs vs. spaces II. (the Philosophy flamewar reloaded ;-))
    ... I read somewhere that good-style Ruby code is indented by 2 spaces. ... code if the indentation is only at 2 spaces. ... spaces or just tabs rather than switching between the two. ... You can alter tab length in your editor (for at least most editors, ...
    (comp.lang.ruby)
  • Re: reformat to tool/editor-compliant C style?
    ... convert 0x09 into whatever tabspace you've defined in your editor. ... anywhere that the code was written with tab-width 4. ... If someone writes this with tab-width = indentation = 4, ... no. *remove* all the tabs. ...
    (comp.lang.c)
  • Re: Tabs vs. Spaces
    ... >> everyone who reads my code has to suffer from my indentation style. ... >4 consecutive spaces to two consecutive spaces (i.e., ... >code and view it on a viewer with 2-space tabs). ... intervening history of how tabs have been used in computer systems is ...
    (alt.comp.lang.learn.c-cpp)