Copying CODE or BUGS?

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.

Share on : Twitter, Facebook or Google+