Are the Objects Helping Us?


We ask this question whenever something seems to be hard to program. If we have to send several messages to an object to accomplish something, maybe that object isn t helping enough. Maybe it needs a method added to it that does more of the job for us. Or perhaps there is another class in there trying to get out. Here s an example from our most recent work, the SingleCharacterSnapshot s Restore() method:

 public void Restore(TextModel model) { 
String lineToEdit = (String) model.Lines[lineNumber];
String oldLine = lineToEdit.Remove(positionInLine,1);
model.Lines[lineNumber] = oldLine;
model.SelectionStart = selectionStart;
}

This method sends three messages to the model, one message to a string, and no messages to itself. This is a lot of work outside the model. In some of our earlier attempts at Undo, we added methods to the TextModel to help out. We might want to do that here, perhaps this way:

 public void Restore(TextModel model) { 
model.RemoveCharacterForUndo(lineNumber, positionInLine, selectionStart);
}

supported in TextModel by:

 public void RemoveCharacterForUndo(int line, int position, int start) { 
Lines[line] = ((string) Lines[line]).Remove(position,1);
SelectionStart = start;
}

Better? Quite possibly. At least the functionality is now where it belongs, instead of over in the Undo, ripping the guts out of the TextModel and jamming them back. We might want to improve the name , but at this moment I don t have a better idea.

We do ask, of course, Are the names helping us? You have seen places where we renamed methods just to make them more expressive.




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

Similar book on Amazon

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