DEV Community

Scott Hannen
Scott Hannen

Posted on

 

Mandatory Copy and Paste

I think most skilled developers understand why copying and pasting code is a horrible way to implement "reuse." Copying and pasting code from an external source like stackoverflow.com is one thing with its own issues. But when we start copying and pasting significant amounts of code within our own repositories, that's another level of bad.

But there's something worse.

I've sometimes found myself in environments where in order to develop software, I am required to start by copying and pasting an entire repository and then just editing small parts of it to meet specific requirements. That's not bad. It's evil. Why?

90% of the time the person who thinks it's a good idea to copy and paste an entire application and make minor changes is the person who developed the original. That means that you are going to start with software written by someone who doesn't understand a concept as fundamental as avoiding code duplication. Therefore it's a safe bet that whatever code you're copying and pasting is abysmal. If you wrote it yourself you could make it work or at least take responsibility. Now you're going to waste time studying bad code and fixing its bugs.

Following from the previous point, copying and pasting entire applications forces a developer to deliver bad code. What could be more demoralizing than knowing how to build something but being required to implement it with someone else's bad code?

I've seen developers describe this sort of approach as "using a framework." Here's a newsflash: If you can write exactly the code you need for specific requirements and plug it in to existing code, you can call that a framework. Copying and pasting bad code isn't a "framework." Spreading out multiple levels of inheritance across multiple NuGet packages so that classes interact with their bases in incomprehensible ways and no one can tell what a class does by looking at it is not a "framework."

They tell you that it's going to be simple because all of the work is already done, and you're just copying it. But if it's really a few minutes of work then why do you want someone else to do it? The answer is that once you've duplicated the code you begin to discover countless other issues that involve undocumented behaviors and glitches buried under multiple levels of proprietary code in several libraries that you have to decompile because you don't have the source. It would take less time to rewrite the whole thing because you understand the language and technology, and whatever you don't know you can find. But you're not writing software. You're doing unnecessary work that doesn't advance anyone's requirements to get code no one needs to work.

The really fun part is when you start cleaning up some of the atrocious code, not just because you want to check in clean code, but because you can't follow what it does without refactoring at least a little bit.

So you break up gigantic classes and methods, give classes names that reflect what they do, remove dead code which may be 80% or more if because the "template" has nothing to do with your requirements, remove class-level variables which are inexplicably used only as if they were local variables, collapse three levels of inheritance into one class which is smaller than any one of the three on its own after you remove dead code, and remove unnecessary mutable state which you're pretty sure is causing bugs no one has noticed in every other application using the same code because the original developer doesn't understand that their code operates in a multithreaded environment, or does but doesn't know how to account for it.

The result: You're told to start from scratch from the template because your code doesn't meet "standards." (There's nothing like being lectured about "standards" by someone who's just said he adds separate constructors to classes "just for unit testing" and calls it a "pattern.") The problem isn't the code, which he didn't look at. He explains that making changes means that no one else will be able to support it. Removing classes, projects, methods, and variables that the application does not use will make it more difficult to support.

Translation: My code is perfect, and you should be grateful for the honor of copying and pasting it. Don't change anything, because I want every line of code produced by this organization to resemble my own perfect work.

It's like a music teacher who writes a cacaphonic, ear-splitting symphony and teaches others that all good music begins by starting with their own work as a template and changing as little as possible. It's self-serving egotism.

I'm not a snob. I've written bad code, and code that in retrospect could have been better. Sometimes I see StackOverflow questions containing blocks of code so unpenetrable and hideous that no one can even attempt to find the part that relates to their question. I don't look down on that because we've all been there.

But if your code looks like a bad StackOverflow question, don't tell other people to copy and paste it. I don't care what your job title is. When you learn how to write code that sucks less, it will come with the wisdom that we should not "reuse" it by copying and pasting.

In the meantime, if you think that your rampant code duplication is a "framework" and that copying and pasting it is how software development works, then here's an idea: Copy and paste it yourself. For whatever specialized development is required, give developers an interface to implement so they can write that code, hand it to you, and you can integrate it into your glorious framework.

Thank you, dev.to for having a tag called "rant."

Top comments (5)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git