Copy Pasta Madness

A brief moment on copy-paste. Copy-pasta, if you're into that kind of dish.

I whine about copy-paste often. I whine about it at work a lot. I whine about it to the point where the joke “look out, pseudoramble will be angry if he sees you copying-and-pasting!” is a fan favorite.

I have my reasons for whining about copy-pasta (mainly that I've made these mistakes). I wanted to make my own thoughts both more clear and less hard-line than I've managed to set myself up for.

When it might be OK to copy-paste

Moving code to different parts of the app

Taking the time to move useful code into a module, function, or method is worthwhile. Solidifying this code into a more reliable hunk is a meaningful improvement to the codebase as a whole, present and future.

Present You has to put in a little extra legwork. Future You is very content with your choice.

Adding content to visual components

When you have content generated by an outside source (like an editor) it makes sense to move it verbatim to avoid losing the effort already put into that work. Since the effort is in the content, making a mistake by rewriting it isn't worth it.

Present You will be happier. Future You will be indifferent, and this is a good thing.

Bad times to copy-paste

Learning a new programming concept by borrowing examples

Learning new programming concepts is of course costly in time and effort. People want to minimize this time. It's tempting to copy-paste code from a tutorial and change that. But rather than making you learn faster, you've made implementation time faster instead without understanding the pieces. So the learning was skipped.

Present You doesn't learn as much. Future You now works much harder to grok the concept.

Using a feature from the internet

If learning new concepts costs $1000 for developers, a new feature (probably) costs $10000. Features takes massive effort to be somewhat confident in its correctness. Of course going to StackOverflow for help is so tempting. But remember it doesn't include an API, a data structure, automated tests, docs, or a promise to be correct at all.

Present You saves an hour or so. Future You fixes bug and builds tests later on.

Implementing a new feature by starting with an existing similar feature as an example

While modules are unique, putting them together can sometimes seem repetitive. React components for example contain a render() function, styles, and some helpers. Copying an existing React component into another file to use as a template is misleading because typing it out wouldn't take much longer. And problems are immediate. “Do I need this? I dunno what {x} does. Better leave it.”

Present You is nervous about the code. Future You is terrified to change anything at all ever.

TL;DR

  1. Copy-pasta is an OK tool when shaping code you've written and adding content to your system that does not need changing.

  2. Copy-pasta is not a good tool for creating new modules, even when borrowing from existing ones.

  3. Copy-pasta is not a tool for learning new programming concepts.