Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

With plain Google as well as Google Code search tools it is easy to find how to program using some resource or solve certain problems (such as create a Java class, or an FTP block in Perl, etc.). So developers are tempted to just purely copy & paste the code (in a way re-use). Is this an incompetency? I have done this myself, though I think I am a better programmer than many others I have seen. Who has the time to RTFM? In this age of information abundance, I do not think that copy & paste programming is bad.

Isn't that what sites like Stack Overflow do anyway? People ask - OK, here is my problem - how to solve it? Now someone will post complete code and the person who asked the question would simply copy & paste the most voted answer. No matter how small the problem is.

I am working with a bunch of young coders who heavily rely on the Internet to get their job done. I see convenience in copy/pasting and modifying code to get the job done. For example, you may be quite good with algorithms and such, but you may not know how to use a BufferedReader in Java - would you read complete the Javadoc for BufferedReader or look up some example of using it somewhere?

What are the real dangers of copy & paste coding that can impact their competency?

share|improve this question

marked as duplicate by gnat, Kilian Foth, MichaelT, BЈовић, Jalayn Jun 10 '13 at 12:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

38  
I hope I never have to work with your code. –  FrustratedWithFormsDesigner Jun 28 '11 at 15:30
14  
Who has the time to RTFM? - MAKE time to read manuals so you don't have search for how something works and learn how to move your programming forward. –  StuperUser Jun 28 '11 at 15:35
9  
...and be grateful that you HAVE an M to RTF. When you get some proprietary system where you have to trace out the interactions with a pencil and paper you'll wish you could RTFM. –  FrustratedWithFormsDesigner Jun 28 '11 at 15:37
3  
Normally I +1 questions that have negatives because I think negativing questions you don't like to be a bit obtuse. This time though, the cognitive dissonance that giving you kudos would create for me could send me off the deep end. I just can't risk that this time. –  Crazy Eddie Jun 28 '11 at 16:21
4  
+1 just because the title of the question "is copy paste programming bad" itself is a perfectly fine and valid question. I don't agree with the position "who has time to rtfm" but I don't see what's wrong with the question itself. –  Ken Li Jun 29 '11 at 4:04
show 10 more comments

15 Answers

up vote 40 down vote accepted

Is it bad? Maybe... for learning small examples, for testing out a concept, it's not that bad.

BUT... you have to understand what you are copy/pasting. Otherwise, how do you know that the code is really doing? Sure, it gets the one result you want on the screen but maybe it has horrible performance, maybe it has security holes, maybe it causes memory leaks, maybe it summons Cthulhu, maybe it will cause customer credit card numbers to be leaked, maybe it contains a backdoor...

And most likely, maybe it requires some tweaking to meet business requirements and if you don't understand the code you will not be able to properly tweak it (or better yet: write a more "correct" version)...

As for "RTFM", yes, I do when it's available. I would read the BufferedReader javadocs, and if I can't get enough information to get my code working, I would then hit Google and search for "Java BufferedReader example". I would not expect the code I find to work immediately with my code, but I would expect to find a simple working stand-alone sample that I can use as an example to correct my own code.

And when it's your own code that you are copying/pasting, that's usually a sign to start refactoring.

share|improve this answer
7  
+1 for emphasis of understanding code. –  StuperUser Jun 28 '11 at 15:34
    
Right, there's a huge difference between blindly c&p'ing some code and taking an example and running with it. I pretty routinely will do a google search, look on SO w/ a problem, but I make sure I understand why the code block I'm looking at helps me - such that I can solve that problem on my own the next time, not to mention so I can tailor it to my code and not tailor my code to it. –  geoffjentry Jun 28 '11 at 20:49
5  
+1 for Cthulu... –  MattyD Jun 29 '11 at 3:20
add comment

You can solve some problems by doing it like that. But not all problems; it's not very worthwhile.

Also, it is plagiarism.

It is OK to reuse code to some extent, but make sure that you understand the code, and that you don't violate copyright. For most common programming tasks, libraries, frameworks and toolkits exist, use those when available.

share|improve this answer
3  
Make the most of available libraries, frameworks, and toolkits. If you are tempted to copy/paste in your own code, make a library module of the code, and call it from both the source, and target of the copy/paste. You should find this will save you lots of development time. –  BillThor Jun 28 '11 at 18:32
add comment

Copy-paste programming presents two problems.

  1. there is some concern about copyright infringement if you do too much of this.
  2. eventually a problem will come up that you cant solve with copy-paste programming, when this does happen you will not have gained the skills required to solve it on your own.

Other than that, for solving small problems, copy-paste programming can help solve them in a quick and efficient manner.

One final note, obviously we all rely on other people for help from time to time solving problems, if you take the time to understand the code given and implement it you will have learned something as well as solved your problem.

share|improve this answer
add comment

It's actually quite a good idea to know what your code is doing. I would recommend reading Code Complete

Widely considered one of the best practical guides to programming, Steve McConnell’s original CODE COMPLETE has been helping developers write better software for more than a decade. Now this classic book has been fully updated and revised with leading-edge practices—and hundreds of new code samples—illustrating the art and science of software construction. Capturing the body of knowledge available from research, academia, and everyday commercial practice, McConnell synthesizes the most effective techniques and must-know principles into clear, pragmatic guidance. No matter what your experience level, development environment, or project size, this book will inform and stimulate your thinking—and help you build the highest quality code.

Discover the timeless techniques and strategies that help you:

  • Design for minimum complexity and maximum creativity
  • Reap the benefits of collaborative development
  • Apply defensive programming techniques to reduce and flush out errors
  • Exploit opportunities to refactor—or evolve—code, and do it safely
  • Use construction practices that are right-weight for your project
  • Debug problems quickly and effectively
  • Resolve critical construction issues early and correctly
  • Build quality into the beginning, middle, and end of your project...
share|improve this answer
5  
+1 for "It's actually quite a good idea to know what your code is doing." –  Anto Jun 28 '11 at 15:37
    
So do you mean to say, I google up some piece of code, then understand it, and then copy paste it, I am all good? –  ring bearer Jun 28 '11 at 15:45
2  
In some cases but not most. The chance of you finding code on the web that with no modifications complies with your naming conventions and coding conventions is tiny –  Tom Squires Jun 28 '11 at 15:49
5  
If you find a an example, understand it then adapt it to your needs then its not copy paste programming. –  Tom Squires Jun 28 '11 at 16:04
1  
@ring bearer: The prejudice would be against coders that only change some variable names (yes they adapted the code, but in such a way that some smart IDEs can do it almost as well - if your coder is no better than your IDE's refactoring tool then there's a problem) to get the copied code working without trying to understand how it works. –  FrustratedWithFormsDesigner Jun 28 '11 at 16:32
show 2 more comments

When we are talking about copy paste programming as a bad practice, we mean the copy paste programming that is done out of lazyness. Instead of programming a proper base class to eliminate code smell, some just violate DRY out of lazyness. Also, it introduces a lot of bugs, because you then tend to ignore the subtle differences of the use case and don't apply the necessary changes. That's mostly why it's considered a bad practice.

I don't think it's generally a bad practice to take a piece of code here and there from the Internet. But I do think it's a bad practice when you take a piece of code that you don't understand, as you introduce code to your software which no one can maintain.

Moreover, just look at websites like The Code Project. Every article provides a solution to a problem. Most solutions are quite good. But when I review the implementation, every second or third of them makes me want to vomit, and I would not allow such a piece of code to appear in my software.

share|improve this answer
add comment

It's perhaps the worst possible way to program.

is this an incompetency?

Yes. If someone can't understand it well enough to write it themselves, they really need to find a new job where they aren't expected to code.

More importantly, if someone can write the unit test case, they must be able to write the code. Or prove that the problem can't be solved in a reasonable amount of time. If there are technology issues, training is essential. If there are algorithm issues, then training is also essential.

Who has the time to RTFM?

Everyone.

Isn't that what sites like stack overflow do anyway?

No.

the person who asked the question would simple copy paste the most voted answer.

The most-voted code may not work as expected. It may not be optimal. It may not handle boundary conditions or special cases correctly. Indeed, it may not even work for the questioner's architecture, even though it did work for the person answering.


would you read complete Javadoc for BufferedReader or look up some example of using it somewhere??)

Read the entire javadoc. Always.

What are the real dangers of copy paste coding that can impact their competency?

Rule 1: Software is just encoded knowledge.

If there's no knowledge behind the software, it's random junk that appears to work for a few examples. There's no value in that.

share|improve this answer
    
What if it's something like a complicated mathematical algorithm to say, invert a matrix using LU decomposition? Granted, there's usually a math library I can link/include, but is it okay to use math libraries I don't completely understand? Is it okay to use copy/pasted code I don't completely understand (granted I follow copyright rules)? Both? Neither? –  Chance Jun 28 '11 at 20:31
1  
@Chance if you link to a lib you don't need to know the implementation details of the lib. But if you copy & paste source code from it you'd better make sure you understand exactly what that code does because when there's a problem with it, for example there's a bug in it, then you are expected to know how it works. –  Jesper Jun 28 '11 at 20:37
5  
When you use a library, you're not copying and pasting anything. You're calling a library, which is maintained and supported by someone else. When you copy and paste, you are now the maintainer and supporter, therefore, you must understand the code you have undertaken to create. –  S.Lott Jun 28 '11 at 20:38
    
@Jesper, S.Lott, makes sense. Thanks! –  Chance Jun 28 '11 at 20:43
2  
Applying this logic to forking a project on GitHub leads to interesting issues. You are now the maintainer of lots of code that you didn't write and now must maintain. –  sixtyfootersdude May 8 at 13:54
add comment

Isn't that what sites like Stack Overflow do anyway? People ask - OK, here is my problem - how to solve it? Now someone will post complete code and the person who asked the question would simply copy & paste the most voted answer.

That's certainly not how I view the site. I view it as a place to get help, not get your job done for you.

Further, you know how often people vote up wrong answers? Very.

All I can really say is, "Wow!"

share|improve this answer
    
I meant small tasks not the complete project. But I have seen students posting their homework questions and successfully getting answers as well. You and I probably see SO like sites in a different way - but not everyone. –  ring bearer Jun 28 '11 at 16:23
    
I see co-workers visiting SO at work all the time for specific work-related problems. Not just high level, 'I want to learn' type things. –  Rob P. Jun 28 '11 at 23:11
add comment

Copy-paste has two implications, both of which can be bad.

When copying code from one part of your own codebase to another part, you are violating the single responsibility principle of SOLID code design; you now have the same line of code residing in two places in your codebase, and if one of those lines of code change you will likely have to go find and change the other (IF you still remember it's there). You have to ask yourself why you need the code in two places, and whether there is anything you can do to keep the code residing in one place while using it from two other places.

Copy-pasting a line of code is almost always a signal that the code should instead be extracted into a method, or placed in a loop, allowing it to be remotely accessed and/or iterated through.

Copy-pasting from a website or blog is not, in itself, inherently bad; it's working code that you are adopting for your use without reinventing the wheel. However, it is critical that you understand the code you are copying, because if you do not understand the how and why, it will be difficult for you to maintain that code. Also, however well they work, there are some really badly-designed code snippets out there, that if copied-pasted will become a nightmare just by virtue of their layout and architecture.

share|improve this answer
add comment

The problem with working directly (and solely) from an API reference like Javadoc is that it often doesn't provide sufficient context to perform a complete workflow. Certainly it's helpful to understand what the API can do and what arguments and individual methods are available, but plugging that into a non-trivial real-world application is something different.

For Javadoc specifically, I find that only a few Sun/Oracle javadocs have sufficient overview text to give context (but those themselves provide the same copy/paste code). For the most part, even Sun/Oracle don't give a complete context and it's rare indeed to find a complete and fully-written third-party Javadoc.

I don't think that this is the fault of Javadoc-writers, rather that there's often an impedance mismatch between API methods and higher-level task blocks.

share|improve this answer
add comment

This kind of coding is called Cargo cult programming. As a Ruby on Rails developer in a very professional company we had to handle several projects that customers asked us to refactor, after another company had managed to mess them up this way.

In Ruby on Rails programming (or web programming in general) this is a bad habit, maybe caused and assisted by the general use of many tools and plugins and the large amount of code samples and tutorials available. You will end up with code that has no meaning in the context of your specific project and with code that relies on functionality that you don't understand, can't change and can't debug.

Even plugins meant to be included this way can fail. (Remember, that an early version of acts_as_taggable run in an infinite loop when you tried to delete a tag). Code posted on the web is rarely tested even within the context it was written, less so for your special needs.

That's clearly the dark side of the force. You don't want this in your project.

share|improve this answer
add comment

You mentioned all the young programmers do it; that should tell you something. If I'm building a prototype, I'll probably do this a few times because I'm learning/trying to see if something will work. The problem is when you end up building everything using junior level strategies. You end up with a "I've always done it that way and it worked." mentality and risk never growing out of it.

If you want to become a better programmer, you'll need to develop some fluency. Until you repeatedly apply and use what you've learned, this is difficult to develop. The amount of things you need to Google should decrease. Creating your own reference area should prevent looking up the same thing over and over. If you're doing that, then you are creating a major copy & paste violation. Probaly worse is copying and pasting your own code.

share|improve this answer
add comment
  • Every good professional in any field copies what has been created by others in that field.
  • Why should I reinvent the wheel myself if there is one that already fits my needs?
  • Code reuse is the holy grail, we use libraries, components, open source projects extensively; why should we reject this particular form, if it works fine?
  • Patchwork programming is a very productive pattern, especially if copied code comes from an authoritative source or has been scrutinized by a community.
  • If you are not good at programming then you are not good at copying others' code, that's a fact.
  • This is not about copying code inside an application, which is to be avoided as much as possible.
share|improve this answer
    
my understanding almost resonates with this. 95% of the time plain copy paste will not work. The person copying and pasting has to scramble through the code a little bit. At the end it turns out to be productive –  ring bearer Jun 29 '11 at 17:32
add comment

My opinion is that copying and pasting examples/solutions from the web is a valid approach....like anything else, it can be abused.

If you work in any modern language there is a ton of code you've copied and that you don't fully understand (at least, for 99% of us). I don't routinely view the source code of the class libraries I use. I don't know the details of how Array.Sort() is implemented. I have zero understanding of what went into the Infragistics controls I use on my winForms applications.

A lot of people have an emotional objection to copying-and-pasting code from the internet; but I don't see why it's any different. Why is adding a function and encapsulating logic I didn't bother to write or understand worse than including a reference to a library and encapsulating logic that I didn't bother to write or understand?

People will say, 'But, but it could be bad code!' but that's true of any library you use. Sure, with the standard libraries you can hope that they were well implemented, but that's not always the case. Everything else is just 'stuff' on the internet. Anyone can post 'stuff'. You can 'kinda' trust reviews and ratings; but sites like SO let people rate posted code snippets the same way that CodePlex and others do.

As developers we might recognize a difference between calling a library and code you've pasted. Your users will not. They won't care. I spent hours jumping through all sorts of hoops because a 3rd party component we built our application around had a memory leak and would eventually crash the app. The fact that it was someone else's library didn't make it any easier to fix (actually, arguably, it was harder because I didn't have the source).

Pasting code is no substitute for knowing how to code. But knowing how to code doesn't mean you can't use existing code.

share|improve this answer
1  
You don't maintain Array.Sort()... Not until you paste the implementation into your own code and use it in lieu of the library version. For the most part, you shrug and let Microsoft and Windows Update take care of the framework, but no such luxury is afforded you for those snippets you pulled of the 'Net. And if you're gonna maintain it, you'd better understand it... (oh, look, S.Lott already had this discussion earlier) –  Shog9 Jun 28 '11 at 23:29
    
Microsoft has no legal obligation to me or my product; if I found a bug in Array.Sort() tomorrow they don't have to fix it. I'm not implying that MS wouldn't. But I am saying anytime you use someone else's code, via library or cut/paste you are assuming responsibility for that code. If it doesn't work, your users are going to blame you. I'd say the .Net Framework is a pretty safe risk to take; but there are certainly companies that produce .Net libraries one day and then close shop the next. –  Rob P. Jun 28 '11 at 23:37
    
the risks of relying on libraries, especially libraries where you don't have the option to patch when critically-necessary, is worthy fodder for a whole question in and of itself. But the fact remains, Array.Sort() is someone else's responsibility - if they drop the ball, that sucks for everyone, but assuming they don't it's one less thing for you to worry about. But bring code into your own app, with no one else maintaining it, and it's on you to find and fix problems. If you wait to understand it until there's a problem, you're waiting for a fix from yourself - worst of both worlds. –  Shog9 Jun 28 '11 at 23:42
    
Most of the time ( in enterprise app world ) some team develops the code and that team would not be responsible for maintaining it. So there will not be much of a sense of "ownership". That means one can carelessly pull whatever code works for him and ignore whatever happens later. –  ring bearer Jun 29 '11 at 18:47
add comment

Copy and Paste program means two different things to me. One is that you stitch together other code examples to get a program. The other thing is that you are cutting/pasting a bunch of repetitive code and then slightly modifying it. I think to some degree we are all stitching together code examples, if not by cutting/pasting explicitly then by memory. On the cut and paste repetitive code thing, it is a cost versus reward. For a small amount of cut/paste or even a large amount if you do not have time to do it right, it may be worth doing the cut and paste....but in general at some point with a large amount this will come back to bite.

We all copy/paste programs. A lot of code is boiler plate and so are the algorithms. If you are writing quick sort, you did not invent it. Most likely you memorized a typical implementation from somewhere, either a lecture or a book. You are sort of copy and pasting programming. Sometimes you may read a paper where an algorithm is presented and implement the algorithm in the paper. To me this is copy paste programming as well. Additionally most libraries are full of example. Aka open a buffered reader in java BufferedReader br = new BufferedReader(System.out); I'm pretty sure I saw that code somewhere, and if not the code exactly....the basic structure/pattern with my own names replaced...... .NET is very good for having code examples throughout the library. If I want to open up a web page, I will look up the class for making web requests, and then mimic the code example. Sometimes you come back and tweak later (e.g. you need to set up timeouts) but in general you are basically going by example.

Overall, I would say most programs are sort of copy/paste programs. You start with core examples/algorithms/etc. taken from memory, books, etc. and then you modify to suit your needs. As long as you understand what you are stitching together I think it is fine. You don't need to understand everything you are cutting and pasting, exactly, just in general what it does and any functions. It is really just like using a library except you also have the underlying source code. It definitely pays to go to the right sources, and in general if you can get a feature from cutting/pasting from code examples, or a library...it is better to go with the library as that can be updated later.

The second thing is just cutting/pasting your own code. You have a bunch of repetitive code, so you cut/paste and modify. Large amounts of this make a mess to change, and also it is very boring and mind numbing... Sometimes the right alternative is to use another approach, but other times cut and paste is the best way to do what you want, in which case you should create a code generator to handle the repetitive code. But if it is just a few lines or a one off thing, it may not be worth it. Cut/pasting similar code is often quicker than coming up with a complex abstraction or creating a code generator. At the end of the day if cut and paste saves hours and you are under the gun to get a product out before your start up goes belly up, then do it. After all if you do not ship the code, you won't have a future. But basically this all becomes technical debt that should be cleaned up later. It's all about understanding the tradeoff you are taking and managing when it bites you. E.g. rush he project out today so you don't go out of business, then end up fixing it next week when the duplication makes adding that new feature too hard.....

Overall we all cut/paste mentally for examples/algorithms. And on the second type it is all about knowing when it is appropriate and when it is not...... Like any tool you need to understand the tradeoffs of what you are doing....

share|improve this answer
add comment

By copy-pasting code, I would assume that it is a code snippet that get copied/pasted. Code snippets are mostly for demonstration of the idea or the algorithm, not for real production because it might not align to your context. Worse than that, it might contain bug because the author of code snippet might have never tested the code itself at all.

Real life example? Yes there are plenty and I ran into one like last year. My colleague was trying to do Base64 encode/decode in iOS, and he got this answer

http://stackoverflow.com/a/3411653/397807

which I later found out that it got a nasty bug here:

char *outputBuffer = malloc(outLength);
outputBuffer[outLength] = 0;

Well, if you know C well, you might be able to spot the error. The author just malloc the buffer short by 1 (yet he still managed to get 8 votes..). My colleague just naively use this code for Base64 and it worked fine most of the time. This cause tremendous headache to all the team as we were very beginners of iOS development and the app just got crash out of nowhere. Everyone checks their code again and again, including myself, but we never found what was going wrong. Until one day, I think the bug decide itself that it should show up. The app crash itself in the simulator and I got some hint from stack trace that it's there. I spotted the bug, fixed it and case closed. That was after a lot of complaints from end users that our app always crashed for no reason. The damage has been done.

So where's the merit of the story, you ask? The merit is that the coder is responsible for any code submitting to the codebase, and one bad coder can cause wasted hours for the whole team. Copy-pasting the code is not a bad idea per se, but many programmers just blindly believe what internet says and put the code there without validating it and without understanding what it really does. When bugs show up, the very last thing they would think is the bug is in their pasted code because they always think internet codes better than himself. If you got many programmers colleague doing copy-pasta code most of the time, then be warned.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.