Enough for Now


I feel that that s enough clean up for now, a couple/three hours work, probably less if I weren t writing chapters as I go. There s still some confusion between ArrayList and Array, but that s mostly a function of the TextBox needing Array and our own classes looking better with ArrayList. We might look at that later, but more likely we ll move on to improving the tool and its tests. As an example of what has changed, here s what this code looked like when we started this exercise:

 public void InsertParagraphTag() { 
//
// On Enter, we change the TextModel lines to insert, after the line
// containing the cursor, a blank line, and a line with <P></P>.
// We set the new cursor location to be between the P tags: <P></P>.
//
// handle empty array special case (yucch)
if ( lines.Count == 0 ) {
lines.Add( "<P></P>" );
selectionStart = 3;
return;
}
lines.InsertRange(LineContainingCursor()+1, NewParagraph());
selectionStart = NewParaSelectionStart(LineContainingCursor() + 2);
}
private int NewParaSelectionStart(int cursorLine) {
int length = 0;
for (int i = 0; i < cursorLine; i++)
length += ((String)lines[i]).Length + Environment.NewLine.Length;
return length + "<p>".Length;
}
public ArrayList NewParagraph() {
ArrayList temp = new ArrayList();
temp.Add("");
temp.Add("<P></P>");
return temp;
}

public void AltS() {
InsertSectionTags();
}
public void InsertSectionTags() {
if ( lines.Count == 0 ) {
lines.Add( "<sect1><title></title>" );
lines.Add( "</sect1>");
selectionStart = 14;
return;
} lines.InsertRange(LineContainingCursor()+1, NewSection());
selectionStart = NewSectionSelectionStart(LineContainingCursor() + 1);
}
public ArrayList NewSection() {
ArrayList temp = new ArrayList();
temp.Add("<sect1><title></title>");
temp.Add("</sect1>");
return temp;
}
private int NewSectionSelectionStart(int cursorLine) {
int length = 0;
for (int i = 0; i < cursorLine; i++)
length += ((String)lines[i]).Length + Environment.NewLine.Length;
return length + "<sect1><title>".Length;
}

And here s what it looks like now:

 public void Enter() { 
InsertParagraphTag();
}
public void InsertParagraphTag() {
InsertTags(newParagraph, paragraphSkip);
}
public void AltS() {
InsertSectionTags();
}
public void InsertSectionTags() {
InsertTags(newSection, sectionSkip);
}
private void InsertTags(string[] tagsToInsert, string tagsPrecedingCursor) {
int cursorLine = LineContainingCursor();
lines.InsertRange(cursorLine+1, tagsToInsert);
selectionStart = NewSelectionStart(cursorLine + 1, tagsPrecedingCursor);
}
private int NewSelectionStart(int cursorLine, string tags) {
return SumLineLengths(cursorLine) + tags.Length;
}
private int SumLineLengths(int cursorLine) {
int length = 0;
for (int i = 0; i < cursorLine; i++)
length += ((String)lines[i]).Length + Environment.NewLine.Length;
return length;
}

Remember that all that code was duplicated for the Enter/InsertParagraphTag, and now it s all using the same methods . Very little duplication. I think it s a noticeable improvement and hope that you do as well.

Lesson  

The big learning here is worth underlining: we have very much improved the design of this class. Almost everything that was specific is general now. Things that were cut and pasted or duplicated are now almost all centralized. The code looks like it was designed, and designed fairly well.

But we know differently. This code was not designed: it was refined, and refined, by looking always at little chunks , not by looking at the overall picture. To me, this is the big programming lesson of XP: simple design, solid tests, and continuous refactoring make a program come into being that is well-designed ”without having to make any big design decisions. Of course, if you like making big (and risky) design decisions, you might not like this outcome. I like it a lot, because it lets me focus on delivering what my customer wants and needs, without concern that I ll mess up the design by going too fast.

And don t forget those tests. They saved me a number of times ” and I learned that I need a few more. We ll address that concern as we go along.




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