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?
Oldest comments (28)
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.
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.
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.
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.
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!
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.
yes but only Regexes
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
I copy and paste, then mould into what I need.
I copied impossible Regex without shame, but with my own tests.
Rest of code need to be picked with tweezers. Copy and paste without understand what is doing, is like Russian roulette game.
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.
Me too.
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.