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.
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.
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.
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.
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.
Ask this question about your functions
"if this small requirement changes, will I have to change both functions in order to satisfy it?"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With