Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is copy-and-paste coding ever acceptable?

It's generally accepted that copy and paste programming is a bad idea, but what is the best way to handle a situation where you have two functions or blocks of code that really do need to be different in just a few ways make generalizing them extremely messy?

What if the code is substantially the same, except for a few minor variations, but those few minor variations aren't in things that are easy to factor out through either adding a parameter, template methods, or something like that?

More generally, have you ever encountered a situation where you would admit that a little copy-and-paste coding was truly justified.

like image 478
dsimcha Avatar asked Dec 30 '08 23:12

dsimcha


People also ask

Is it okay to copy paste in coding?

Copying and pasting the code is often considered as an antipattern or even absolute evil. However, copy and paste is just a tool that developers can use inappropriately, causing significant damage to the codebase, or can use it appropriately to speed up development without compromising code quality.

Is it OK to copy someone's code?

Yes, obviously. This is an example of copying from yourself, and as long as you don't recycle anything you've used in someone else's proprietary project, there is absolutely nothing wrong with it.

How much of coding is copy and paste?

Code once, run millions of times Depending on who you ask, as little as 5-10% or as much as much as 7-23% of code is cloned from somewhere else. Whether these clones are good or bad is up for debate. Regardless of the exact amount, code cloning is extremely common.

Are you allowed to copy code Why or why not?

It is never ok to copy and paste code from an open source project directly into your proprietary code. Don't do it. Just don't. Even if you're on a tight deadline.


1 Answers

Ask this question about your functions

"if this small requirement changes, will I have to change both functions in order to satisfy it?"

like image 147
Tim Jarvis Avatar answered Oct 14 '22 09:10

Tim Jarvis