Exploration Through Example

Example-driven development, Agile testing, context-driven testing, Agile programming, Ruby, and other things of interest to Brian Marick
191.8 167.2 186.2 183.6 184.0 183.2 184.6

Sun, 25 Jan 2004

Code-reading practices

My first event in the Master of Fine Arts in Software trial run was a lecture on code-reading in the style of the literary critic Stanley Fish. His "affective stylistics" has one read a poem (say) word-by-word, asking what each word does for the reader. What expectations does it set up? or overturn? What if that word were omitted or moved elsewhere? (I've written on a similar topic earlier, and I drew my examples from that entry.)

I compared idiomatic Lisp code, "C-like" Lisp code, idiomatic C code, and Lisp-like C code to show how expectations and membership in "interpretive communities" influence readability. In the process, I learned something unexpected.

I presented code like this to Dick Gabriel, expecting he would think it an idiomatic recursive implementation of factorial.

(defun fact(n &optional (so-far 1))
   (if (<= n 1)
        so-far
      (fact (- n 1) (* n so-far)))

Note: because of my presentation's structure, I originally named the function f so as not to give away immediately that it was factorial. I don't think that's germane to this note, so I'm giving it the clearer name here.

He didn't think it was idiomatic, not really. He found it somewhat old-fashioned, preferring an implementation that replaces the optional argument with an internal helper function (introduced by the labels form).

      (defun fact (n)
        (labels ((f (n acc)
                   (if (<= n 1) acc (f (- n 1) (* n acc)))))
            (f n 1)))
    

Now, I always hated labels. What's the difference between Dick and me? It appears to be reading style. As I understand it from him, truly idiomatic Lisp reading style goes like this:

  1. Look for a key name (fact(n)).

  2. Quickly skip down to the code of maximum density.

               (if (<= n 1) acc (f (- n 1) (* n acc)))))
          
    That's the important code. If that's not clear, find the declarations that clarify it by scanning upward. The most important ones will be nearby.

The labels version of the code fits that. The reading style and writing style are "tuned" to each other. It does not fit my reading style, which is to read linearly through functions (though I do bounce around among functions). So the labels verbiage at the front slows me down. I expect the interior names to be more intention-revealing than they need to be when they're just placeholders to make interesting ideas invokable. Because I don't know the visual cues that say "Pay attention here!", I may do more memorization of facts that turn out to be unimportant.

It's arguable that my reading style is just flat-out worse, but I do think that tuning reading to writing is a more useful way to think about it.

All this may seem small, but it reinforces my idea that attending closely to the act of reading will yield some Aha! moments to improve our practice.

## Posted at 10:41 in category /mfa [permalink] [top]

About Brian Marick
I consult mainly on Agile software development, with a special focus on how testing fits in.

Contact me here: marick@exampler.com.

 

Syndication

 

Agile Testing Directions
Introduction
Tests and examples
Technology-facing programmer support
Business-facing team support
Business-facing product critiques
Technology-facing product critiques
Testers on agile projects
Postscript

Permalink to this list

 

Working your way out of the automated GUI testing tarpit
  1. Three ways of writing the same test
  2. A test should deduce its setup path
  3. Convert the suite one failure at a time
  4. You should be able to get to any page in one step
  5. Extract fast tests about single pages
  6. Link checking without clicking on links
  7. Workflow tests remain GUI tests
Permalink to this list

 

Design-Driven Test-Driven Design
Creating a test
Making it (barely) run
Views and presenters appear
Hooking up the real GUI

 

Popular Articles
A roadmap for testing on an agile project: When consulting on testing in Agile projects, I like to call this plan "what I'm biased toward."

Tacit knowledge: Experts often have no theory of their work. They simply perform skillfully.

Process and personality: Every article on methodology implicitly begins "Let's talk about me."

 

Related Weblogs

Wayne Allen
James Bach
Laurent Bossavit
William Caputo
Mike Clark
Rachel Davies
Esther Derby
Michael Feathers
Developer Testing
Chad Fowler
Martin Fowler
Alan Francis
Elisabeth Hendrickson
Grig Gheorghiu
Andy Hunt
Ben Hyde
Ron Jeffries
Jonathan Kohl
Dave Liebreich
Jeff Patton
Bret Pettichord
Hiring Johanna Rothman
Managing Johanna Rothman
Kevin Rutherford
Christian Sepulveda
James Shore
Jeff Sutherland
Pragmatic Dave Thomas
Glenn Vanderburg
Greg Vaughn
Eugene Wallingford
Jim Weirich

 

Where to Find Me


Software Practice Advancement

 

Archives
All of 2006
All of 2005
All of 2004
All of 2003

 

Join!

Agile Alliance Logo