Chapter 22: The Delegate from Troy


Paul Friedman drove to Ann Arbor from Troy to pair with me. After some pleasant food and chat, we attacked some code that has been bugging me for a long time. Using delegates, we reduced duplication and learned a lot.

Finding a Pair and Something to Do

Back in November, I posted to the extremeprogramming list that Chet s newfound gainful employment was making it hard for us to pair, offering to pair with anyone who wanted to meet me in Ann Arbor or vicinity. Paul Friedman allowed as how he would give it a try. After a holiday break, we finally got together last night at Grizzly Peak, where my son is brewmaster.

Paul and I chatted a bit, had a couple of GP s special pizzas, and then sat down to look over the code and find something to do. I showed Paul some of the XML and XSLT tests I had written over the past couple of days, and then we looked at some code that I considered to be the most problematical in the system, in the XMLNotepad form itself.

I ve been saying since the early days that I m not comfortable with the breakout of responsibility between the form and the TextModel. And worse , there s duplication in the code. Let s look at the duplication. The menu item handlers and the KeyDown handler all duplicate some code lines:

 public void XMLKeyDownHandler(object objSender, KeyEventArgs kea) { 
model.SetLines(textbox.Lines); model.SelectionStart = textbox.SelectionStart;
if (kea.KeyCode == Keys.Enter && kea.Modifiers == Keys.None) {
model.Enter();
kea.Handled = true;
}
else if (kea.KeyCode == Keys.Enter && kea.Modifiers == Keys.Shift) { model.InsertReturn();
kea.Handled = true;
}
PutText(textbox, model.LinesArray(), model.SelectionStart);
}

void MenuInsertSection(object obj, EventArgs ea) {
model.SetLines(textbox.Lines); model.SelectionStart = textbox.SelectionStart;
model.InsertSectionTags();
PutText(textbox, model.LinesArray(), model.SelectionStart);
}

void MenuInsertPre(object obj, EventArgs ea) {
model.SetLines(textbox.Lines); model.SelectionStart = textbox.SelectionStart;
model.InsertPreTag();
PutText(textbox, model.LinesArray(), model.SelectionStart);
}

You see, of course, that the lines shown in boldface are duplicated three times. Remember back when we first did the menu and I predicted there would be a lot of duplication across menu items? Well, there it is. A while back I did something weird and wonderful with dynamic method dispatch to try to eliminate it, but I backed that code out because it was too complex. My assumption was that sooner or later we d see what to do.

As we chatted, Paul and I observed that the KeyDown handler has the textual duplication in it but that it is also duplicating the loading and unloading of the TextModel on every single keystroke, because the duplicated lines are outside the if statements. We observed that the code would be more efficient but with even more textual duplication if we moved the lines inside the if. In the absence of a performance measure telling us that we needed that extra efficiency, we didn t make that change.

Lesson  

Notice that we have identified two different kinds of duplication here: simple textual duplication (perhaps with a few little changes) and duplication of the same activity repeated many times. All forms of duplication are candidates for removal. The ones relating to efficiency may be less important than the ones relating to clarity.




Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net