March 07, 2010

More than functional

I listened to Episode 10 of the Pluralcast podcast the other day, in which David Starr discusses "Reading Code" with Alan Stevens:

A few weeks ago at the MVP Summit in Redmond, I was fortunate enough to run into Alan Stevens, a man my wife refers to as “your southern-gentleman-vegan-hippy friend.” Alan shared with me some new ideas he has around the act of writing (songs or literature) as a metaphor for good practices of software development. The basic idea is that we write a line of code once, and read it many times. Why aren’t we optimizing for the reading part?

The discussion they had struck close to home with me, as it resembled discussions my wife and I have occasionally. She is a fiction writer, and we commonly discuss her craft and how it relates to my craft of software development. In the podcast, Alan Stevens talks about his process of writing code in a way that resembles how he writes prose.

rough draft
With prose, it's pretty standard practice to write a first-draft with the purpose of just getting ideas down on paper and providing a basic structure, knowing that this draft is never meant to be used or seen by anybody other than the author. The author with then go through and make edits, adding, removing, and changing content, producing more drafts along the way, until, much later in the process, a (semi) final product is revealed. Then, that product will be looked at by an editor or two, and more changes will be made before publication. It's understood that a first-draft is almost never publishing quality, and it's not expected to be able to write a non-trivial product in one go. That's just not how our brains work.

When we write code, however, we seldomly venture past the first draft. We write the code for a module or a feature, verify it's functional, and then we pack away the code with no intention of ever changing it again. I mean, it works, right? There's no such thing as a "first draft". Occasionally there might be a code review session which will result in some edits. But really, how often does that happen?

As it turns out, we always have to come back and read it again sometime. We all know that the code we write spends most of its lifetime in maintenance mode, but we tend to write it with an eye towards functionality only, forgetting about readability. As a result, when we come back to read our code later, we have to spend an inordinate amount of time rebuilding its context. Time we may not have had to spend if we have spent some time moving beyond the purely functional level.

Lately, when I write code, I tend to consider my first run-through as a first-draft, like prose. Even if it ends up working perfectly on the first build, I always intend to make a couple more passes over the code with an eye toward readability. I don't expect to write anything correctly the first time through, even if all my unit tests pass on the first try. I have to think about how I or others will see the code when we read it again 6 months, a year, or (aaagghh!) 3 years from now.

If you find this correlation between writing code and writing prose to be interesting, then you should give the podcast a listen.

No comments:

Post a Comment

Please leave a comment. You can use some HTML tags, such as <b>, <i>, <a>