Tag: reuse
How I Escape the “Reuse Trap”
by Chris Wash on Aug.02, 2008, under Software Engineering
If you’ve checked out my blogroll, you’ll notice I have a link to Basil Vandegriend’s “Professional Software Development” blog. I like a lot of the articles he has written and to a large extent the subject matter I envision for this blog frequently crisscrosses with Basil’s.
While I was reading “The Reuse Trap In Software Design” I found myself thinking,”Me too! Me too!” like a giddy kid on the playground that’s found a new pal with a mutual interest in “pet snakes and/or tarantulas.” As with many of the problems Basil writes about in his blog, I experienced the same problem, investigated it and found the same root causes, and came to many of the same conclusions as those outlined. He describes the “reuse trap” as:
…[A] term I coined to describe the situation when one becomes stuck trying to design new functionality while simultaneously attempting to reuse existing code that needs some modifications.
He then goes on to describe why it’s a tough problem for n00bs and outlines a strategy he uses to get out of the trap–one that’s best described as a two-step process: copy-paste to reuse/refactoring to remove duplication. I encourage you to read his article if you’re not familiar with the terms, or how this strategy can solve the problem described above.
My preferred technique for mitigating the problems involved isn’t copy-paste reuse/refactoring; it’s stubbing the calls to the code-to-be-reused (C2BR) that needs modification. If I can fake out the behavior I need, it lets me focus on the nature of the dependency that exists between the two objects/components. When I go to modify the C2BR, my inputs and outputs (or other consequences that occur as a result of a call to the C2BR) translate directly into a test that I can use to drive the changes I make to that code. It also ensures that the coupling that exists between the objects is loose and legit.
The only thing to watch out for is that care must be taken to then remove the (small) duplication of the stubbed out inputs/outputs and actually wire the two pieces together when finished with the reused code. I usually do that by throwing a TODO:WIRE marker in when I stub something.
I’ve found this is the approach I take more often, though I do take the copy-paste reuse/refactoring route from time to time. This stub/rewire approach is a bit more advanced, but it’s how I envisioned solving the “reuse trap” when, as Basil puts it, “I became aware of it” and all that it entailed.
Do you ever come across this problem? How do you get out of this “trap” or avoid being caught up in it in the first place?