Tabs versus Spaces in Source Code

P

PoD

PoD a écrit :

Actually, who said you had to always use the same number of spaces to
indent ? 12 = 6 + 6 = 4 + 4 + 4 but also 12 = 2 + 10 = 1 + 1 + 3 + 3 + 4 :D

Thus supporting my assertion that space indenting is implicit not
explicit. Spaces are evil.
No, it could be 3 levels or 3 tabs per level or 2 tabs for the first
level and 1 tab for the second ...

Could be but wouldn't be.

Maybe what Python should do (but never will given the obsession with using
spaces) is only allow one level of indentation increase per block so that

def foo():
<TAB><TAB>return 'bar'

would return a syntax error
 
D

Duncan Booth

PoD said:
I think it is universally accepted that mixed tabs and spaces is indeed
**EVIL**

I should have said any code using tabs exclusively.
Can you point at any significant body of publically visible Python code
which uses tabs exclusively? All of the Python projects I've ever been
involved with use spaces only as a convention (although as I pointed out in
my previous post, some with more success than others).

The problem with conventions such as 'tabs only' or 'space only' is that
they only work if everyone sticks to the conventions, and it helps if the
same conventions are in place everywhere (otherwise people forget when they
switch from one project to another). Also, in the open source universe you
are quite likely to pull in bits of code from other projects, and you don't
want to either have to reformat it or to switch your editor settings for
some files.

My experience of programming with either spaces or tabs has taught me
that tabs are evil not for themselves, but simply because no matter how
hard you try they always end up being mixed with spaces.

Do you know of any open-source projects which actually try to enforce a
'tab only' convention for Python? I'd really like to see a similar scan
over some 'tab only' code as I did over Plone to see whether they actually
manage to remain 'pure'.
 
C

Christophe

PoD a écrit :
Maybe what Python should do (but never will given the obsession with using
spaces) is only allow one level of indentation increase per block so that

def foo():
<TAB><TAB>return 'bar'

would return a syntax error

Which would make <TAB> mandatory for indentation. What about some
freedom of choice ?
 
S

Sybren Stuvel

Duncan Booth enlightened us with:
Can you point at any significant body of publically visible Python
code which uses tabs exclusively?

Everything Python at http://www.stuvel.eu/software
Also, in the open source universe you are quite likely to pull in
bits of code from other projects, and you don't want to either have
to reformat it or to switch your editor settings for some files.

If I grab a module, I just leave the module as is. If I grab a code
snippet, I always reformat it to my own style. That's very easy using
VIM's "retab" command.
Do you know of any open-source projects which actually try to enforce a
'tab only' convention for Python?

My software is, although I'm still the only one working on them ;-)

Sybren
 
D

Dave Hansen

On 19 May 2006 07:18:03 GMT in comp.lang.python, Duncan Booth

[...]
My experience of programming with either spaces or tabs has taught me
that tabs are evil not for themselves, but simply because no matter how
hard you try they always end up being mixed with spaces.

That's been my experience as well. At least on projects with more
than one programmer. And more than once with single-programmer
projects where the programmer changed or updated his editor in the
middle...

Regards,
-=Dave
 
P

PoD

PoD a écrit :

Which would make <TAB> mandatory for indentation. What about some
freedom of choice ?

Hey, if people are allowed to say that tabs should be banned, then I'm
allowed to say they should be mandatory ;)
 
P

Peter Decker

On 19 May 2006 07:18:03 GMT said:
Can you point at any significant body of publically visible Python code
which uses tabs exclusively? All of the Python projects I've ever been
involved with use spaces only as a convention (although as I pointed out in
my previous post, some with more success than others).

Dabo. http://dabodev.com
 
C

Christopher Weimann

My experience of programming with either spaces or tabs has taught me
that tabs are evil not for themselves, but simply because no matter how
hard you try they always end up being mixed with spaces.

Swap the word 'tabs' for the word 'spaces' and you get...

My experience of programming with either tabs or spaces has taught me
that spaces are evil not for themselves, but simply because no matter how
hard you try they always end up being mixed with tabs.

Which is just as vaild as the un-swapped paragraph. Both versions
express a bias. The first is biased in favor of spaces. The second is
biased in favor of tabs. Neither have any useful content. Mixing is bad
but that fact doesn't favor spaces OR tabs.
 
R

Roedy Green

Actually, spaces are better for indenting code.

Agreed. All it takes is one programmer to use a different tab
expansion convention to screw up a project. Spaces are unambiguous.

Ideally though you should run code through a beautifier before checkin
to avoid false deltas with people manually formatting code slightly
differently.
 
A

Andy Sy

achates said:
Yeah - we've got to the repeating ourselves stage.

Actually a couple of the responses on this newsgroup
have settled the question for me. I did learn something
new by engaging in this holy war.


Tabs need not be evil, but ONLY if they are used in one
particular way:

If you really must use tabs, use them *ONLY* for 'semantic'
indentation and use pure spaces when you need arbitrary
indentation (e.g. arbitrary spacing).

PHP Example
===========

function xyz() {
->print "This is a really really really long line that I'd ".
-> "like to extend to the one down below while preserving ".
-> "ease of readability by aligning the start of the string".
-> "lines. I use tabs to reflect syntactical (e.g. semantic) ".
-> "indentation, but use spaces for arbitrary indentation.\n\n".;
->while (10) {
->->print "This code will align properly no matter what tabsize setting ".
->-> "the reader uses, and is the only real benefit of using tab ".
->-> "characters instead of pure spaces.\n\n";
}



I did some basic tests, and it looks like this style should also
work for Python code.


THIS IS THE *SINGLE* CORRECT WAY TO USE TABS IN CODE! ANYTHING
ELSE WILL BE A PAIN FOR PEOPLE WITH TABSIZE SETTINGS DIFFERENT
FROM YOURS!!

But that's the problem with this issue: it's really hard to get the
space-indenters to actually think about it and to address what is being
said. Every time it comes up, there's always a few people trying to
explain why tabs give are a good idea, facing a whole raft of others
spouting stuff like:

Most of the tab-indenters have the same attitude. But I did get ALL
of my points against tabs addressed this time. And thank you to those
people including you for the 'less -x<tabstop>' tip.


But unfortunately the situation is worse than that: tab indentation
needs to be actively defended.

A world where everyone uses pure spaces would be far far far better
than the current situation where tab users can't even decide what
the universal tabsize should be and whose code alignment breaks
on different tabsize settings.

However, a world where EVERYONE who uses tabs strictly practice the
ONE TRUE TAB WAY would be a *slightly* better place than one where
everyone used pure spaces.

And remember, the ONE TRUE TAB WAY does not come for free and involves
a certain amount of tedium and attention. It will be impractical on
most simple text editors. For most people, I frankly don't think the
minor benefits are worth it.


Unlikely perhaps. I hope so. It's a cruel irony that Python's creator
didn't appreciate the benefits that tab indentation would bring to his
own language - the only major language in which indentation levels
actually have semantic significance.

You might want to run those benefit/s/ by me again, because the SINGLE
benefit I can still see to using tabs is so that people who have
different indentation width preferences can view code according to
the way they want. And remember, this benefit will ONLY occur
IF people stick to using the ONE TRUE TAB WAY outlined above.

Any other method of tabbing would just be worse than a pure-spaces
world.
 
C

Christophe Cavalaria

Christopher said:
Swap the word 'tabs' for the word 'spaces' and you get...

My experience of programming with either tabs or spaces has taught me
that spaces are evil not for themselves, but simply because no matter
how hard you try they always end up being mixed with tabs.

Which is just as vaild as the un-swapped paragraph. Both versions
express a bias. The first is biased in favor of spaces. The second is
biased in favor of tabs. Neither have any useful content. Mixing is bad
but that fact doesn't favor spaces OR tabs.

The difference is that you cannot code without spaces but you can do it
without tabs.
 
A

Andy Sy

Andy said:
Actually a couple of the responses on this newsgroup
have settled the question for me. I did learn something
new by engaging in this holy war.


Tabs need not be evil, but ONLY if they are used in one
particular way:

If you really must use tabs, use them *ONLY* for 'semantic'
indentation and use pure spaces when you need arbitrary
indentation (e.g. arbitrary spacing).

PHP Example
===========

function xyz() {
->print "This is a really really really long line that I'd ".
-> "like to extend to the one down below while preserving ".
-> "ease of readability by aligning the start of the string".
-> "lines. I use tabs to reflect syntactical (e.g. semantic) ".
-> "indentation, but use spaces for arbitrary indentation.\n\n".;
->while (10) {
->->print "This code will align properly no matter what tabsize setting ".
->-> "the reader uses, and is the only real benefit of using tab ".
->-> "characters instead of pure spaces.\n\n";
}



I did some basic tests, and it looks like this style should also
work for Python code.


THIS IS THE *SINGLE* CORRECT WAY TO USE TABS IN CODE! ANYTHING
ELSE WILL BE A PAIN FOR PEOPLE WITH TABSIZE SETTINGS DIFFERENT
FROM YOURS!!

Also... remember that the 'ONE TRUE WAY' essentially involves *mixing*
tabs and spaces for indentation with all the objections that that
entails... (although like mentioned above, it should work with Python,
at least in the simple cases i've tried)

Frankly, the case for tab usage is not that compelling...
 
E

Edward Elliott

Andy Sy wrote:

[snipped 50 lines of previous message]
Also... remember that the 'ONE TRUE WAY' essentially involves *mixing*
tabs and spaces for indentation with all the objections that that
entails... (although like mentioned above, it should work with Python,
at least in the simple cases i've tried)

Frankly, the case for tab usage is not that compelling...

Quoting usenet posts is like hunting buffalo: only take what you need.
http://www.xs4all.nl/~wijnands/nnq/nquote.html#Q2
 
X

Xah Lee

the following are 2 FAQ following this thread. Thanks.

Addendum: 2006-05-15

Q: What you mean by embeding tab position info into the source code?
How's that gonna be done?

A: Tech geekers may not realize, but such embedding of meta info do
exist in many technologies by various means because of a need. For
example, Mac OS Classic's resource fork and Mac OS X's bundling system,
unix shell script's shebang (#!), emacs and Python's encoding
declaration “#-*- coding: utf-8 -*-â€, Unicode's BOM, CVS's
change-log insertion, Mathematica's source code system the Notebook,
Microsoft Word's transparent meta data, as well as HTML and XML's
various declarations embedded in the file. Some of these systems are
good designs and some are hacks.

Somehow tech geekers have the sense that “source code†must be a
plain text file containing nothing else but the programing code. This
may be a defendable position, but as we can see in the above examples,
this idea is primitive and does not address the various needs. If the
tech geekers have thought out about these issues, computing languages
and its source code may have developed into more powerful and flexible
integrated systems as the above standardized examples. For instance,
many commercial development systems actually already have such
meta-data embodied with the source code. (e.g. Borland Delphi,
Metrowerks's CodeWarrior, Microsoft Visual Studio, Wolfram Research's
Mathematica.) Some of which, not only embody development-related info
such as debug points or linking files, but also allow programers to
high-light code for visual purposes like a word processor, or even
display them visually as type-set mathematics.

Q: Converting spaces to tabs is actually easy. I don't see how spacess
lose info.

A: Here is a illustration on how it is not possible to convert spaces
to tabs. Suppose you are writing in a language where the indentation is
part of the semantics, not just for appearance. Now, suppose you have
these two lines:

1234567890
A
B

The first line has 2 space prefix and second line has 4 space prefix.
How, if you convert this to tabs, how do you know that's 1 and 2 tabs,
or 2 and 4 tabs? In essence, there is no way to tell how many tabs n
represents, where n is the smallest space prefix in the code, unless n
== 1.

The above demonstrates the information loss in using spaces for
indentation in a theoretical way. There are also practical problems. In
practice, many languages allow string literals like this myName="i love
you", and strings easily can have a run of spaces. One cannot simply
run a blind find-n-replace operation to replace all spaces to tabs. But
also, many unix languages contains a so-called construct of
“heredoc†as a mean to embed a literal block of text. For example,
here's a PHP construct of heredoc:

$novelText = <<<arbitraryCharsHereAsDelimiter
(__)
(oo)
/-------\/
/ | ||
* ||----||
~~ ~~
arbitraryCharsHereAsDelimiter;
}

Regardless of its design as a language construct, the purpose of
“heredoc†is that it allows programers to easily embed a text (a
large string), without worrying about the text containing sequence of
characters that may be meaningful to the language. If a language has
heredoc construct, then it is basically impossible to convert from
spaces to tabs, as that will botch literal string embedded in heredoc.
However, it is less of a problem to convert tabs to spaces, because the
frequency of spaces appearing in literal strings are far higher than
literal tabs.

Another practical issue is error recovery. Suppose, one uses 4 spaces
for a indentation. Now, it is not uncommon to see lines with odd number
of space prefixes such as 7 or 10 out of common sloppiness. Such error
would happen more often if spaces are used for indentation, and the
essence is that tabs enforce a semantic association and is impossible
to make a half-indentation.

Q: Well, i just like spaces because they are most compatible.

A: Sure, crass simplicity is always more compatible. Suppose a unixer
will say, he doesn't like HTML because it is fret with problems and
incompatibilities. He'd rather prefer plain text. And, indeed, a lot
unixers seriously think that.

---------------------------
PS in the answer to the first question, i gave the following examples
of IDE/Language that actually embed formatting info in the source code:
Borland Delphi, Metrowerks's CodeWarrior, Microsoft Visual Studio,
Wolfram Research's Mathematica

actually, i know Mathematica does, but i'm not quite sure about the
other examples. So, my question is, does any one knows a language or
IDE that actually allows the coder to manually highlight parts of the
code and this highlight stick with the file upon reopening, as if a
word processor?

Xah
(e-mail address removed)
∑ http://xahlee.org/
 
M

Mumia W.

Xah said:
the following are 2 FAQ following this thread. Thanks.

Addendum: 2006-05-15

Q: What you mean by embeding tab position info into the source code?
How's that gonna be done?

A: Tech geekers may not realize, but such embedding of meta info do
exist in many technologies by various means because of a need. For
example, Mac OS Classic's resource fork and Mac OS X's bundling system,
unix shell script's shebang (#!), emacs and Python's encoding
declaration “#-*- coding: utf-8 -*-â€, Unicode's BOM, CVS's
change-log insertion, Mathematica's source code system the Notebook,
Microsoft Word's transparent meta data, as well as HTML and XML's
various declarations embedded in the file. Some of these systems are
good designs and some are hacks.

Vim's mode-lines do this too.
Somehow tech geekers have the sense that “source code†must be a
plain text file containing nothing else but the programing code. This
may be a defendable position, but as we can see in the above examples,
this idea is primitive and does not address the various needs. If the
tech geekers have thought out about these issues, computing languages
and its source code may have developed into more powerful and flexible
integrated systems as the above standardized examples.

The tech geekers have thought about it. Donald Knuth invented TeX, and
went on to invent the WEB literate programming system. You don't get any
geekier than that :)
> For instance,
many commercial development systems actually already have such
meta-data embodied with the source code. (e.g. Borland Delphi,
Metrowerks's CodeWarrior, Microsoft Visual Studio, Wolfram Research's
Mathematica.) Some of which, not only embody development-related info
such as debug points or linking files, but also allow programers to
high-light code for visual purposes like a word processor, or even
display them visually as type-set mathematics.

Q: Converting spaces to tabs is actually easy. I don't see how spacess
lose info.

A: Here is a illustration on how it is not possible to convert spaces
to tabs. Suppose you are writing in a language where the indentation is
part of the semantics, not just for appearance. Now, suppose you have
these two lines:

I'd say that such a language removes the choice of whether to use tabs
or spaces, and the discussion is over when you don't have a choice.
1234567890
A
B

The first line has 2 space prefix and second line has 4 space prefix.
How, if you convert this to tabs, how do you know that's 1 and 2 tabs,
or 2 and 4 tabs? In essence, there is no way to tell how many tabs n
represents, where n is the smallest space prefix in the code, unless n
== 1.

vim: tabstop=4

The argument for spaces over tabs says that you have to include some
metadata in order for the document to look right on other people's
computers if you use tabs. This example, plus my example mode-line for
vim, reinforces that idea IMO.
The above demonstrates the information loss in using spaces for
indentation in a theoretical way. There are also practical problems. In
practice, many languages allow string literals like this myName="i love
you", and strings easily can have a run of spaces. One cannot simply
run a blind find-n-replace operation to replace all spaces to tabs. But
also, many unix languages contains a so-called construct of
“heredoc†as a mean to embed a literal block of text. For example,
here's a PHP construct of heredoc:

$novelText = <<<arbitraryCharsHereAsDelimiter
(__)
(oo)
/-------\/
/ | ||
* ||----||
~~ ~~
arbitraryCharsHereAsDelimiter;
}

Yes, there are lots of situations like this where you can't just
willy-nilly convert between tabs and spaces. But even in this case shows
that, if you use consistent tab widths, the text has a chance of
surviving. I converted your little doggie to and from text with tab
sizes of eight, and he survived. (I did it with tabs set to four too,
and it worked.)

Regardless of its design as a language construct, the purpose of
“heredoc†is that it allows programers to easily embed a text (a
large string), without worrying about the text containing sequence of
characters that may be meaningful to the language. If a language has
heredoc construct, then it is basically impossible to convert from
spaces to tabs, as that will botch literal string embedded in heredoc.

Yes it would. Upon printing, if the terminal tab width was set to eight,
but the text conversion was done with tabs at four, bye bye doggie.
However, it is less of a problem to convert tabs to spaces, because the
frequency of spaces appearing in literal strings are far higher than
literal tabs.

Another practical issue is error recovery. Suppose, one uses 4 spaces
for a indentation. Now, it is not uncommon to see lines with odd number
of space prefixes such as 7 or 10 out of common sloppiness. Such error
would happen more often if spaces are used for indentation, and the
essence is that tabs enforce a semantic association and is impossible
to make a half-indentation.

What I've learned is that, if I'm going to use tabs for indentation, I
have to be consistent.
Q: Well, i just like spaces because they are most compatible.

A: Sure, crass simplicity is always more compatible. Suppose a unixer
will say, he doesn't like HTML because it is fret with problems and
incompatibilities. He'd rather prefer plain text. And, indeed, a lot
unixers seriously think that.

---------------------------
PS in the answer to the first question, i gave the following examples
of IDE/Language that actually embed formatting info in the source code:
Borland Delphi, Metrowerks's CodeWarrior, Microsoft Visual Studio,
Wolfram Research's Mathematica

Perl's POD and Java's javadoc do it too.
actually, i know Mathematica does, but i'm not quite sure about the
other examples. So, my question is, does any one knows a language or
IDE that actually allows the coder to manually highlight parts of the
code and this highlight stick with the file upon reopening, as if a
word processor?

Xah
(e-mail address removed)
∑ http://xahlee.org/

I'm slowly moving into the "spaces" camp. After reading your earlier
post on tabs vs. spaces and other people's responses, I began thinking
about why I like tabs so much, and there is only one answer--backspace.

If I use tabs, when I backspace I go back to the previous tab position,
which is what I want. With spaces, I have to hit the backspace key
several times to get back. That's it--one feature is the only reason I
like tabs, so I decided to investigate vim's features to see if vim
would let me backspace to the previous tab position with one keystroke.

'Softtabstop' (sts) is the feature. I would have never thought to look
for this feature without your post. Thanks again Xah.

Your posts are on topic, informative, engaging and necessary. Keep them
coming Xah. :)
 
O

Oliver Wong

Jonathon McKitrick said:
How do you even *enter* these characters? My browser seems to trap all
the special character combinations, and I *know* you don't mean
selecting from a character palette.

à¿¿ hey, this is weird...

î

I've got something happening, but I can't tell what.

Yes, I'm an ignorant Western world ASCII user. :)

What OS are you using? In Windows XP, you'd have to let the XP know that
you're interested in input in languages other than English via "Control
Panel -> Regional Settings -> Languages -> Text Services and Input
Languages". There, you'd add input methods other than English. Each "input
method" works in a sort of unique way, so you'll just have to learn them.
For example, under English, you can use the "keyboard" input method which
probably is what you're using now, or the "handwriting recognition" input
method, or the "speech recognition" input method to insert english text.
There are other input methods for the Asian languages (e.g. Chinese,
Japanese, etc.)

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top