Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
×
Programming IT Technology

Spaces vs. Tabs? 32

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?
This discussion has been archived. No new comments can be posted.

Spaces vs. Tabs?

Comments Filter:
  • To untab code with the 4-space-tab convention I use the following script:
    #!/bin/csh
    if (-e $1) then
    &nbsp &nbsp mv $1 $1-backup
    &nbsp &nbsp expand -4 $1-backup &gt $1
    else
    &nbsp &nbsp echo "untab: file not found"
    endif
    I have a similar script with an unexpand instead of expand to retab a file. Feel free to replace the -4 with other values (or to use unexpand and expand directly).
  • 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.

  • 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)
  • 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
  • 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!

  • Think of it this way--it is easier to see at a glance all the brackets the way the previous poster uses them, as they line up--you can tell which one go together just by their position. Not that I am saying yours or his is better, just that their are justifications for both--don't be so harsh!
  • 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.

  • With tabs, we can all set our editor preferences as we please, and freely work in each other's wakes without reformatting,

    That only works for the first column. What about those things lined up on the right?

  • Don't collaberate much? Try diff'ing and getting something useful.
  • Using spaces is fine, unless you expect other people to edit your code. If everyone uses tabs ("tab" being an general (in that tab stops can be set to _anything_!) way to specify an indent!) then everyone can be happy.

    The clincher is that _you_have_to _learn_to_use_your_friggin_tool!_ If you don't know how to get lpr, less, or vi to show your indentions how you want, then you should RTFM!
  • "3" what? 3 tabs?
  • Torvalds cant code. Open Source wannabe. Fully. Anyone who thinks having an if inside a tight loop (count that, function indentation, for indentation, second for indentation (thus the tight loop) and if then indentation makes 4) is bad obviously has never actually had to write a bit of code to do anything more complicated than triviality.
  • My team at work decided on a certain coding standard that we liked, and I was going to run GNU indent on all of the horrible code (C and C++) to make it more readable. When GNU indent was through with it, it wouldn't even compile.

    Not only that, but GNU indent has no way to indent with spaces instead of tabs.

    Summary: GNU indent is bad.

  • by po_boy ( 69692 ) on Friday April 13, 2001 @06:39PM (#292433)
    JWZ's perspective is at Tabs versus Spaces: An Eternal Holy War. [jwz.org] It's not too bad.

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

  • but that begs the question, what happens when you deindent? do you hit backspace 8 (or 4) times, or does your editor remember that those couple spaces should be treated as one text object?

    trying not to start another holy war in the same thread, but what editor do you use?
    --
  • In x86 assembley 12 space tabs is the best, (this way your operands always line up)...

    Point being, choice of tab size really depends on the langauge :)

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

    :)

  • by cperciva ( 102828 ) on Friday April 13, 2001 @06:59PM (#292437) Homepage
    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.
  • *exactly*. An editor that can't be flexible enough to make the code look like you want from tabs isn't a good editor.
  • by danpbrowning ( 149453 ) on Friday April 13, 2001 @07:52PM (#292439)
    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.
  • at my work, my boss made the policy tabs, with exactly one tab before any comments that happen to be on the same line as code. this way everyone writes the same code, and they can setup their editors to use their preferred number of spaces.

    html (php included) has no policy however, so everyone that has to do any of that gets to deal with idiot code. i tend to put all of my html/php onto one single line. i wish i could do the same for c.....

    .brad


    Drink more tea
    organicgreenteas.com [organicgreenteas.com]
  • Although I've never seen it carried out, I've thought that one way to handle this is to indent however you yourself want to, and use scripts to translate the source back and forth between your preference and whatever the 'standard' is. That way, you can let the neo-fascist boneheads^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^ H project managers define whatever they want and not have it perturb your wa.
  • 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.
    -----
  • It seems to me like tabs are the best way to let everyone have their own preference. I know this can lead to wierd formatting later on, so why not just stick a big fat comment at the top to say what tab size was used to make the file and will make it look like the author saw when he was writing it?
  • by satch89450 ( 186046 ) on Saturday April 14, 2001 @02:16PM (#292444) Homepage

    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!");
    }
  • I use nedit [nedit.org], with the "emulated tabs" option turned on. That way, when I press tab, nedit inserts however many spaces I want instead. It'll even delete the same groups of spaces as if they were tabs - if you do it right after you create them.

    The feature works well enough that I'm rarely annoyed by having to deal with many space characters rather than single tab characters. Besides, if I ever want to change the indentation of a block of code, I can just highlight it all and indent/deindent it all at once. So, spaces or tabs make little difference.

    I'm sure other editors can do this, but if they didn't, I'd probably use tabs and put a comment at the beginning of the code to indicate what the tab setting was when the document was created. (It's no fun having to figure that out at some later date.)

  • Ugh.
    When I'm doing php, I do it like this:
    <!-- html code --> <?php

    //php code ?> <-- html code -->
    With the php code indented. Different strokes for different folks...
  • The problem with indent is that it fscks up C++, especially when templates get into the picture (all those angle brackets mess it up). FOr me that makes it a definite nonstarter...
  • JWZ's perspective is at Tabs versus Spaces: An Eternal Holy War. It's not too bad.

    No, it's merely shortsighted and moronic.

    It all comes down to an elaborate defense of the shortcomings of his preferred editing platform.

    The fact is, tabs are the One True Way. Any other approach condemns the entire development team to using the same indentation, despite the deleterious effects it may have on their work (I have a really hard time with large indentation like 8 spaces or so, and many other people have a hard time with the 2 spaces that I prefer). With tabs, we can all set our editor preferences as we please, and freely work in each other's wakes without reformatting, screen-squinting, or other hassles.

  • I'm a tabs person.

    Back in my C days, I used tabs set to three. Now I use four. In the legacy code I occaissionally pull up where some idiot went through and converted my tabs to (3) spaces, it's a mess.
    --
  • What rules might one use to display (convert per session) spaces to tabs or vice versa? How many spaces == a tab? Do you convert all spaces or only the ones on the left of a line.. bah bah bah..

    Tabs serve a different purpose to spaces. When indenting use tabs. When spacing use spaces. The only reason for spacing in is stupid editors with queer indentation.

  • I use Delphi for a lot of my editing, even if I'm not doing Pascal. I have indent sent to 2, so hitting the tab key inserts two spaces. With auto-indent everytime you start a new line it indents over to where the previous line starts. Hitting backspace will move to the left, automatically aligning with the last block up, repeat as needed. Works for me.

    To my surprise I found that delphi understands C syntax and does highlighting if the file suffix is ".c".

BLISS is ignorance.

Working...