Minimal

stressed for motivation and achievement

2005-10-01

 

Code reuse vs. YAGNI refactoring

TDD advises that you let duplication occur naturally before refactoring—for instance, to extract a superclass. That’s fine for the first two classes (the original and the duplicating class), but what happens when you get to class number 3? [more...]

You can predict that it, too, will end up requiring the functionality shared by the first two classes. For example, it may be a third wizard page you’re creating—you know you’re designing a wizard page, just like its forebears. However, as you’re diligently writing one test at a time for this new class, can you justify taking the leap to include all of that existing functionality without having already written tests for it? Or does your conscience kick in and force you to go through the rigmarole of duplicating all of that stuff again, just to prove that reusing the code is valid?

I guess there are a couple of things to consider here:

  1. How comfortable do you feel about taking the shortcut? Are you disciplined enough to make sure that you test all inherited behaviour on the new class?
  2. More importantly, which path is going to provide greater productivity? If there’s a non-trivial risk that early adoption of the superclass will adversely affect the quality and slow down progress, perhaps the better path is to duplicate after all.

Actually, I’m still not comfortable with this conclusion. Sure, productivity has to be the deciding factor, but it’s a judgement call as to how your decision will affect it. There seems to be no hard and fast rule—personal experience and confidence appear to be the key. Damn. One thing I am pretty sure of, however, is that if you’re going to get it wrong, get it wrong quickly. ;-)

Update: following some comments from Kevin Rutherford on the Stacking unit tests post, I’m rethinking what I’ve said above. Do I need to test all inherited behaviour on the new class? Probably not—I’ve already got tests for it associated with other classes. All I need to test is the various behaviours I’m implementing for the new class and they’ll be very class-specific.


Comments:
Good point - thanks for the note.
 
Post a Comment



<< Home

Archives

April 2002   May 2002   June 2002   July 2002   August 2002   September 2002   October 2002   November 2002   December 2002   January 2003   February 2003   March 2003   April 2003   May 2003   June 2003   July 2003   August 2003   September 2003   October 2003   November 2003   December 2003   January 2004   February 2004   March 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007  

This page is powered by Blogger. Isn't yours?