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?

12 of 32 comments (clear)

  1. Re:What about the wrong spaces? by Tet · · Score: 2
    If a file contains tabs you can easily :se ts=4 or :se ts=2 to your preference.

    Precisely! A tab is a single indent. That's completely portable across any environment. By far the biggest argument against using spaces (and I've seen it in *every* instance I've worked anywhere that uses spaces) is that people try to do it manually -- i.e., without having the editor automatically expand tabs. And that leads to them getting it wrong! Try maintaining code written five years ago when the indentation varies between 2 and 9 spaces in the same file. It's not much fun.

    --
    "The invisible and the non-existent look very much alike." -- Delos B. McKown
  2. What about the wrong spaces? by Paul+Carver · · Score: 2

    One tab equals one indent. What does eight spaces equal? One indent? two? four?

    If you're writing code that no one else will ever see, then you can use whatever you prefer. What if someone who disagrees with you about indent level has to read your code?

    If a file contains tabs you can easily :se ts=4 or :se ts=2 to your preference. If a file contains sequences of spaces there's no easy way to change it to your preference. I suppose you could have a collection of patterns that search for sequences of 2, 4, or 8 and replace them with a different number of spaces, but that's a lot more complex than just setting your tabstop preference once.

    Spaces just don't seem practical if you have to share a file with someone whose preference is different from yours.

    (I use vim)

  3. 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

    1. Re:Tabs Permit Individual Preferences by danpbrowning · · Score: 2

      *exactly*. An editor that can't be flexible enough to make the code look like you want from tabs isn't a good editor.

      --
      Daniel
  4. spaces .. tabs are unpredictable by josepha48 · · Score: 2
    The truth is that I have always been told use spaces in my programs. I usually try to do so. You never know what a person has their tab set to. If someone has their tabs set to 4 and you set yours to 8 you can change the display of how a program will look in an editor. Then if some are using spaces then you have big mess. I'd recommend using spaces as a space is a space is a space and a tab is unknown....

    I don't want a lot, I just want it all!
    Flame away, I have a hose!

    --

    Only 'flamers' flame!

  5. The disk space argument by dmorin · · Score: 2
    I once worked at a place that required tabs, on the argument that code full of spaces took up too much space in the code management system.

    Same guy that made that decision went on to declare that indents should be *3*. Not 2 or 4.

  6. 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"

  7. Are you serious ? by OmegaDan · · Score: 2
    All I'm gonna say, is, if this is the big controversy around your office can I *PLEASE* work where you work? :)

    Where i work its always, "Umm, the boss just gave us until morning to ..."

    :)

  8. 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.

  9. 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
  10. I'm a vi junkie by decaf_dude · · Score: 2

    In my .vimrc I have "set tabstop=4" and "set expandtab" so the tabs are 4 spaces wide but are converted to spaces. I'm sure those using Emacs have a similar solution. I also use NetBeans for Java and it can also be set to do same things. In HTML and XML I like to use just 2 space indentation because nature of hierarchical ordering of the tags makes for a lot of wasted space if you use more than that. I've seen people use only 1 space indent, and it's not too bad either.
    -----

  11. 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!");
    }