Hi All,
I have been working on various technologies and frameworks from past 5 years. I have seen copy and paste practice in most of projects specially when a developer is new to the technology or framework or project itself.
Last month I started working on project using Grails. There was some amount of code that was already written. I was new to Grails and didn’t get time to learn it because of strict dead lines. I started working and for saving time I did copy and paste from the existing code. We did the sprint release and we were happy about that. I was feeling confident that I have managed to complete my work.
Client came back to us and asked us to improve the code coverage and write the unit test cases. We started writing unit test cases and we found few bugs. Now those bugs were in the code that was copied by me in a lot of other places as well. So the person who actually wrote that code finally made corrections and checked in the code. Again I did the same thing copied the correction and tried to run the tests. My tests were failing then I realized that there are more scenarios in my case. which needs to be implemented.
Then I realized that Instead of copying CODE I was copying bugs. So I decided to study about copy and paste programming. Here are few points that we need to take care while we do copy paste from the existing code:
- Do read comments on the Method from where you are copying the code.
- In case comments are not there try to understand the code first. Don’t just go by name of the method.
- After understanding that what that piece of code does. Look at your requirement and compare if it is the same.
- If it is the same then you can copy and paste and write test cases. TDD helps here a lot. If you write your tests before you write the code then instead of doing the above three steps you can simply copy and paste the code and run your tests to check if it works for your requirement or not.
- If it doesn’t complete your requirement you may need to modify the code.
So the idea is don’t just blindly copy and paste the code. Because you never know you are copying the CODE or copying the BUGS!!
I hope you will like this post. I will be looking forward for your feedback.
Thanks,
Pawan.


Pingback: Is proper code reuse really so hard? | Software Dev 2k
Hi Pawan,
Nice post, it demonstrates clearly one of the dangers of copy/pasting.
I was writing about copy/paste/adapt (although under a different context) just yesterday on http://softwaredev2k.com/2011/10/thinking-outside-the-box-an-example-with-gwt/
Reading your post though brings forward the following (maybe obvious) question. Why did you need to copy and paste? Was it not possible to abstract, create a method and generally reuse the code in a way that it stays defined in only one place?
I have been thinking on the reasons for copy/pasting myself and I wrote a little post about this on
http://softwaredev2k.com/2011/10/is-proper-code-reuse-really-so-hard/
If you have the time take a look and tell me what you think,
Cheers,
George.
Hi George,
I read your article and I loved it. Good work mate. Keep it up. Regarding your question why did I need to copy and paste?
Answer: Well the only reason behind copy and paste was that I joined a project in between the spring release we had a release in 3-4 days and I was new to Grails and project as well. I was told by some old team member that there is a functionality that has been implemented you need same functionality for a different page. Now there are two ways to do this. One is to copy and paste and other is as you suggested to extract the common functionality for reuse. But that needs a kind of design change and also change in test classes and addition of new tests. Which takes time. This is not the first time I have come across such situation. But I completely agree with you. Always try to avoid copy and paste even though you are at your best in this act! Thanks for your suggestions and wonderful article hope to see some more from your side.
>>But that needs a kind of design change and also change in test classes and addition of new tests. Which takes time.<<
Bingo Pawan
and like you said this is not the first time one gets pinned against the wall with such decisions.
And even though we all agree (in kitchen coffee discussions over the water cooler) that we should do it right, we all return to our desks and copy/paste to death.
I recently wrote another article again under a different context (http://softwaredev2k.com/2011/11/the-purpose-of-the-architectural-prototype/) that somewhere along the sidelines suggests accepting the copy/paste fact and try to take advantage of it. You may find it an interesting read.
Cheers,
George.