Slashdot Mirror


Spaces vs. Tabs?

Mike Hall asks: "It has come up at work again. We are about to start a new design on our product and the spaces vs. tabs discussion is back. When you indent c/c++ code should all spaces, or tabs be used? Almost everyone agrees that a mix of the two is a bad idea. Currently most of the programmers here want to use tabs set to 4. I am a space junkie. I prefer all my code to look the same if I am looking at it on vi, emacs, less, or on a paper after a lpr command. What does the slashdot community feel is the pros and cons of spaces and tabs in code?" I'm kind of torn on the issue. I believe that properly indented code is important in improving readability, but I'm also a firm believer in the fact that to edit code, you shouldn't need to spend hours and hours on formatting. Tabs are the fastest way to align your data, but they have their drawbacks and formatting code with spaces takes for ever. What works best for some of you, out there?

5 of 32 comments (clear)

  1. Tabs Permit Individual Preferences by waldoj · · Score: 3

    You can set emacs, vi, and lpr to all make tabs equal whatever your preference is, in terms of size. Why force everybody to indent code with the same number of spaces when tabs let everybody make their code look like whatever they want?

    Waldo

  2. Ask JWZ by po_boy · · Score: 3
    JWZ's perspective is at Tabs versus Spaces: An Eternal Holy War. It's not too bad.

    Join us next week for stories such as "Jewish vs. Christian", "Emacs vs. vi", and "PERL vs Python"

  3. I use spaces by cperciva · · Score: 4

    I use spaces in order to maintain legibility across editors. That said, I normally use editors which automatically auto-indent my code with spaces anyway so there wouldn't be any time saving for me to use tabs.

  4. Thus spake Torvalds by danpbrowning · · Score: 3

    from /usr/src/linux/Documentation/CodingStyle:

    Chapter 1: Indentation

    Tabs are 8 characters, and thus indentations are also 8 characters.

    There are heretic movements that try to make indentations 4 (or even 2!)

    characters deep, and that is akin to trying to define the value of PI to

    be 3.

    Rationale: The whole idea behind indentation is to clearly define where

    a block of control starts and ends. Especially when you've been looking

    at your screen for 20 straight hours, you'll find it a lot easier to see

    how the indentation works if you have large indentations.

    Now, some people will claim that having 8-character indentations makes

    the code move too far to the right, and makes it hard to read on a

    80-character terminal screen. The answer to that is that if you need

    more than 3 levels of indentation, you're screwed anyway, and should fix

    your program.

    In short, 8-char indents make things easier to read, and have the added

    benefit of warning you when you're nesting your functions too deep.

    Heed that warning.

    --
    Daniel
  5. WHO CARES? Just use GNU indent(1) when done by satch89450 · · Score: 3

    Of all the holy wars, this is the one that makes the least sense to me. One of the reasons we have tools is to make such useless pablum as unnecessary as possible.

    Now, I speak as one who publishes source code on a regular basis, and finds that Mr. Torvald's edict works when editing but sucks when you are trying to publish on page. Never mind the fights with the "professional" graphic artists who don't understand the readers' needs, the rules according to Torvalds do not work on the published page. Witness Don Knuth's comments about white space management with respect to WEB source.

    So let people do it the way they want. Tabs at 8? Tabs at 4? Spaces? No tabs at all? Fine. When you are done with the code, run it through indent(1) with the shop standard specification and be done with it.

    And when I decide to write a book chapter using your Open Source code, I'll use my own configuration to make it easier to deal with on the printed page.

    Oh, you want to know my standard? Just remember, you asked for it...

    • Courier 72 with monospacing, with a 66% set width

    • Braces on their own lines

    • Open and close braces at the same indent as the enclosed text

    • Each indent is equal to the type height, which is identical to a M-space followed by an N-space at the 66% setwidth, or an M-space if you ignore setwidth

    • bolding all appropriate keywords and certain macro variable names

    Here is an approximate example (lack of proper typesetting controls in HTML prevent an exact depiction):

    int main(int argc, char argv[])
    {
    printf("Hello world!");
    }