Tabs "can be displayed completely differently on different types of systems and editors"?

970 views Asked by At

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.

2

There are 2 answers

2
Giedrius D On BEST ANSWER

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
Philip Kelley On

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.