Improving the Code

In the middle of that discussion, Chet s wife Sue showed up and we had dinner; then they went home. I m on my own again now. Keep an eye out for the mistakes that I d have avoided if I d had him to pair with.


You may be tempted to design something better to take care of all these special cases. Frankly, a while back in our programming experience, so would we. But we ve learned something powerful and interesting about simple code: if we simply find and remove duplication in obvious ways, we usually wind up with code that s much cleaner and clearer. sb Removing duplication is your friend. So let s just do that and see what happens.

First let s scan for duplications:

  • In the section- related code, the strings for the first and second line of the section tag are duplicated , in the NewSection() method and in the special case code of the InsertSectionTags() method. We could clearly get rid of the latter by calling the former and looping. That might lead to further consolidation when we do the same thing for the paragraph tag.

     public void InsertSectionTags() { 
    if ( lines.Count == 0 ) {
    lines.Add( " < sect1 >< title >< /title > " );
    lines.Add( " < /sect1 > ");
    selectionStart = 14;
    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;
  • The paragraph code has almost the same duplication, except that we double-space before each paragraph tag in all but the empty-lines case. Otherwise, the same strings would appear in analogous places there. That makes it tempting to remove the double-spacing feature to consolidate the strings. We d have to check with the customer, but I happen to know that they don t double-space their paragraph tags now, so they d probably go for it.

     public void InsertParagraphTag() { 
    // handle empty array special case (yucch)
    if ( lines.Count == 0 ) {
    lines.Add( " < P >< /P > " );
    selectionStart = 3;
    lines.InsertRange(LineContainingCursor()+1, NewParagraph());
    // set cursor location
    selectionStart = NewSelectionStart(LineContainingCursor() + 2);
    public ArrayList NewParagraph() {
    ArrayList temp = new ArrayList();
    temp.Add(" < P >< /P > ");
    return temp;
  • The paragraph code and the section code have exactly the same structure. They check the special case of the model being empty, add some stuff, and set the cursor explicitly. Otherwise, they insert basically the same stuff and set the cursor (somewhat) programmatically. There are three kinds of duplication here:

    • Paragraph and section have the same shape. That suggests consolidating across them.

    • Paragraph and section have duplicated function, the insertion, implemented in two different ways. That suggests removing the special case.

  • There are, of course, many duplicated individual lines between these two implementations . We could attack them singly .

In the end, it s all a matter of taste what you go after. You ll likely wind up with much the same code in every case, and you ll wind up with much better code for sure.

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

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: