Grouping code by indentation - feature or ******?

Antoon Pardon apardon at forel.vub.ac.be
Fri Mar 25 09:41:09 EST 2005


Op 2005-03-25, Carl Banks schreef <invalidemail at aerojockey.com>:
>
> Tim Tyler wrote:
>> What do you guys think about Python's grouping of code via
> indentation?
>>
>> Is it good - perhaps because it saves space and eliminates
> keypresses?
>
> 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).

That is not true. You can with the innermost levels, but it isn't
always that clear with intermediate levels.

> 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.

Structure/Disciplined programming is a burden in general. I have
never found putting braces or what ever delimiter such a problem.
I don't see people argueing that putting the right number of parenthesis
and or brackets is an extra burden.


> 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.

IMO the flow of a lot of code would be easier to follow if code
like "if condition: break" could be dedented, because such a
condition is really at the same level as the "while"

> 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.

Use a tool for that. If people want something in python that python
doesn't has, those people are refered to tools that provide it.
It it works in that direction, it should also work in the other
direction and people that would like some feature of python in
an other language should use a tool for that. You wnat consistent
style? Use a tool to put all your source in a consistent style.

> 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
> phased out.)
>
>
>> Or is it bad - perhaps because it makes program flow dependent on
>> invisible,
>
> 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.

Not with my own code. The difference is not great but my C programs
have a slight edge here.

>> 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.

Well to each his own.

-- 
Antoon Pardon



More information about the Python-list mailing list