Code Reuse

In writing programs the mantra is “Reuse is good.” If you don’t have to write something twice then don’t. This is a simple idea and has great power. Reuse makes the code base smaller which makes maintenance simpler and cheaper. It lets you spend time writing “new” things. Everyone wins.

But is reuse an absolute? It it always better than the alternative? I don’t think so. I believe that there are several issues involved and that reuse is effective if the cost is below a threshold. The real questions are what are the costs and what is the threshold?

One of the most obvious costs is trying to find code to reuse. But even this is difficult. If the code you want to reuse is complex then the time spent looking for it and learning how to use it may be greater than the time spent to write it. But even if the time is longer that doesn’t necessarily mean that the time was wasted. Once you have the code you may use it again which reduces the cost.

Then there is the maintenance cost. Reusing code reduces the maintenance cost…or does it? The code must still be tested in your system’s context and if it is not working right you still have to spend time determining where the problem is. Then, if you find that the problem is in the reused code, you have to fix it.  If you incorporated the source into your system then you do the fix yourself but then you have to learn how the code works which takes time.  If you are using a library then you have to get the library’s maintainers to fix it which means learning how to submit a defect and tracking the defect’s progress and be ready to respond to questions from the maintainers.  Once you have a fix then you have to merge the fix into your system and retest to verify that the problem is fixed and that nothing else was broken.  All of this can be quite costly.

So when does it make sense to reuse instead of rewrite? Some of the factors involved are:

  • Do you have the time to write it yourself? If you don’t then you have to take on the costs.
  • Do you have the technical knowledge to write it? If the problem is complex and the only reasonable solution is complex and you don’t have the knowledge then you can’t write it. You either have to learn how (more cost) or reuse.
  • Can you find an acceptable package or piece of code to reuse? If you can’t find a solution out there then you have no choice but to write it.
  • Is the maintenance cost higher or lower if you write it yourself?
  • The code you have found is close but not exactly what you need.  Do you change your code/requirements to fit or do you change the reused code (assuming you have the source)?
  • Related to the previous item, you have found a completely different way of doing what you want in a code library that may require a lot of rewriting.  Will the result be better?

This is not an exhaustive list but it shows that reuse is not always a good thing.  You must check your specific case and weigh many factors to determine whether it is worth the cost.

By Reflector

I am a Senior Engineer for VMware, a company that produces cloud and virtual machine software. When not working I enjoy board games and woodworking (producing furniture and other items for our house).

1 comment

Leave a Reply

%d bloggers like this: