| Store | Cart

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

From: Carl Banks <inva...@aerojockey.com>
25 Mar 2005 06:15:30 -0800
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).  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
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.


> and unpronouncable characters - and results in more> manual alignment issues by preventing code formatters from managing> indentation?

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


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


-- 
CARL BANKS

Recent Messages in this Thread
Tim Tyler Mar 25, 2005 08:11 am
Peter Otten Mar 25, 2005 08:48 am
Skip Montanaro Mar 25, 2005 12:52 pm
Tim Tyler Mar 26, 2005 03:30 pm
Antoon Pardon Mar 25, 2005 12:56 pm
John Roth Mar 25, 2005 01:22 pm
Antoon Pardon Mar 25, 2005 02:26 pm
Antoon Pardon Mar 29, 2005 07:57 am
Antoon Pardon Mar 29, 2005 07:47 am
Roy Smith Mar 29, 2005 01:35 pm
Kent Johnson Mar 25, 2005 01:51 pm
Carl Banks Mar 25, 2005 02:15 pm
Antoon Pardon Mar 25, 2005 02:41 pm
Diez B. Roggisch Mar 25, 2005 03:02 pm
Antoon Pardon Mar 25, 2005 03:16 pm
Diez B. Roggisch Mar 25, 2005 03:33 pm
Antoon Pardon Mar 29, 2005 07:34 am
Diez B. Roggisch Mar 29, 2005 09:43 pm
Rocco Moretti Mar 25, 2005 05:21 pm
Tim Roberts Mar 26, 2005 09:03 am
Peter Otten Mar 26, 2005 10:29 am
Kent Johnson Mar 26, 2005 12:09 pm
Skip Montanaro Mar 26, 2005 12:41 pm
Peter Otten Mar 26, 2005 07:02 pm
Kent Johnson Mar 27, 2005 02:43 am
Peter Otten Mar 27, 2005 05:42 am
Reinhold Birkenfeld Mar 27, 2005 05:08 pm
projecktzero Mar 25, 2005 02:43 pm
Larry Bates Mar 25, 2005 04:15 pm
Ivan Van Laningham Mar 25, 2005 04:39 pm
James Stroud Mar 25, 2005 07:31 pm
Robert Kern Mar 25, 2005 07:38 pm
Bill Mill Mar 25, 2005 07:47 pm
Larry Bates Mar 25, 2005 09:56 pm
Jeremy Bowers Mar 26, 2005 05:50 am
Ron_Adam Mar 26, 2005 05:36 pm
Mike Meyer Mar 26, 2005 10:52 pm
Terry Reedy Mar 25, 2005 06:42 pm
Paul McGuire Mar 26, 2005 06:13 am
Javier Bezos Mar 26, 2005 09:02 am
Tim Tyler Mar 26, 2005 03:42 pm
Jeremy Bowers Mar 26, 2005 11:17 am
Jacob Lee Mar 27, 2005 07:00 am
Javier Bezos Mar 27, 2005 03:30 pm
Reinhold Birkenfeld Mar 27, 2005 05:11 pm
Javier Bezos Mar 27, 2005 06:04 pm
Myles Strous Mar 30, 2005 06:08 am
Myles Strous Mar 30, 2005 06:13 am
Javier Bezos Mar 30, 2005 06:00 pm
Javier Bezos Mar 28, 2005 06:05 pm
Reinhold Birkenfeld Mar 27, 2005 05:10 pm
Scott David Daniels Mar 27, 2005 09:41 pm
Terry Reedy Mar 30, 2005 01:55 am
Tim Roberts Mar 30, 2005 06:48 am
James Stroud Mar 26, 2005 11:36 pm
Terry Reedy Mar 25, 2005 06:41 pm
Messages in this thread