[Tutor] Readabilty vs 80 characters

Peter Otten __peter__ at web.de
Fri Apr 20 12:14:10 CEST 2012


Russel Winder wrote:

> On Thu, 2012-04-19 at 16:00 +0200, Peter Otten wrote:
> [...]
>> Seriously, learn that you don't need backslashes if you put the
>> expression into parens, precalculate parts of the expression and put them
>> into temporary variables -- and if that doesn't suffice to keep the code
>> readable and below the 80 char threshold reread the part of the PEP with
>> the Emerson quote ("A foolish consistency...").
> 
> I agree that where there is a backslash in a Python program, there is a
> better layout that doesn't have it.  

> I wouldn't agree though that,
> necessarily, decomposing long expression using temporary variables
> improves anything. A good fluent API leads to function call chaining,
> far fewer variables, and far more readable code. Much of the time, not
> always, of course.

For the examples in 

http://www.dabeaz.com/generators/Generators.pdf

David Beazley uses

a = f(...)
b = g(a)
c = h(b)

rather than

c = h(g(f(...)))

I think this makes for very readable code, especially with well-chosen names 
for a and b. I remembered it because you mention "declarative" style and 
list comprehensions further down.

> There is currently a definite trend (*) away from 1960s FORTRAN style
> code structuring, to a 1980s Miranda (think Haskell and Scala) type
> thinking about how to structure code.  There is experimental evidence in
> psychology of programming that declarative expression leads to more
> easily comprehensible code. This isn't advocacy research as much of
> programming research has been. 

I still won't trust it one bit until I've seen what was measured and how.
If /Fortran/ is

"
go to the refrigerator, 
open it 
and bring the first bottle on the left
"

and /Haskell/

"get me a beer"

the results are both obvious and meaningless.

> Even C++ is going to a more declarative mode of expression, along with 
Java.  Python's list comprehensions fit into this trend.

New in Python 2.0, released in October 2000. For me the addition of 
generators was much more important.




More information about the Tutor mailing list