DEV Community

Ben Sinclair
Ben Sinclair

Posted on

Do you copy and paste code from Stack Overflow?

I always hear about developers running into trouble because they blindly copy-paste code in from places like Stack Overflow answers without understanding what the code is doing.

For instance, it's brought up most recently in an article here called How to Improve Your Debugging Skills. It's a good article, go read it.

But I'm curious now. When I find an answer to something that had me stumped, I learn from it, understand it, and implement it in my own way. The idea of copying and pasting code straight into my own work seems alien. If nothing else, it would at least need to be refactored to use my variable naming conventions or put into a class or method or something instead of being bare code.

I'm using Stack Overflow as the biggest example, but I don't really mean to limit my question to that one site.

Have you ever pasted anything in like that? If so, are you new to development or an old hand - and has your attitude changed over time?

Top comments (31)

Collapse
 
ben profile image
Ben Halpern

I still find a big part of my workflow around utilities like combining and operating on data structures etc. in different languages, and other go-to commands I haven't memorized, I still search/find/copy/paste.

Basically, some of my knowledge is stored in Stack Overflow and I just remember how to access it. It's still better to actually know things so you don't have your workflow interrupted, but this is what it is for me.

BUT when I was newer in programming, I definitely copied and pasted from Stack Overflow a lot and really relied on it. The big one was jQuery. I really didn't understand how a lot of concepts in front-end web development and the jQuery library. It was a while before I was doing much more than copying a big chunk of code and changing things until it worked.

I can't picture myself doing that today, but this was my life a few years ago.

Collapse
 
tterb profile image
Brett Stevenson

It feels like Jquery is still the big one for me whenever I'm able to go long enough without using it. I usually won't find myself directly copy-pasting code, but I'll definitely spend a lot of time on Stack Overflow.

Collapse
 
ahmedmusallam profile image
Ahmed Musallam • Edited

I definitely share this exact experience. I often times code does not "just flow" out of my head. Even though I know, at a high level, what I need to do. I just need a memory boost. Other times I need something very specific and for various reasons, notably time and effort, I do not want to figure it out from scratch. So I turn to SO because sometimes modifying existing code snippet is a lot easier than writing it from scratch.

Now, this is specific to small code snippets I can ready and understand. Not hundreds of lines of code.

Collapse
 
tux0r profile image
tux0r

There's a library for that.

I, personally, would prefer to understand an answer and implement the suggested functionality instead of just copying and pasting. Debugging code which you have not written yourself can be a pain.

Collapse
 
ben profile image
Ben Halpern

Stack Overflow Import is a joke, but I also think it's kind of a vision of things to come in software development. Let's just hope the future is created responsibly and not out of shiny new thing mania.

Collapse
 
erikerskine profile image
Erik Erskine

I'd agree with your approach to learning from and understanding these answers.

I often search for code snippets to use, but I find typing them in manually a useful excercise, even if I end up with an exact copy. I treat the act of typing code myself like a constraint, ensuring I really do understand what that code does and I'm happy with it. And I'm taking responsibility for it from that point on too - after all, the answer may be incorrect, outdated etc.

I'd extend this principle to tools that generate boilerplate code. Where possible I'll try to generate the output somewhere temporary and consider it documentation. Then I'll copy it over a file or function at a time. Here I may well copy & paste, but the principles of understanding & responsibility apply in the same way.

Plus as you mention, there's almost always something you want to tweak anyway.

Collapse
 
jsrn profile image
James

I mostly find myself checking Stack Overflow when I want the idiomatic way to do something.

A lot of the time if it's just a one or two liner that I can understand easily, yeah, I'll just copy and paste it, but for larger bits of code, I think you are doing yourself a disservice if you just snap up the code without understanding it fully. After all, you presumably don't get to be the kind of developer who can answer questions on SO by copying and pasting everything.

That said, deadlines and stress have pushed me to do a few horrendous copy/paste jobs in my time. I try not to make a habit of it.

Collapse
 
dan_starner profile image
Daniel Starner

My mind is like RAM. Its very fast, and when its in my head, I almost instantly know the code that I have to write. The only problem is that I can't think of a lot of concepts at once, so that's when I turn to StackOverflow and similar sites.

To keep the analogy going, they are like the hard drives to my thought process. They have all the information in the world saved in them, and I know where to look and how to find them, its just a little slower.

As I learn more and become more confident, I use StackOverflow as a double check that I do something correctly, instead of just copying and pasting everywhere.

Collapse
 
kylegalbraith profile image
Kyle Galbraith

I don't copy and paste code from Stack Overflow nowadays, but earlier in my career, I certainly did to get something to "work". However, this is detrimental to your own learning in my opinion.

I definitely Google a lot of different things on a daily basis and read a lot of answers from Stack Overflow. However, I prefer to understand the answer given and compare it to others. This allows me to implement my own interpretation that is often the combination of multiple answers.

Collapse
 
tcelestino profile image
Tiago Celestino

Me too.

Collapse
 
stealthmusic profile image
Jan Wedel • Edited

No I use the concepts.

And actually, if you do so, you violating copyright laws. If there is no explicit licence mentioned, the code is actually simply posted under a personal copyright of the author.
This is important if you use that code somewhere in production...

Collapse
 
moopet profile image
Ben Sinclair

From every StackOverflow page (bottom right):

user contributions licensed under cc by-sa 3.0 with attribution required.

See also the accepted answer for Do I have to worry about copyright issues for code posted on Stack Overflow? on the meta.

Collapse
 
stealthmusic profile image
Jan Wedel

Huh, ich just read the license:
BY -> Attribution required (This is actually not that easy, license material is created automatically in our company, you would need to store and use that information)
SA -> Share alike when modified (This seems to be an issue)

Correct me if I am wrong, but doesn't it actually prevents commercial usage in a proprietary piece of software?

Thread Thread
 
moopet profile image
Ben Sinclair

I think it depends where you use it. Anything trivial (a line or two) I think is basically fair game (notwithstanding things like the true binary farce, etc.)
If it's a function and you're doing more than change variable names then you should credit the author and share changes, but you don't have to open source your whole application.

I think, anyway.

Collapse
 
stealthmusic profile image
Jan Wedel

Wow, I actually searched the page once for license information a couple of years ago. Did that change (I guess not)? Plus, we had some OSS legal consultant that told us, SO falls under copyright laws and we must not copy/paste anything because of legal issues...
So, thanks a lot for correcting my statement, that’s very important!

Thread Thread
 
moopet profile image
Ben Sinclair

Sebastian Baltes has a couple of links about this below that I thought were interesting:

You may find our article about Stack Overflow code snippets in GitHub projects interesting, which has recently been published in Springer's Empirical Software Engineering journal [1]. Beside some information about Stack Overflow's license, we report on an online survey with developers about their awareness regarding Stack Overflow's licensing and its implications (spoiler: they are often not aware of the implications). I also wrote a blog post [2] summarizing some of the results.

[1] rdcu.be/8irZ
[2] empirical-software.engineering/blo...

Collapse
 
thallesaraujo profile image
Thalles Henrique • Edited

Stack Overflow teached me why newcomers in a programming language should hear that advice they hate the most: "read the documentation". When my current project started, the team haven't any knowledge about Python, Django and how the legacy application we would develop modules work. Well, we decided to only read few slides about Django, tried to implement a basic test module, and started the development of real modules. And the code was a complete mess! Without the proper knowledge, we ended doing most things using Django template without need, making so much templates, views and forms and polluting all the code. All the time we don't know how to do a specific thing, we tried to "adapt" something from Stack Overflow. Until the time I become tired of the answers there, and the lack of practice of some of them, which become more like reinvent the wheel to do relatively simple things. So I finally decided to follow the old advice from college teachers... And after reading Django documentation (finding there methods which would be useful), I finally could get ride of that ugly cluttered templates, "dry" my so long code, and getting things done in a more simply and more understandable way. And with this, I could not only wrote a better code, but a more rich documentation as well.

Collapse
 
picocreator profile image
Eugene Cheah

In very limited cases yes. But only to save time, and not without understanding.

At the heart of it, there is nothing wrong copy-and-pasting code, especially glue/boilerplate code. However what I find is the bigger "crime" is to copy-and-paste without understanding.

Understand the very code you are pasting. In programming over a decade, I still find myself sometimes searching up very fundamental pieces of code (eg. array sorting), especially when I jump from one programming language to another.

My benchmark would be for junior programmers. If copy pasting, would probably save you 5 to 10 minutes instead of writing the code manually. With pretty much the exact same result. Then its alright, because as a paid programmer, I too have a responsibility to be efficient in my work. However if you do not understand what you are pasting, stop!

Naturally as you mentioned, naming convention, and syntax styling, if those do not match, I would do a quick refactoring.

Collapse
 
sublimegeek profile image
Jonathan Irvin

It's important to understand the code you are using, but more often than not, I'm adapting the code I'm using rather than taking it for face value.

Remember, SO code solutions are for the scenario of the poster, not your exact solution. So, there's always risk.

On the other hand, if you're desperate, you'll try anything to get unstuck!

Collapse
 
rhymes profile image
rhymes

Sometimes but I tend to understand what I'm doing being an "old hand" as you say :D

There are details I never remember and usually the search engine always sends me to the same stack overflow pages :D

Collapse
 
scottishross profile image
Ross Henderson • Edited

I copy and paste, then mould into what I need.

Collapse
 
ryan profile image
Ryan

I'll often copy two- or three-line snippets. Usually I arrive at stack overflow searching for a problem that I know is possible, and I can imagine a complicated solution that might work. Then I find someone has written a solution that's efficient, elegant and totally understandable.

In that case, why reinvent the wheel?

Collapse
 
vdedodev profile image
Vincent Dedo

Yes, I will copy-paste StackOverflow code.

Paste into a sandbox and make sure it actually runs.
Figure out what parts I need for my bit of work.
Try that approach for my work and check the output/result.
If it works, go read up on it and figure out if that specific implementation/call is the best for my needs.
Write a fresh piece of code with the concept I just tested and learned.

Collapse
 
stegriff profile image
Ste Griffiths

Yes.

I make an effort to understand the code, and I usually tweak it. But yeah! I don't think we should drive impostor syndrome by disparaging this as a junior thing to do.

(6.5 years pro fullstack dev)