OooOOooOOo Magic Python Powers OoooOooOooOO

chromatic on 2007-09-10T17:03:12

It is hard to write unreadable code in python.... You can still have meaningless variable names, bad encapsulation, functions that are too long, tight coupling, and poor/no documentation, and no tests. However if you compare a program written in python to a program written in some other language, the python program will not be less readable unless effort was put into making the python program less readable.

Python's Magic Powers of Readability in the Face of Barely-Competent Monkeys

I think I'll credit this one as "I hate dollar signs and luuuuuurve prefix and postfix underscores, nanny-nanny-boo-boo."

Of course my favorite line is "You should learn some other programming languages." I counted how many languages I've programmed in to get Perl 6 out the door the other day... and stopped at ten.


It's about program design, stupid!

brian_d_foy on 2007-09-10T17:41:44

I find poor program design more of a factor on readablilty than syntax issues. At the microscopic level of syntax, it's very easy to see what is going on, and that's in any language. It's something you can just look up.

At the higher levels, the how and why of things fitting together and how the data needs to be passed around, most people seem to just attach things with whatever pipe fittings they have close by, rather than using some consistent, overarching philosophy.

On the other side are overly-absract frameworks where even understanding the design ins't enough because none of the abstract framework really points to specific application tasks.

Design is a readability problem no matter which language it is. Syntax is trivial.

Re:It's about program design, stupid!

chromatic on 2007-09-10T17:49:42

Design is a readability problem no matter which language it is.

That's why I prefer to talk about "maintainability" rather than "readability". It shifts the discussion away from syntax to other factors which actually matter.

Second order effects of language syntax

ziggy on 2007-09-10T21:31:29

When I was working in the Great White North, I was working with a few Perl programmers that were thrown onto a Python+Zope project for the oddest of reasons. They were smart. They picked up Python without any difficulty. They learned how Zope wanted them to write and structure web apps, eventually.

After a few months on the project, they had some interesting insights into the different world views of Perl and Python. A moderately experienced Perl programmer can skim over a random piece of Perl code and immediately tell if it's total crap (a la Matt's Script Archive), intentionally obfuscated, unintentionally obfuscated (influenced by misguided style guides), moderately good, well-written and crystal clear, seriously clever for good reasons, or drunken code. I think that pretty much covers the gamut.

On the other hand, Python is intentionally and proudly optimized to be readable by beginners. Therefore, any Python programmer of even the most basic skill level should be able to read any Python program (if not completely understand the underlying algorithm and data structures). The unintentional side effect is that a moderately experienced Python programmer needs to read, rather than skim, each and every Python program he picks up to determine if it's total crap, moderately good, well-written and crystal clear, clever for good reasons, or written while drunk.

Since then, I've heard stories of projects like Chandler, where the Python code is seriously unpythonic, and written in the early days by C++ programmers who coded as if Python were an interpreted dialect of C++. In these cases, it seems that it's even more difficult to discern what a Python program does, since you need to read more deeply into the code to get to the moment when you ask yourself what on Earth were they thinking? before you reach enlightenment, trace through the execution paths before seeing how to rewrite ~1000 lines of C++-in-Python with ~30 lines of more reasonable, idiomatic Python.

There is no 'good' or 'bad' here. There are just choices with desired properties and unintended consequences.

Re:Second order effects of language syntax

chromatic on 2007-09-10T22:21:35

Great thoughts, as usual Ziggy.

Therefore, any Python programmer of even the most basic skill level should be able to read any Python program (if not completely understand the underlying algorithm and data structures).

Without being able to understand the program's design in the large or small, is being able to read the program even interesting? I can't think of a case where it is. Am I missing something?

Re:Second order effects of language syntax

ziggy on 2007-09-11T18:27:14

Without being able to understand the program's design in the large or small, is being able to read the program even interesting?
I think you're approaching the problem from the wrong direction.

Given a new program and no previous understanding of how it works, you could read the program and build an understanding from there. Consider the first time you approached a full set of basic binary tree primitives, or a bubble sort. It may have been the first time you came across these concepts, but if you could read the code, you could understand what was going on without too much difficulty.

Now, consider the first time you came across code for a balanced binary tree, an AVL tree, or quicksort. There probably wasn't enough information there for you to understand the algorithm completely, either in terms of how it worked or how it behaved (i.e. time/space performance).

For a more pragmatic example, consider any of the programs from Matt's Script Archive. You may not understand what it is trying to do, but you can read it. If you've got more than a passing background in Perl, you'll probably find at least a dozen howlers before you understand the program. For a more mundane example, consider a program that passes everything through a magic regex. If you can read the regex, you can determine that it is a buggy RFC 822 matcher, and should probably be replaced, because no simple regex can email addresses. If you can't read the regex, then this is just a piece of cargo-cult code with magical properties.

Reading a program without prior understanding is interesting because it is a gateway to gain that understanding. It's a necessary precondition, but not a sufficient one.

I can't think of a case where it is. Am I missing something?
Guido's right that readability is an important property, because if you can't read a block of code, you have no hope of understanding it. Consider quicksort written in APL or befunge. You may understand quicksort, but you wouldn't be able to find and fix a bug. Even Tony Hoare himself would have trouble.

I won't speak for Guido, but a plethora of anonymous and interchangeable Pythonistas cite "readability" as if it were identical to "understandability". If this were the case, then Python indeed would have magic pixie dust, Chandler wouldn't be years behind schedule, and frameworks like Zope and Twisted wouldn't be so horrifically unapproachable.

Re:Second order effects of language syntax

Aristotle on 2007-09-11T00:31:05

The unintentional side effect is that a moderately experienced Python programmer needs to read, rather than skim, each and every Python program he picks up to determine if it’s total crap, moderately good, well-written and crystal clear, clever for good reasons, or written while drunk.

That’s how I feel about Java. Regardless of how crappy the code, it always looks reasonable on a microscopic level.