The story of advancers
The story of advancers
Submitted by Brian Marick's blog on Thu, 19/08/2004 - 22:44.An author wrote a story for Better Software about the usefulness of complex tests. I was struck by a conversation I'd once had with Ward Cunningham about how his team came up with Advancers. Isaw a nice way to complement a test design article with code designcontent. So I wrote a sidebar. In the event, it didn't get used, so I'm putting it here.
Complex tests can find bugs in complex code. What then? Usually, theresult is an unending struggle against entropy: a continuous effort tofix bugs in unyielding code, hoping that each fix doesn't generateanother bug. Once upon a time, Ward Cunningham was mired in entropy,but what happened next makes an unusual story.
His team was working on a bond trading application. It was to have twoadvantages over its competition. First, input would be morepleasant. Second, users would be able to generate reports on aposition (a collection of holdings) as of any date.
The latter proved hard to do. Many bug fixes later, one method inparticular was clearly a problem. It was the one that advanced aposition to a new date by processing all related transactions. It hadgrown to a convoluted mess of code, one that proved remarkably hard toclean up.
The solution was to convert the method into a Method Object. You canfind a fuller description of method objects in Martin Fowler's fineRefactoring, but the basic idea goes like this:
Suppose you have a big method that contains many interrelatedtemporary variables. You can't untangle it by extracting littlemethods because each little method would use temporaries also used inother little methods. The need to coordinate sharing of variablesmakes it too hard to see any underlying structure.
Therefore, turn the method into an object, one that has a singlemethod - perhaps called
compute. Code that used to call the originalmethod will have to make one of the new objects and call itscomputemethod.What does this gain you? Now you can change temporary variablesinto instance variables of the object. Since these are automaticallyshared among any methods of the object, you can now extract littlemethods without worrying about coordination.
As the little methods get extracted, the object's structure andresponsibilities become clearer. Clearer code means fewer bugs.
It's common to treat method objects as just a coding convenience. ButCunningham's team found themselves treating this one as a designtool. They gave it a name - Advancer - that sounded like one from thedomain (though none of the domain experts had a correspondingnotion). Once Advancers were part of their design vocabulary, thinkingabout how to satisfy a new requirement meant, in part, thinking aboutwhether a special kind of Advancer might fit nicely. By changing theway they thought about the domain, the team was able to write bettercode faster.
Advancers later helped out in another way. The program calculated taxreports. What the government wanted was described in terms ofpositions and portfolios, so the calculations were implemented byPosition and Portfolio objects. But there were always naggingbugs. Some time after Advancers came on the scene, the team realizedthey were the right place for the calculation: it happened thatAdvancers had instance variables that contained exactly theinformation needed. Switching to Advancers made tax reports tractable.
It was only in later years that Cunningham realized why taxcalculations had been so troublesome. The government and traders haddifferent interests. The traders cared most about their positions,whereas the government cared most about how traders came to havethem. It's that latter idea that Advancers captured, but conversationswith domain experts couldn't tease it out - even tax experts didn'tknow how to express it that way. It only came out through aconversation with the code.
In some circles, it's said that programmers + the code + rules forcode cleanliness are smarter than programmers alone. That is,programmers who actively reshape or mold the code as it changes willcause new and unexpectedly powerful design concepts to emerge. Thestory of Advancers shows massaging the code as a learning tool.
