The Most Destructive Keystrokes In Software Development Are The Ones Used For Copy/Paste

Okay, so maybe the title of this post is a little over-dramatic. But the fact remains: copy/paste "coding" leads to problems, and is probably my least favorite bad coding practice (or the bane of my development experience!).

I think we've all been there: we don't know how to code something, so we do a quick search, either via search engine or in our existing codebase, to find an example, and then we copy and paste that example into the code we're currently working with. And sometimes, that's where it ends -- we've found our solution, and we move on with the rest of our coding. But often, just pasting that code in and moving along can have some unintended repercussions. Here's what can happen.

A Lack of Understanding

Sure, you've got code that works -- but do you know how it works? Developers spend more time maintaining code than initially developing it, and if you don't know how or why something works, it can make maintaining the code -- or troubleshooting it -- more difficult. If you needed to search for an example and you've copied and pasted, be sure to take the time to learn how and why that code does what you needed it to do.

Duplicate Code

More code means more code to maintain. And duplicate code means the potential for having to make the same changes in multiple places if bugs are found or business requirements change. But the easy path that some developers take is to find the last code that was similar to what they need to develop now, and just copy and paste the whole thing. A better alternative is to implement a reusable solution, such as a service class containing the code that needs to be used in multiple places, or even implementing a shared base class (though the growing consensus, which is not new, is that composition should be favored over inheritance, but that's a topic for another time).

Unnecessary Code

Along the lines of the aforementioned "copying the whole thing" scenario mentioned above, this can occur when someone, for example, finds a class they need to replicate, and then copies the whole thing, including the functionality they don't need. The result can be a new class with duplicate code and some functions and variables which aren't even used, which can result in misleading code. Code that accurately conveys intent is a principle of clean coding, and having unnecessary code violates that.

Perpetuation of Bad Coding Practices

This can be very destructive. Just because someone coded something before, and you're finding it to use as an example, doesn't mean that the original developer did it in a good way. Maybe their code leaks memory. Or is implemented in an inefficient way. Or can lead to intermittent failures. The copy/paste mentality of "I'm going to do it the way the last developer did it" can lead to a lot of bad coding practices perpetuated throughout the code base. And this leads us back to the first item on this list, A Lack of Understanding.

Conclusion

Developers often need to find quick solutions to problems, and examples can be helpful. But care must be taken to understand the code being implemented, to be mindful of what's being implemented, and how, to avoid problems in the future.

Comments

Popular Posts

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

Silent Renew and the "login_required" Error When Using oidc-client

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4

How to Determine if a Column Exists in a DataReader