What Do We See Now?


There are a number of things that I don t like much. There is still evidence of confusion between arrays and ArrayLists, and I don t like the look of that highlighted LineContainingCursor method. Also, the AltS and Enter methods just call the InsertParagraphTag and InsertSectionTags methods . I m not entirely fond of that but it is a well-honored coding pattern, Explaining Method Name. We could just copy the code for InsertSectionTags in there, but if we did, then when we read the AltS code we would have to figure out its purpose. Using the other method name says Alt+S is InsertSectionTags . If the code didn t say it, then a comment would have to. I d rather have the code say it.

Another thing I notice is that the highlighted InsertSectionTags and InsertParagraphTag methods look a lot alike. Duplication! We have to get rid of it. How to do it? Extract a new method called, say, InsertTags, giving it parameters for the variable bits, the tags. We ll do them one at a time, first InsertSectionTags, for no particular reason. The new code is

 public void InsertSectionTags() { 
InsertTags(newSection, "<sect1><title>");
}
private void InsertTags(string[] tagsToInsert, string tagsPrecedingCursor) {
int cursorLine = LineContainingCursor();
lines.InsertRange(cursorLine+1, tagsToInsert);
selectionStart = NewSelectionStart(cursorLine + 1, tagsPrecedingCursor);
}

Tests all run. I ll make the same change to InsertParagraphTag:

 public void InsertParagraphTag() { 
InsertTags(newParagraph, "<P>");
}

Tests still run. Now, looking at those Insert methods, it s clear that their tagsPrecedingCursor strings should be converted to static constants, just like the tagsToInsert. Let s do that:

 private static string[] newParagraph = { "<P></P>" }; 
private static string paragraphSkip = " < P > ";
private static string[] newSection =
{"<sect1><title></title>","</sect1>" };
private static string sectionSkip = " < sect1 >< title > ";

public void InsertParagraphTag() {
InsertTags(newParagraph, paragraphSkip);
}
public void InsertSectionTags() {
InsertTags(newSection, sectionSkip);
}

Again, the tests all run. There s a little duplication in those strings, but we ll let it go for now: I like the way the strings communicate what s inserted and what is skipped to set the caret.




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