[ Content | View menu ]

Unpack the bag

6 Sep 2008

There’s a common pattern where an object is constructed with a Context object that carries all the dependencies the new object might need. It’s often used, for example, with classes that package up domain behaviour based on lower-level services, something like this:

public class PriceReconciler {
  private final Context context;

  public PriceReconciler(Context context) {
    this.context = context;
  }

  public boolean isAvailable(Item item, Price price) {
     Location location = context.getLocationFinder().findLocationFor(item);
     SKU sku = location.lookup(item);
     return context.getCurrencyConverter().compare(price, sku.cost()) < 0;
  }
 // and so on...
}
Mary Poppins

Over time the Context gets used in more and more places, and acquires more and more contents to carry around. It starts to feel like Mary Poppins’ carpet bag (there’s an old cultural reference) that contains a roomful of furniture. When everything is available everywhere then developers will use what they have in scope and, pretty soon, there are implicit dependencies all over the domain layer.

A while ago, I was working with a class that used one of these context objects. The class was quite large so, to help us understand it, my pair and I extracted the services it used into fields:

public class PriceReconciler {
  private final LocationFinder locationFinder;
  private final CurrencyConverter currencyConverter;
  private final LooseChangeCollector looseChangeCollector;

  public PriceReconciler(Context context) {
    this.locationFinder = context.getLocationFinder();
    this.currencyConverter = context.getCurrencyConverter();
    this.looseChangeCollector = context.getLooseChangeCollector();
  }
 // and so on...
}

Doing this made clear that the class used only three of the many services available on the context. More interesting, it showed that the looseChangeCollector was only used once, in a method that turned out to be referenced just once—by a class that also had access to the context. Our extraction highlighted that this behaviour was in the wrong place, it didn’t really have a relationship with the rest of PriceReconciler, so we moved it to its calling object and simplified the PriceReconciler. With just two dependencies, our next step was to set them directly in the constructor

public class PriceReconciler {
  private final LocationFinder locationFinder;
  private final CurrencyConverter currencyConverter;

  public PriceReconciler(LocationFinder locationFinder, 
                                CurrencyConverter currencyConverter) 
 {
    this.locationFinder = locationFinder;
    this.currencyConverter = currencyConverter;
  }
 // and so on...
}

which made the PriceReconclier just a little easier to reuse since we’d narrowed its dependencies.

Kent Beck wrote recently about this tension between being concrete and abstract parameters. There obviously isn’t a single answer, except to note that when two or three objects keep turning up together, there’s probably a missing intermediate concept where some of the behaviour really belongs. The trick, when turning that concept into a type, is to give it a meaningful name so that it’s hard to add features that don’t belong.

Coding - 3 Comments

Speaking in Oslo

4 Sep 2008

I’m speaking at JavaZone in Oslo, on September 17th. I was so impressed last year that I managed to get back in. The topic will be Listening to Test Smells, which Nat and I have been raising for a while.

I’ll only be there until mid-afternoon Wednesday, so get in touch early if you want to meet up, and I’ll miss the remarkable ClubZone evening when the conference overflows into downtown Oslo.

Events, News - 0 Comments

Asian trip. Anyone interested?

1 Sep 2008

I’ll be travelling for personal reasons to Beijing, Shanghai, Seoul, and Hong Kong in late October.

I’ve already been making enquiries, so if you’re interested in getting together then please drop me a line.

Personal - 2 Comments

Great teaching

25 Aug 2008

Here’s a story, A Teacher on the Front Line as Faith and Science Clash, from the New York Times about a teacher David Campbell doing a first rate job for his students. One of the interesting features of the story for me is how he had to work out how to get students committed to diametrically opposing views to open up to other possibilities, a direct challenge would just have alienated them. I’m not sure I have his skill or patience (actually, I’m sure I don’t).

Reading the background to the story reminds me of a line attributed (I think) to Romani Prodi that a nation can’t stay ignorant and rich for more than a generation.

via Andrew McAfee

Grumpy Old Man - 3 Comments

The cognitive neuroscience of magic

24 Aug 2008

A heavy title, but there are some great videos linked from this paper in Nature on “Attention and Awareness in Magic”, written by a team of scientists and top rate illusionists. Watch James Randi pull a fast one on Daniel Dennett, see if you can tell which is which…

via Neurophilosophy

Cool sites - 0 Comments

Great assistants help everyone

21 Aug 2008

Johanna Rothman has a nice post about why people with responsibilities need assistants. As she points out, most organisations have been stripping out administrative support except for the most exalted positions, which makes sense until one looks at the bigger picture. Is it really better that people who are supposed to be busy with activities that generate income for the company spend their time filling in orders for stationery?

One of my (multiple) rants is about my brief experience at Digital Corp’s late Systems Research Center. Their technical recruiting was pretty tough but so, I guess, was their recruitment for all the other staff. They had the best administration staff I’ve ever seen. They would perpetrate unprovoked acts of forethought and helpfulness when you weren’t looking, same with the system administrators.

This works on all sorts of levels. First, if you’ve just spent a great deal of effort recruiting top-class researchers, it doesn’t make sense to have them spend time battling the corporate bureaucracy. Second, and deeper, the ethos in the building was that stuff would just work, so the technical staff would not be diverted from their real jobs—and, by implication, there would be no excuses for not doing good work. In contrast, at another international research lab, it took me months to get my expenses paid because the relevant administrator could not figure out which way exchange rates worked (more Dollars than Pounds at the time).

Like many such systems, it’s hard to understand or believe how much of a difference getting things right makes until you’ve experienced it; I expect this is how it feels at a first-rate Toyota plant. The rest of us have to learn to cope with the Gumption Traps waiting for us in the “real” world. In the distance, we hear the Siren call of the Pragmatic Fix.

Organisations - 4 Comments

Bootstrap problem

18 Aug 2008

Today I found on the way to work that I’d forgotten my glasses so, rather than waste the day, I popped into a pharmacy to pick up some cheap reading glasses. The instructions for the viewer machine that figured out which strength to buy were in small print. Hmmmm.

Grumpy Old Man - 2 Comments

“Hammers considered harmful”

Here’s another post on the lines of: “Hammers considered harmful. Every time I use one, it strips the threads from my screws.” One of the clues is in the list of symptoms at the end of the first paragraph: “mammoth test set-ups”. The tests were complaining but not being heard.

In truth, we’ve done a dreadful job of explaining where interaction-based techniques are relevant and where they aren’t. I keep bumping into codebases that are supposed to be written that way but where the unit tests have baroque, inflexible setups because the team weren’t listening to the tests. I even saw Lasse Koskela, who knows what he’s doing, during a programming demo at the recent Agile Conference, slip into writing expectations for a simple clock object that should have just returned a series of values; J.B. Rainsberger, being more forthright than me, called him on it.

Romilly, one of my partners in crime collaborators, once said he was surprised when he started working with Nat and me, how simple our unit tests are and how few expectations we set. That degree of focus is one of the points we try to get across whenever we talk about our approach. I find that the best use of interaction-based testing is to nudge me into thinking about objects and their relationships, not as a single solution for all my TDD needs.

In the meantime, we’re working on making our ideas more accessible.

Test-Driven, Agile Programming - 3 Comments

Another reason for licensing programmers

12 Aug 2008

Stuart Wray has posted a draft of an interesting paper on How does Pair Programming Work?. I can’t judge all the psychological claims, but many of the points appeal to my confirmation bias.

In “Mechanism 3”, Wray makes a link between code-n-fix programming and a form of “operative conditioning” otherwise known as gambling. The academically respectable quotation comes from Gleitman et al.:

In a [Variable Ratio] schedule, there is no way for the animal to know which of its responses will bring the next reward. Perhaps one response will do the trick, or perhaps it will take a hundred more. This uncertainty helps explain why VR schedules produce such high levels of responding in humans and other creatures. Although this is easily demonstrated in the laboratory, more persuasive evidence comes from any gambling casino. There, slot machines pay off on a VR schedule, with the “reinforcement schedule” adjusted so that the “responses” occur at a very high rate, ensuring that the casino will be lucrative for its owners and not for its patrons.

This matches some of my experience programming, and is a pretty good explanation of some of the behaviour I see around me: the continual hope that the next fix will be the right one, the explosions of frustration when something doesn’t work, and the endless sessions. We’ve even strengthened the addictiveness with our taste for darkened rooms with flashing lights (like casino floors), and our highly responsive IDE’s.

Wray hypothesises that Pair Programming may help to manage this addiction by keeping us honest. I suspect that Test-Driven Development also helps by smoothing the flow, removing the Variable Ratio effect, and, as I wrote before, by breaking us out of the programming Tar Pit long enough to refocus on the real goal.

Perhaps we programmers should be licenced, not for the sake of our unlucky customers but to preserve our own health.

Software culture, Test-Driven - 1 Comments

XpDay London Keynote speakers

1 Aug 2008

We’re very pleased to announce our keynote speakers for this year’s London XpDay.

Chris Ambler, European QA Director for games company Electronic Arts, tests some of the most complex software in the world

Daniel Jones and Marc Baker, Lean Enterprise Academy, are two of the most established Lean consultants in the world. Jones wrote, with James Womack, wrote “The Machine That Changed the World”, the standard book on the Toyota Production System.

There’s still time to propose your session.

Events, News - 0 Comments