You didn't fall prey to PrematureGeneralization; you didn't try to write reusable code ahead of time. When you're about to perform CopyAndPasteProgramming, then you cut it, paste it into a reusable module (class, function, method, whatever), and call it from both the old code (replacing the code you just cut) and the new (where you were about to paste it).
(Consider this a placeholder for a real pattern. Please feel free to replace it. Comments, suggestions, additions greatly appreciated. --PaulChisholm)
Inspired by PrematureGeneralization. See also CodeHarvesting.
In ExtremeProgramming we call this SwitchingHats?. You have to add some functionality:
- You turn it into a TestCase, perhaps the first of many for the story, but you have to start somewhere
- You ask yourself "What is the simplest code that could possibly work for the current test cases and the new one?"
- Switching hats: If you can now see a simpler structure for the existing code that will comfortably accommodate the new test case, you forget about the new test case and refactor, keeping all the old test cases running
- You get the new test case running
Refactor, refactor, refactor. Refactor some more. Then refactor it a bit. Why not let the
CopyAndPasteProgramming code "age" a little before doing all this? I like the arguments presented in
CodeHarvesting; they seem to fit my experiences pretty well.
CategoryReuse