Learning to Re-use
I’ve discovered something obvious: refactoring is best done in the course of making something reusable, and the best way to do that is to have a need to reuse something.
A few months ago I was part of a complete rewrite of a critical piece of our system which happened to be written in the worst possible style: no separation between presentation and logic, ASPX pages rendering most of their markup from code-behind (meaning design changes had to go from the designer through a developer), crazy inter-dependencies that were never obvious until they broke. So one of my resolutions for the rewrite was to avoid those mistakes the second time.
I had an feeling that we wouldn’t see the last of these components, so I tried hard to avoid them, but as the project hit crunch time and some late requirements arose, some crept back in; in particular the crazy inter-dependencies. But the code worked and it was less bad that its predecessor and achieved all the new aims required of it, so it sat on our servers for a month or so happy and not requiring any attention.
Then a new project came up, a small one, that really could use a component from the original project. Happy to score a small bonus from having a reusable component, I went about building the new page, but ran into a few small issues – things I needed to change to break the component out of the environment it grew up in. The changes were all minor – a new property, and a check later on the value of that property were all there was to it. The new page came out, happily reusing the component, and the new page went on for a month or so, not requiring any attention.
Now I’m in the beginning of a new project which also could really use a few components from the first project, and now the problem of bad dependencies is really coming up – 4 or 5 places that break because the environment is different from the one the components were created for. It’s a pain and I’m cursing my original lack of foresight, until it occurred to me that our users had the benefit of a couple new features while I learned how to write better code.
— Gordon Weakliem
Comment
Commenting is closed for this article.