A Few Words In Defense of Copy And Paste Programming
Dec 22nd, 2008 | Practices
I was always against Copy and Paste programming. I condemned people who use this practice as discrediting the honorable profession of The Programmer. This evil activity inflates application code without adding anything useful. This shameful practice causes various complications – more code, more things to remember, needless complexity, and difficulty to manage and synchronize related changes in the future. But at some point, I admitted to myself – things are not so black and white. I do Copy and Paste in my code, it is difficult to resist this seductive activity.
The only indulgence is that I’m very keen to eliminate bad effects of Copy and Paste before finishing my task.
I use two main approaches for writing new code. The first is Top-Down – I have clear design ideas how I should program and I know what classes and components to use. I go ahead and write code from scratch using my memory and power of intellect :). There are few problems with this approach. Unfortunately, my memory is not so great and knowledge is not so vast. Regularly I have new tasks that I don’t know or don’t remember how to program. Sometimes I don’t get immediately what other people programmed without debugging. And often I want to save time and avoid re-implementing the same idea again. And that is why I also use the second approach – Bottom-Up. I copy and paste code first. And after I figure out how this code works, how to use, fit and improve it.
When is Copy and Paste approach useful?
Shamelessly, I can come up with few cases:
1. Work by example – you found new code through search and want to use it immediately to solve your problems.
2. Quick start – you copy code (including your old code) to save time.
3. Test in the new context – you want to quickly assess if code will be correctly running in the context of your application
4. Too hard to reuse – if you cannot reuse, just copy code. For example, you don’t have time to build common framework with separate application or another team owns code and don’t want you to touch and spoil it.
5. Series of experiments – you try to find the best way to solve a problem and copy code to try if it solves a problem well.
How to eliminate bad effects of Copy and Paste
Now I’m going to return to canonical view on evilness of Copy and Paste. Even if you have legitimate reason to use Copy and Paste, it is acceptable only on temporary basis before you commit your code or your team granted absolution (probably for very short time) :).
Therefore, after committing Copy and Paste scandalous act, do following:
1. Understand what have you done – any code that you don’t understand is ineffective for future use, and potentially excessive, wasteful and dangerous.
2. Clean, minimize and shape – remove all useless, over-engineered and questionable pieces; consider yourself as a jeweler, who is converting rough crystals in beautiful diamonds that fit well into solid code and concepts in your program.
3. Refactor and Integrate – use refactoring techniques to integrate new cleaned code into existing application: improve design, simplify and increase power of your code.
4. Learn – the best outcome of this sinful activity is gained knowledge and experience that will make Copy and Paste unnecessary next time.
With age and experience of harsh programming reality, I’m becoming less purist and more pragmatic. I have started to accept everything what makes me effective. The only rule – I shouldn’t be ashamed of my code after the finish… even if I did Copy and Paste couple times 😉
What do you think?