Chapter 19. A Case for Code Reuse

If it can’t be reduced, reused, repaired, rebuilt, refurbished, refinished, resold, recycled or composted, then it should be restricted, redesigned or removed from production.

Pete Seeger

We hear about a mythical thing called “code reuse.” For a while it became incredibly fashionable; another software silver bullet, something new for the snake-oil vendors to peddle. I’m not sold on it.

We often talk in terms of “use cases” when developing software. We also see these reuse cases:

Reuse Case 1: The Copy-Pasta

Code copied out of one app is surgically placed into another. Well, in my book that’s less code reuse and more like code duplication. Or, less politely: copy-and-paste programming. It’s often evil; tantamount to code piracy. Imagine a bunch of swashbuckling programmers pillaging and hoarding software gems from rich codebases around the seven software seas. Daring. But dangerous. It’s coding with the bad hygiene of a salty seaman.

Remember the DRY mantra: do not repeat yourself.

This kind of “reuse” is a real killer when you’ve duplicated the same code fragment 516 times in one project and then discover that there’s a bug in it. How will you make sure that you find and fix every manifestation of the problem? Good luck with that.

Having said that, you can argue that copy-and-paste between projects actually gets stuff done. There’s a lot of it about and the world hasn’t come to a crashing end. Yet. And copy-and-paste code avoids the unnecessary coupling which overly DRY code can suffer.

However, copy-and-paste is a nasty business and no self-respecting programmer will admit to this kind of code “reuse.”

Key

Avoid copy-and-paste coding. Factor your logic into shared functions and common libraries, rather than suffer duplicated code (and duplicated bugs).

Whilst it is tempting to copy-and-paste code between files in a codebase, it is even more tempting to copy in large sections of code from the Web. We’ve all done it. You research something online (yes, Google is a great programming tool, and good programmers know how to wield it well). You find a snippet of quite plausible-looking code in a forum or blog post. And you slap it straight into your project to see whether it works. Ah! That seems to do it. Commit.

Whilst it’s awesome that kind souls provide online tutorials and code examples to enlighten us, it’s dangerous to take these at face value, and not apply critical judgment before incorporating them into our work.

Consider first:

  • Is the code genuinely completely correct? Does it handle all errors properly, or was it only illustrative? (Often we leave error handling and special cases as an exercise for the reader when publishing examples.) Is it bug free?

  • Is it the best way to achieve what you need? Is it an out-of-date example? Does it come from a really old blog post, containing anachronistic code?

  • Do you have rights to include it in your code? Are there any license terms applied to it?

  • How thoroughly have you tested it?

Key

Don’t copy code you find on the Web into your project without carefully inspecting it first.

Reuse Case 4: Buy In, or Reinvent the Wheel

When you need to add a new feature, there may already be third-party libraries available that provide the functionality.

Carefully consider whether it is economically more sensible to roll your own code, to pull in an open source version (if license terms permit), or to buy in a third-party solution with vendor support.

You need to weigh the ownership costs against build costs, the likely code quality, and the ease of integration and maintenance of each solution. Developers tend to want to write things themselves, not just for the intellectual exercise, but also due to a distrust of the unknown. Make an informed decision.

Key

Don’t dismiss other people’s code. It may be better to use existing libraries rather than write your own version.

Questions

  1. How much duplication is there in your codebase? How often do you see code copied and pasted between functions?

  2. How can you determine how different sections of code have to be before it is acceptable to consider it not duplication, and to not try to refactor the versions together?

  3. Do you often copy code examples from books or websites into your work? How much effort do you invest in “sanitising” the code for inclusion? Do you mercilessly update the layout, variable names, etc.? Do you add tests?

  4. When you add code from the web, should you place comments around it stating the source of the implementation? Why?

See also

Example 19-1.

If you are working on any unnecessarily general code, work out how to remove that generality and only keep the essential husk of useful logic.

bbpg 19in01

bbpg 19in02