What Code Reuse Really Means
Posted May 23rd, 2006Categories: Technical, Programming
Code reuse in software development is something assumed literally through re-using bits of code in more than one place. It is truly achieved through using common libraries and interfaces accessible to both the entire application and to other applications in the domain.
What is code reuse?
Literal definition: Code reuse is the practice of using existing code…again. There are some benefits and drawbacks to code reuse as it is defined here. If you take the literal meaning of the terms you end up with simply using what you’ve got in another place.
Benefits
- Speed – I tell you what, I can copy/paste stuff like no other. My fingers can instinctively assume the copy/paste position on near any keyboard shape w/o me even realizing it.
- Easy – there’s not really much thought involved in having to just duplicate your code in another applicable area of the application, or in another application. If it works in one place, it’s more than likely going to work just fine somewhere else.
Drawbacks
- Maintenance nightmare – if for any reason you want to change a copied section, you have to change ALL the copies.
- Unorganized – there isn’t much organization to it. You simply create massive amounts of code to decipher after a while.
So, if there’s such a possible mess after massive code reuse, what’s all the hype about? Companies are pushing for code reuse, yet it doesn’t seem to lend itself well to long term maintenance, which is the majority of the time spent on any lasting application.
More realistic definition: Code reuse is allowing code that you’ve written to be accessible from more than one place. It can also imply that as a component, it’s accessible via a common interface to other components or applications. Though this may not be the best 2-line definition of the term, the key is that it’s far more meaningful than the literal definition. The following sections elaborate more on what code reuse really is.
Reuse is not duplication
A colleague of mine said something today that made me laugh. He was being sarcastic of course, but I still want to share it. He said something to the effect of, “But if I write something beautiful and elegant in one place in my application, why wouldn’t I want to copy it all over my app so the entire thing is beautiful and elegant too?”
Some people may actually believe after coming out of school that lines of code, minimal processing cycles, and visual appeal of how the syntax looks on the page are much more important than they are. I don’t for one second think they have absolutely no significance, but to say they’re vitally important? No way. The look of the app should be way down the priority list, but i know it feels good to write good looking code.
Maintenance points
Duplicating code is the result of a copy-paste scenario. The second you’ve copy-pasted a module of code to two locations in your application you’ve just created an additional maintenance point. Though this can’t always be avoided, I highly suggest you do if you don’t want to be looking for all the places you have a cache being filled up with data to save load times (for example). Not only that, if you coded a bug into the “beautiful” piece you pasted everywhere, now that bug is also in every place you pasted it!
Reuse is different from duplication in that you can reuse the logic or work that a module does in multiple areas of the application from one place. It doesn’t have to physically reside in all of those places to get reused! There are plenty of programming techniques to avoid falling into the copy/paste scenario.
Interfaces
Reuse is not always functional code that needs to be applied in more than one place. It can be obtained through the use of interfaces and generics as well. By using a solid interface design to allow any type of object with a compatible set of methods to be used in a piece of your application, you allow for everything on both sides of that interface to be easily interchangeable.
Other type of interface-like methods that promote architectural reuse are generics, tag libraries, and dozens of frameworks out there that allow pieces to be easily decoupled and injected into the application.
Conclusion
In the end, reuse is definitely more than the literal meaning of the term. It most certainly implies more than just a quick copy-paste job against other modules. There’s even more to it than simply putting the code in a commonly accessible method, though that’s usually enough for most scenarios.
Code reuse is sought after so much by corporations out there. They really see the value behind not inventing the wheel every time some little function is needed. Take the time to think about your modules as you create them and just think ahead a little (and I mean a very little, just enough so that you’d be aware of it if someone asked you if you could…the module should only be made common once the requirements say it’s functionality is needed elsewhere) about what you would do if you needed to use such a module somewhere else as well.
Tags: code+reuse, programming, coding, reuse
Related Posts:
- Which is more effective: Debuggers vs. Print Statements
- The Pattern of Not Using Patterns - Part II
- 5 Steps To Help Your IT Team Concentrate
- The Pattern of Not Using Patterns - Part I
- Group Communication: Start with something positive
Explore posts in the same categories: Technical, Programming