6

While reading arguments against the use of Tabs, I came across this (source):

...it can be a good idea to avoid tabs alltogether, because the semantics of tabs are not very well-defined in the computer world, and they can be displayed completely differently on different types of systems and editors.

I am relatively new to programming, and have never experienced any issues with tabs in my code, and I've used a number of editors including Notepad++, Programmer's Nodepad, Gedit, Kate, Sublime Text, etc. I may not have done enough coding to get to that point, hence the question:

Can someone please explain, in simple terms, what the quote states? And is the problem with tabs still relevant?


Please note that I am not asking you whether I should use tabs or spaces in my code. I am only after a rational explanation for a specific argument against tabs that I've come across.

6
  • 1
    Note to downvoters: Please care as much to comment.
    – its_me
    Dec 4, 2014 at 17:38
  • An explanation of #2 in this page has some good information about how tabs are not uniformly treated the same.
    – admdrew
    Dec 4, 2014 at 17:38
  • @admdrew Yes, I've read that and various others. I one I quoted in the question is the "simplest" (to understand) that I'd find, and I am still unable to make sense of it.
    – its_me
    Dec 4, 2014 at 17:41
  • I think the most basic answer is right up in your quote: "displayed differently on different systems". Tabs can be inconsistent between systems, but spaces are not.
    – admdrew
    Dec 4, 2014 at 17:42
  • @admdrew I find that ambiguous at best. Without any clear explanation it seems like a hasty attempt to put the argument to rest. I mean, "how" exactly are they displayed differently on different systems, other than depending on the size of tabstop configured by the user?
    – its_me
    Dec 4, 2014 at 17:45

2 Answers 2

8
+100

I assume you want to see some examples so bellow I've listed some of the most common.

Problem #1: Tab width is not consistent

This is for "displayed completely differently on different types of systems and editors" part.

Even assuming all systems and editors (that your code will be displayed on) agrees on same tab semantics: "move to the right until the current column is a multiple of N", the N is arbitrary.

Historically "standard" for this N is 8 but now most people configure their editors it to be 4 or 2 to "look better".

This is where tab width inconsistency problems come from.

I will use tab widths 2 and 8 in my examples to make differences more visual but same applies to other widths as well.

Indent

Lets say someone is using tab width: 2 in their editor. They see the code like this:

class Foo:
  def doSomething(a):
    if test(a):
      // some nice comment
      // about this
      bar(a)

Now someone else reads this code in say terminal that uses tab width 8. They see the code like this:

class Foo:
        def doSomething(a):
                if test(a):
                        // some nice comment
                        // about this
                        bar(a)

That someone may see this as not very pleasant.

Align

So far we have seen inconsistency in indent. But some people like to also align code e.g. assignments, comments.

Again with tab width 2:

class Foo:
  def do_something(a):
    if test(a):
      foo     = a.foo     // some nice comment
      foo_bar = bar(foo)  // about this
      bar(a)

Again someone else reads this in environment where tab width is 8. Lets say they need to post this snippet to the web and uses <pre> tag. Browsers by default use "standard" tab width 8 and the code looks like this:

class Foo:
        def do_something(a):
                if test(a):
                        foo                     = a.foo                 // some nice comment
                        foo_bar = bar(foo)      // about this
                        bar(a)

They can't post it as is. They have to modify the code to replace tabs to spaces.

Line length

Most coding standards define maximum line width.

Lets take max line width 80 for example.

Someone using tab width 2 may see this code completely standard conforming. For them the longest line width is 74 (visible width as opposed to line length in bytes which is 72).

class Foo:
  def do_something(a):
    // Some very nice comment about code bellow using more then few words.

Someone else using tab width 8 (in a terminal for example) will see the same line as non-conforming because now longest line width is 86:

class Foo:
        def do_something(a):
                // Some very nice comment about code bellow using more then few words.

Since tab width is inconsistent, line with is now inconsistent too.

Problem #2: Tab is not same thing everywhere

This is for the "semantics of tabs are not very well-defined" part.

So far we assumed that everyone uses tab character as "move to the right until the current column is a multiple of N".

But in some contexts tab character may be used for something a bit different. For example in word processors tab means "move to the the next tab stop", where tab stops are completely arbitrary (and most likely not event the same width).

For example lets say someone is writing a document that uses tab stops:

Document with tab stops

Now lets say they need to paste some code snippet in it. If code is using tabs, following happens:

Code in document with arbitrary tab stops

They can't leave it as is. They have to modify the code to replace tabs to spaces.

Conclusion

As you can see tabs in different contexts may render code from slightly to completely unreadable.

Spaces have none of the above problems.

2
  • 1
    Don't want to start the religious war here, but the ability for every user to customise his/her preferences is probably the largest advantage of tabs; you should not use tabs for alignment, but for indentation, there are very little effective downsides. Yes, it looks different, but that's the whole point! For Ruby, for example, people use 2 spaces; which is often too little for me in heavy nested Ruby blocks, and I would like to set it to 4; but I can't because it's spaces, and not tabs! I wrote a VimScript to do that, but a tab would be a lot easier/more universal, IMHO... Dec 16, 2014 at 11:12
  • Although you've made a fair point, Martin, you must also take into consideration the spacing being displayed differently when reviewing code. If it's part of a team's convention to indent using 4 spaces, and a reviewer opens the code somewhere (say, GitLab) and it looks like it's indented using 8 spaces, you're guaranteed questions will be raised.
    – Daniel
    Sep 7, 2020 at 7:52
1

I like @Giedrius’ answer (+1), and am going to toss in some vastly over-simplified history just because.

What is a space? It is a blank stretch between letters used to identify words.

How big is a space? It depends on the letters—specifically, on the font. There are thousands of fonts, and they can all be tagged as either proportional or non-proportional. In proportional fonts, character width varies (compare i and M); in non-proportional fonts, character width is always the same (compare i and M). How big is a space? It’s as big as whoever designed the font determined it should be. (Typesetting has been around since Guttenberg, which means the typesetting industry has had a few centuries to figure what works and what doesn’t.)

Skipping a few hundred hears, probably relevant stuff happened that I don’t know about.

Along comes the typewriter (hey kids, ever actually seen one?) These allowed you to type out anything you want to. However, to make it work (totally mechanical, all gears and pulleys, no electricity required, but not cool enough for Steampunk) every character has to be the same width, including the spaces.

Free-form words are all nice and well, but quite frequently people wanted to type up numbers in nicely formatted columns—such as for invoices, where you list items followed by their costs lined up by decimal point with a total at the bottom. (Can you imagine doing that by hand? Congratulations, you just imagined my summer job waaay back when. << Insert Liquid Paper reference here.>>) To make this feasible, someone came up with the TAB key. If you do X on the typewriter, it would remember that “hey, there’s a tab stop here”, and if you hit tab and had not yet reached that column on the row you were typing the print head would jump to that point. How big was it? You guessed it, it depended on what the user needed. (What "X" was totally depended on the typewriter manufacturer.)

Thus and so: no real-world correlation between tabs and spaces, unless there's something obscure from the linotype industry. On typewriters a tab stop would line up with a “regular” character position, whereas on computers there’s no such limitation.

2
  • 1
    Almost all, if not all, programming tools use a monospace font; so space/letter/tab size is not much of an issue here, all characters have the same width, so, by definition, a tab is a multiple of n spaces. Dec 16, 2014 at 11:14
  • 1
    Not so. Microsof'ts Visual Studio allows you to select any font installed on your computer (buried under Tools/Options), and it seems likely that many other development environments would as well. Thing is, most programmers are intelligent, and would never try to program in Garamond, Old English, or (shudder) Comic Sans. Dec 16, 2014 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.