Code Reuse

In writ­ing pro­grams the mantra is “Reuse is good.” If you don’t have to write some­thing twice then don’t. This is a sim­ple idea and has great pow­er. Reuse makes the code base small­er which makes main­te­nance sim­pler and cheap­er. It lets you spend time writ­ing “new” things. Everyone wins.

But is reuse an absolute? It it always bet­ter than the alter­na­tive? I don’t think so. I believe that there are sev­er­al issues involved and that reuse is effec­tive if the cost is below a thresh­old. The real ques­tions are what are the costs and what is the thresh­old?

One of the most obvi­ous costs is try­ing to find code to reuse. But even this is dif­fi­cult. If the code you want to reuse is com­plex then the time spent look­ing for it and learn­ing how to use it may be greater than the time spent to write it. But even if the time is longer that does­n’t nec­es­sar­i­ly mean that the time was wast­ed. Once you have the code you may use it again which reduces the cost.

Then there is the main­te­nance cost. Reusing code reduces the main­te­nance cost…or does it? The code must still be test­ed in your sys­tem’s con­text and if it is not work­ing right you still have to spend time deter­min­ing where the prob­lem is. Then, if you find that the prob­lem is in the reused code, you have to fix it. If you incor­po­rat­ed the source into your sys­tem then you do the fix your­self 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 main­tain­ers to fix it which means learn­ing how to sub­mit a defect and track­ing the defec­t’s progress and be ready to respond to ques­tions from the main­tain­ers. Once you have a fix then you have to merge the fix into your sys­tem and retest to ver­i­fy that the prob­lem is fixed and that noth­ing else was bro­ken. All of this can be quite cost­ly.

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

  • Do you have the time to write it your­self? If you don’t then you have to take on the costs.
  • Do you have the tech­ni­cal knowl­edge to write it? If the prob­lem is com­plex and the only rea­son­able solu­tion is com­plex and you don’t have the knowl­edge then you can’t write it. You either have to learn how (more cost) or reuse.
  • Can you find an accept­able pack­age or piece of code to reuse? If you can’t find a solu­tion out there then you have no choice but to write it.
  • Is the main­te­nance cost high­er or low­er if you write it your­self?
  • The code you have found is close but not exact­ly what you need. Do you change your code/​requirements to fit or do you change the reused code (assum­ing you have the source)?
  • Related to the pre­vi­ous item, you have found a com­plete­ly dif­fer­ent way of doing what you want in a code library that may require a lot of rewrit­ing. Will the result be bet­ter?

This is not an exhaus­tive list but it shows that reuse is not always a good thing. You must check your spe­cif­ic case and weigh many fac­tors to deter­mine whether it is worth the cost.

One thought on “Code Reuse

Leave a Reply