Preserving program structure

Peter Wang pzw1 at hotmail.com
Fri Dec 14 03:13:28 EST 2001


"David Dawkins" <david_j_dawkins at spamless.hotmail.com> wrote in message news:
> So I'm using IDLE, and Alt-F5 tells me I have a problem with
> indentation.
> It seems I had (inadvertently) mixed tab and space characters.
> No amount of "tabbifying" and "untabbifying" resolved the issue,
> so I had to shift *everything* to the left-margin, and re-create
> the indentation with a consistent scheme (8 space hard tabs).

IDLE really seems to have a problem with this.  it seems that no
matter what you have selected in the options ("Use tabs"/"Use
spaces"), when it autoindents, it automatically matches the tabbing
with the block of code you are in.  one thing you can do is turn on
"show whitespace", which you show you exactly where the suspect spaces
or tabs are.

so although i liked the syntax highlighting, i switched back to using
UltraEdit (under windows).  vi under linux, of course.  one of the
nice features of ultraedit is the "convert all leading space to tabs"
(and vice versa) function.

> While doing so, I stuffed up a lot of the program logic, because
> it wasn't always obvious which block a particular statement
> belonged in.  This dismayed me intensely.

yes, i can imagine...

> How do experienced Python programmers avoid this problem?

well, there are several layers to your problem.

Layer -1: you always back up your code using source control, so you
never totally F yourself if, for example, you strip out all the tabs
and hit Ctrl-S inadvertently. :)

Layer -2: use an editor you are really comfortable with, or find one
and learn its ins and outs, so you don't get hit with nasty surprises.

Layer -3: pick an indentation scheme and stick with it.  at my
company, we settled on (IMHO) a good scheme, where leading indentation
for setting out code block level use tabs, and all indentation or
spacing meant to line things up beyond that (e.g. for continuation
lines, or end-of-line comments) are done with spaces.[1]  that way we
maximize productivity across different people who need different tab
widths to best read code and still preserve legibility of continuation
lines.

> I don't see much code out there with comments like "# endif", etc.

no, that really wouldn't be a good thing because it violates the
"Don't Repeat Yourself" principle.  by not indenting the next block,
you've already told your human and computer audiences that the block
has closed.  an additional delimiter would be at best redundant and at
worst erronous and confusing.  if you really feel compelled to set out
a certain block of code, put in a blank line before/after it to
visually separate it.

-peter


[1]  the unfortunate thing is that tabs and spaces are
cross-overloaded.  tabs are good for delimiting blocks, but they can
be overloaded to be simple substitutes for spaces.  spaces are good
for lining things up pretty, but are bad for block delimiting because
different people will frequently have legitimate reasons for
wanting/needing different widths for block delimiting.  (the
frequently-used argument that "spaces are just as easy for delimiting
blocks if you use a sensible editor" is sort of moot because not
everyone has a sensible editor.)  people will frequently think that
only tabs or only spaces should be used to fill both needs, and come
out with very religious opinions about them.



More information about the Python-list mailing list