View Poll Results: Do you like editors that default to tabs or to spaces?

Voters
40. You may not vote on this poll
  • Go tabs!

    18 45.00%
  • Go spaces!

    22 55.00%

Tabs or Spaces

This is a discussion on Tabs or Spaces within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I would have to say that one of my biggest pet peeves in programming is when I use a text ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    Tabs or Spaces

    I would have to say that one of my biggest pet peeves in programming is when I use a text editor or an IDE that defaults to inserting "tabs" by inserting a certain number of spaces, instead of inserting an actual tab character. It is incredibly annoying.

    Why? Because I tend to switch between editors, computers, and OS's quite often. I could be programming on my Windows XP machine at home using either Visual Studio, SciTE, or Code::Blocks, and then later that same day I could be on campus using a Linux machine using and using Kate as my editor...all while editing the same program or application.

    It is annoying to switch between two different editors, and to have to account for how those editors insert their tabs! For example, Visual Studio, SciTE, and Code::Blocks ALL insert some random amount of spaces instead of actual tab characters. You can specify in the IDE's settings to have it insert tab characters instead...but usually you are too late. Usually you have already coded some amount of code before you remember to turn on that setting. Often times you have to turn on that setting EACH time you open the IDE (like with SciTE)...which is really annoying...I believe it is the same with Code::Blocks as well.

    So then...going from SciTE or Code::Blocks, which insert spaces, I move over to a Linux machine and open up Kate, and it inserts tabs instead! Of course now all the indentation is going to be off, because half of the code was done using one editor, and the other half using the other editor...and so the code looks like a complete mess now!

    If all editors would just insert tab characters instead of their own pre-defined amount of spaces, it would make life so much easier. You could open your code in any editor, and the indentation would all be the same. It would be nice, neat, and organized.

    Go tab characters! Why on earth do so many editors default to using spaces?
    My Website

    "Circular logic is good because it is."

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    It's usually the other way around. People prefer spaces because they go back and forth between editors and tabs on some editors are the equivalent of four characters, but other editors have them as 2 or 8. Putting spaces means that the code will line up the same everywhere you go.

    However, I tend to prefer tabs, mostly because I generally use the same editor (MSVC++ which defaults to tabs for me) and I otherwise can change the settings to make 4-character tabs on any new editor I switch to. I use arrow keys to maneuver inside code and I moving left three tabs is much easier than moving left 12 spaces.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Spaces all the way man!.

    Sooner or later, you'll come across some tool or other (for example, a message board) which makes a complete mess of handling tabs.

    Or it will display tabs, but only in the default width of 8 spaces.

    Your smart code editor with tabs set at 4 spaces may indent both these to the same level
    <s><s><t><t><s><s>printf("hello");
    <t><s><s><s><s><s><s>printf("world");
    But as soon as you post it on a message board, voila, instant garbage
    Code:
      		  printf("hello");
    	      printf("world");
    We see this kind of stuff all the time, people using random mixtures of both post their nice code and we all see puke.

    Spaces are perfect for indentation, pure vanilla, tastes the same everywhere.

    An example from work.
    One well edited file I recall recently used tabs all over the place. Part of the file made sense if you set the tab stops to 2, another part 4 made more sense and still other places it seems that 8 made the most sense - OMG.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    But that is exactly my point. When one editor inserts "4 spaces" for tabs, and another editor inserts "2 spaces" for tabs...all you see is puke.

    you will see something like:

    Code:
    // Check if the database is available.
    if (!$dbm->isConnected()) {
    <s><s>print 'Unable to access the database at this time.<p>Sorry for the inconvenience.';
    <s><s>exit;
    }
    else print 'Connected!';
    
    if ( !isset($_SESSION['dp_account']) )
    <s><s><s><s>$_SESSION['dp_account'] = new Account();
    Those two if statements are indented differently because two editors choose to handle tabs differently. If both would insert tabs:

    Code:
    // Check if the database is available.
    if (!$dbm->isConnected()) {
    <t>print 'Unable to access the database at this time.<p>Sorry for the inconvenience.';
    <t>exit;
    }
    else print 'Connected!';
    
    if ( !isset($_SESSION['dp_account']) )
    <t>$_SESSION['dp_account'] = new Account();
    Everything is neat! Everything is indented the same!

    Now obviously each editor can choose individually, or let the user choose, how much white space there actually is per "tab character"...but my point is that the actual characters inserted into the file should be tabs in order to prevent all the confusion.
    My Website

    "Circular logic is good because it is."

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    That's why you set the IDE to insert a specific number of spaces for tabs (rather than using the default)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Once again we return to my point: I use, as probably most of you do, more than one editor and IDE. I switch between them often. Many of these editors require you to set those certain settings (like SciTE) each time you open the editor, which is rather annoying. They don't always remember your previous settings. Let each editor insert tabs into the file (it can display however much whitespace it/the user wants as long as it inserts a physical tab), and all will be nice and neat.
    My Website

    "Circular logic is good because it is."

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    I generally stick to spaces for indentation and formatting of code. However, when it comes to the default, I do not care, since I always check the settings before coding on an editor that I have not yet configured.

    Everything is neat! Everything is indented the same!
    Consider this code snippet:
    Code:
    std::transform(a.begin(), a.end(),
                   b.begin(),
                   foo());
    It so happens that "std::transform(" is 15 characters. Suppose you use tab characters, and set the size to appear as 4 spaces. To indent "b.begin()" and "foo()" such that they line up with "a.begin()", one might use 3 tabs, followed by 3 spaces. Now, once we switch to an editor where the tab size is different, they no longer line up.

    One way to solve this problem is to restrict tab characters to indent levels, and then use spaces for additional formatting. This was what I used before I had to submit code for university modules, upon which I decided that I could not trust the marker to change his/her editor settings for tabs to match mine

    Note that this also means that you have to be very careful not to misplace your tabs while editing existing code, which can happen since both spaces and tab characters are whitespace. For example, you could use an editor that has the option of showing whitespace to distinguish between spaces and tab characters. I suspect that as a programming team grows larger, it becomes more difficult to ensure that everyone is as careful, and so it may be easier to just enforce an "all spaces" rule.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Well laserlight, your case is an exception. In such a case I would honestly probably just hold down the space bar until the right spacing was acquired.

    For the large majority of cases we are indenting if-statements, loops, function bodies, and class definitions. In those cases it is highly annoying to move from editor to editor with different spacing rules. I can't believe so many of yall use pure spaces...they frustrate me to no end because the indentation is always off. I have noticed that most Windows editors/IDEs use spaces by default, and most Linux editors/IDEs use tabs by default.

    [edit]
    Case Study:

    Even though we are sitting here on a C/C++ forum, lets imagine we are programming in Python, which is parsed and defined purely by spaces.

    Using "spaces" could easily confuse the Python interpreter...and indeed it has been done. Take two interpreters, one that uses 4-space tabs, and the other that uses 2-space tabs.

    Take the following code:
    Code:
    class MyClass:
    	def myFunction ( ):
    		print 'Hello'
    		print 'Goodbye'
    There is a class declaration on line 1. Line 2 contains 1 tab, and a function definition. Lines 3 and 4 contain two tabs, and code statements.

    Lets say for example that the first two lines were done using editor 1, and the second two lines using editor 2, it would end up looking like this:

    Code:
    class MyClass:
    <s><s><s><s>def myFunction ( ):
    <s><s><s><s>print 'Hello'
    <s><s><s><s>print 'Goodbye'
    This would break the interpreter. Using tabs:

    Code:
    class MyClass:
    <t>def myFunction ( ):
    <t><t>print 'Hello'
    <t><t>print 'Goodbye'
    [/edit]
    Last edited by DavidP; 04-05-2007 at 12:39 PM.
    My Website

    "Circular logic is good because it is."

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    In those cases it is highly annoying to move from editor to editor with different spacing rules.
    I note that "spaces by default" is as effective a standard as "tab characters by default".

    I can't believe so many of yall use pure spaces...they frustrate me to no end because the indentation is always off.
    How can the indentation be off? The bad thing about the "pure spaces" approach is that you have to delete all those spaces instead of just a tab character, and that the file size is larger. The indentation is never off, even in the "exception" that I described.

    I have noticed that most Windows editors/IDEs use spaces by default, and most Linux editors/IDEs use tabs by default.
    Notepad and Notepad++ use tab characters by default, as does MS Visual C++ 2005 Express, if I remember correctly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Notepad and Notepad++ use tab characters by default, as does MS Visual C++ 2005 Express, if I remember correctly.
    Notepad does use tabs. Visual C++ as far as I know defaults to spaces...I haven't verified that, but I assume that because the tabs never line up when I bring it into VIM or Kate, which both default to tabs.

    How can the indentation be off?
    See my above posts...especially the case study above. I am not talking about when you strictly use one editor, because then all is conformed to that one editor. I am talking about a multi-editor situation...which I find myself often in, as most of you probably do as well.
    My Website

    "Circular logic is good because it is."

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> Visual C++ as far as I know defaults to spaces.
    I have used three different versions of Visual C++, and I have never specifically changed this setting. It always uses tabs for me.

    I do change keyboard and layout settings to VC++ 6.0 style, so it is possible that doing so changes from tabs to spaces, but otherwise it defaults to tabs for me.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Using "spaces" could easily confuse the Python interpreter...and indeed it has been done. Take two interpreters, one that uses 4-space tabs, and the other that uses 2-space tabs.
    I note that PEP 8 recommends:
    "Use 4 spaces per indentation level."

    See my above posts...especially the case study above. I am not talking about when you strictly use one editor, because then all is conformed to that one editor. I am talking about a multi-editor situation...which I find myself often in, as most of you probably do as well.
    Ah, but I could just as easily state that: I can't believe so many of yall use tab characters...they frustrate me to no end because the indentation is always off.

    The problem lies in the mix of spaces and tab characters in ways that cause indentation to be misaligned. It does not lie in either the "pure spaces" or a tab character approach per se.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>like SciTE<<

    You can set it once in SciTEGlobal.properties - I can't remember exactly where, but the various 'tab' settings under 'indentation' are the ones to fiddle around with; check out the SciTEDoc.html file in your installation directory which should provide explicit information.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    When I first started, I picked tabs. I thought it was inane to have to hit space 12 times versus hitting the tab key three times. Besides, I didn't -want- the option to perform a non-consistent amount of indenting.

    As I got to know the ropes on my editor, the difference between tabs and spaces grew more transparent with auto-indenting. Tabs was still better, IMO. When I wanted to un-indent a line by 1 shift, I hit backspace once, it unindented.

    Before long, I found out my editor could just retroactively re-indent the whole file whenever I want, so the whole issue is now moot. When a file's mis-indented (usually not due to tabs), I just type in 4 characters and everything's perfect.
    Callou collei we'll code the way
    Of prime numbers and pings!

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I note that "spaces by default" is as effective a standard as "tab characters by default".
    Not exactly true.

    "Spaces by default" represents about half a dozen standards, each of which is inconsistent with each other...
    2 Spaces per indent
    3 Spaces per indent
    4 Spacer per indent
    &ct...

    If I edit a file written by someone who used 5 spaces per indent, I either have to change how I typically indent for just that file, or completely re-indent the entire file.

    "Tabs by default" is unambiguous, and lets different programmers choose their amount of indenting without physically modifying the entire source.
    Callou collei we'll code the way
    Of prime numbers and pings!

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 11:51 PM
  2. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 01:42 PM
  3. Replies: 5
    Last Post: 06-30-2003, 12:52 PM
  4. tabs to spaces with dev c++ v.4
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 01-28-2003, 01:07 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21