0

so I am making a website for myself. I need code about how to get distance from top to an element, and I found it from SO. I'm aware that copy pasting big chunks (a function of 11 lines) of code is not something I should be doing too much. Is it helping me to learn new things if I copy the code and go through it step-by-step, or is it bad to copy code about things that I don't understand?

I understand that searching for, for example "how to reverse a string" is completely fine, but I feel like I'm a bad dev, when I search for harder things I have no clue how to solve. What do you guys think?

3
  • Copy what you need, understand what you have the time and energy to figure out, and don't lie in job interviews. Everything else will sort itself out! A lot of us have times where we feel like bad devs for having to google for code help, its usually referred to as 'imposter syndrome'. – Graham Feb 16 at 20:36
  • Hint: the description of the "stackoverflow" tag tells you what it is not about - if you read it, you will know why I removed it from your question. – Doc Brown Feb 16 at 21:10
  • 3
6

11 lines isn't that much. Go ahead and copy-paste it, but... make sure you understand it! One day you may need to enhance it, expand it's functionality, or modify it in some other way. If you take the time to understand the solution you found, you will have more confidence in making changes in the future, and you'll feel less guilty as a "bad dev".

It's unreasonable to expect every developer to re-invent/re-discover/figure out everything for themselves. Some patterns of functionality are common enough that copy-pasting from StackOverflow is quite common and can save time. And if you learn something from the code you found (and it's really worthwhile to make the effort to learn), that's even better!


For code that is longer and a bit more complicated, or if you just want to give the original author credit, you can add a comment saying where the code came from, for example:

/**
 * This function bars the foo, in a very efficient way.
 * Originally copied from StackOverflow: https://stackoverflow.com/....
 * Modified to ignore foos with foo-score > 5 (2021-02-16), by Peter.
 */
3
  • Do you think that a devs job is more to modify code so that it fits, rather than invent new code? – user385280 Feb 16 at 19:50
  • @Peter No, I don't. But sometimes, you run into a problem that you're not sure how to solve, and in your research you find a snippet of code that solves your problem, sometimes perfectly. It would seem strange to me to discover a solution, then force yourself to re-write it by hand. Why not just copy/paste, and then tweak it as necessary, assuming that you understand what you copied and pasted? And if you don't understand, don't use that code until you do understand. – FrustratedWithFormsDesigner Feb 16 at 20:01
  • 1
    Agreed. What I do to push myself to understand a solution I've found online is to try to simplify the solution as much as I can. For each line I ask myself - is this line necessary? What happens if I comment it out? Is there a native function that does the same thing? Is there another method that requires fewer parameters? Play around with the code you copy to make it your own. – Mike Partridge Feb 16 at 23:47
2

Personally and professionally, I've copied chunks of code far larger than 11 lines, but I make sure I always try to read and understand what is going on or, at the very least, that it isn't doing anything it obviously shouldn't (eg, making sure a complex maths function isn't calling an http request for some reason).

add a comment of the original source question/answer and that it was described to do there, run some tests of your own on it if need be, and use it. Essentially all developers use other people's code. After all, using any open source libraries can be sort of thought of as just advanced copy-pasting someone elses stuff.

And testing it is important - it may have worked for someone elses problem but that doesn't mean it will necessarily work for your problem, even if yours looks to be the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy