If you're copy-pasting while you're writing code, you're doing it wrong. Seriously.
If you're doing something more than once, extract the common code into a separate routine. If you're doing that thing more than once, but slightly differently each time, put the common code into a class, and subclass it as needed for differences. You should've learned this in your first year of programming.
In the 20-ish years I've been writing code professionally, the majority of bugs, the majority of maintenance pains I've experienced, even many of those faced by my colleagues, have all traced back to someone else playing "copy-paste cowboy".
We've since protected ourselves against the tyranny of copy-paste by installing CPD as a build step, and failing builds that pass a threshold: http://pmd.sourceforge.net/pmd-4.3.0/cpd.html
If you're doing something more than once, extract the common code into a separate routine.
You realise that, if you're not using a language with refactoring support, doing what you say involves cutting and pasting more often than not, rather than avoiding it?
put the common code into a class, and subclass it as needed for differences
How about an IDE with refactoring support? Have you used Eclipse lately?
Not for 18 months or so, it's a piece of crap. And regardless of whether or not I have, thousands of people prefer a better editing experience using a non-IDE.
Your eloquent response has not only shown me the err of my ways, but you've lit the path to enlightenment
I'm honoured. I would have thought two decades of every decent programmer on the planet knowing inheritance is a terrible method of reuse would have got through to you, but I'm glad to be the one who finally disavowed you of the naive lessons from your CS 101 class.
-5
u/[deleted] Jul 31 '15
If you're copy-pasting while you're writing code, you're doing it wrong. Seriously.
If you're doing something more than once, extract the common code into a separate routine. If you're doing that thing more than once, but slightly differently each time, put the common code into a class, and subclass it as needed for differences. You should've learned this in your first year of programming.
In the 20-ish years I've been writing code professionally, the majority of bugs, the majority of maintenance pains I've experienced, even many of those faced by my colleagues, have all traced back to someone else playing "copy-paste cowboy".
We've since protected ourselves against the tyranny of copy-paste by installing CPD as a build step, and failing builds that pass a threshold: http://pmd.sourceforge.net/pmd-4.3.0/cpd.html