how to become a really good Python programmer?

Richie Hindle richie at entrian.com
Thu Jun 17 14:59:25 CEST 2004


[Repost; apologies to anyone who sees this twice]

[Randall]
> As I write more and larger and complex programs, I need to 
> code better.  By better I mean clearer, cleaner, more efficient and 
> maintainable.

If you're not doing so already, start using Test-Driven Development.  Google
will give you any number of resources about that, but in a nutshell it means
writing unit tests *before* writing code, then writing the code to make the
tests pass.  This forces you to think about the design of your modules up
front, and usually leads to better-designed software.  The modules you write
will have cleaner interfaces and will be less dependent upon each other,
more clearly written, more reusable, and more amenable to changing without
breaking things.

The other important feature of test-driven development is that it's *hard*,
or at least it's hard for some application areas (like networking and GUIs).
There's nothing like working on a hard problem to sharpen your skills -
knocking up a messy script is easy, but you don't learn from it.

A concrete example of why it's hard and why it leads to better code: I have
a lightweight RPC system (as yet unpublished) that sends "safe pickles" over
the network to invoke functions on another machine.  The unit tests are a
mess, because they run the client and server socket code in different
threads in the same process, and synchronising them is painful.  Had I
thought more about it up front, I'd have realised that the RPC system should
be transport-independent, and the unit tests should provide a trivial
transport system for the RPC code to use during the tests, rather than
mucking about with sockets.  I'd have ended up with a nice
transport-independent RPC module with simple and complete unit tests, plus a
tiny transport module to implement a socket transport for it.  Done right,
the transport module would be so trivial that either it didn't need unit
tests, or the tests would be very simple.

A question for experienced TDD-ers: is TDD really more intellectually
demanding than code-first, or am I either a) doing it wrong, or b) stupid?

-- 
Richie Hindle
richie at entrian.com





More information about the Python-list mailing list