Grouping code by indentation - feature or ******?
invalidemail at aerojockey.com
Fri Mar 25 15:15:30 CET 2005
Tim Tyler wrote:
> What do you guys think about Python's grouping of code via
> Is it good - perhaps because it saves space and eliminates
It's good, but this is only a minor reason.
The reason this is good is because it exactly reflects the way human
beings mentally group code in their heads. In Python, you can eyeball
grouping with 100% accuracy (except for one flaw that's being phased
out). Not so with other languages. In other languages, you have two
simultaneous ways of grouping code: the way that makes sense to humans
(indentation), and the way (braces or begin/end). This creates the
possibility of mismatch, and it puts an extra burden on the programmer
to make sure computer and human grouping stays synchronized.
Grouping by indentations also goes a long way to prevent sloppiness.
No matter how sloppy your style is, you can't slop away the way the way
program was nested in Python; thus a reader should be able to follow
the flow of just about any code. I've ended up as the computer expert
at my engineering firm, so I get (non-Python) code to debug from time
to time, and I can attest that inconsistent style is the single worst
thing that everyone does to make code unreadable. Python-like
indentation would instantly halve that problem.
The drawbacks are insanely minor. It makes one liners more difficult,
and can result in transmission difficulties with some stupid programs
that helpfully strip leading whitespace. (There's another drawback in
Python that isn't inherent to grouping by indentation, namely the
possibility of mixing spaces and tabs. This is the flaw that's being
> Or is it bad - perhaps because it makes program flow dependent on
It doesn't. I suspect Pythonistas will heavily attest to that. But,
as I said, it does make it virtually impossible for sloppy code to
mislead you about the flow. Overall, if someone hands you a random
piece of C code, and a random piece of Python code, you will be more
likely to easily follow the flow of the Python.
> and unpronouncable characters - and results in more
> manual alignment issues by preventing code formatters from managing
This is true. Most common complaint is changing the nesting level of a
block of code. Good editors have ways to cope with this, just as good
editors have ways to cope with all these superfluous braces in other
> Python is certainly pretty unorthodox in this area.
> How would you have dealt with the issue of how to group statements?
Having experienced Python, I can say pretty earnestly that, were I
designing my own language, there is no other way I would do it.
Grouping by indentation is a slam dunk for me.
More information about the Python-list