Copy-And-Paste Code vs Maintainability
/Code Reuse
When writing software, many of the various tasks we want the software to perform are similar. Writing data to the database is a good example. In one place the software may be writing recipe data to the database; in another, the software may be saving contact data to the database. In both cases, the bulk of the code is nearly identical (the only things which change are the actual data fields).
Rather than rewrite the code for modifying the database from scratch each time we encounter this problem, we write the code once and reuse it each time we need to make database changes. This saves time and is less error-prone (since rewriting that same code from scratch would quickly get repetitive, and bored programmers are more prone to typos and omissions).
There are multiple ways to accomplish this reuse of code; the most common is to create a code library which can be referenced anywhere in the application where we need to accomplish the particular task. In this example, the code to write data to the database would be made into a library and both the recipe form and the contact info form could use the code from it. Using a library for this purpose has an important advantage for maintainability - if a bug is discovered in the code for writing data to the database, that bug only needs to be fixed in one place (the library) and every other portion of the code which uses that library will now be fixed.
Copy-and-Paste
"Copy and Paste" coding is a technique wherein a programmer reuses code by literally copying it from one file and pasting it into another, sometimes making small changes in the new file.
A programmer might do this for any number of reasons:
- The programmer doesn't know that other options exist.
- The programmer is not sufficiently skilled to convert the code into reusable library code.
- The programmer is rushed (making code reusable as a library is not as fast in the moment as copying and pasting).
Impact on Maintainability
This kind of coding has a huge impact on the long-term maintainability of the code base. If a bug is discovered in the code being copy-pasted, that bug now has to be manually fixed in every single place the original code was reused (rather than being fixed once in a library). This can be error-prone, as each instance of the copy-pasted code has to be found and modified by human beings. There's also the danger of simply missing one or more places that the buggy code has been reused; a typical symptom of this is having a bug which was thought to be fixed pop up again at a later time.
This same problem applies to new features and enhancements - for any changes which need to be made, all instances of the original code which have been copy-pasted have to be found and modified manually. This is a slower and much more error-prone process than simply updating a single library.
There's an additional impact to maintainability when bringing in new developers to work on the code - the new developer may not be aware that the piece of code they are working on has been duplicated throughout the code base. The new developer makes a change or bug fix in one instance of the copy-pasted code and none of the others - now the various copies are beginning to diverge, making them harder to track down for later changes.