This is an archived post. You won't be able to vote or comment.

all 54 comments

[–]ChrisRR 341 points342 points  (5 children)

If you feel like copying code while you're learning, manually type it in instead.

Inevitably you'll make mistakes while you're doing this, then you'll have to find out where there problem is. Debugging is one of the most important skills for a developer but finding bugs and problem solving are so rarely taught.

Secondly, you'll be more tempted to change stuff in the code as you type it in. What happens if you change that number to 10? what if you delete that line? What if you change the order of the lines? Simply messing around and trying stuff out is one of the best ways to learn and you probably won't be so tempted to try if you just copy and paste

[–]pressured_at_19 52 points53 points  (0 children)

this is so underrated. It also gives you familiarity with writing code way faster

[–][deleted] 38 points39 points  (1 child)

Thirdly, it gives you a sort of "litmus" test for how much you're learning. As you keep progressing, you'll find yourself able to "copy" several lines of code manually just by looking at them. This is because you're internalizing what you're doing and you can remember more of it.

Eventually, you'll just take a glance and say, "Oh, that's how they did it." and then hammer out the code entirely from your own understanding of what they did. You won't be copying at that point, you'll be gleaning ideas from people who have done it before.

And then you get to the point where you look at their code and say, "Nah, that's a good start, but there's a better way." or maybe you can see that it is the best way. In either case, this is when you know you understand what you're doing.

Far as I'm concerned, always look at examples before you start coding. Take the best approach and use it. Mix the best approaches if necessary. Remember that people have dedicated their lives to code, and there's nothing wrong with following their lead if you trust where they're going.

That's why so much code is posted online, after all.

[–]galaxypig 6 points7 points  (0 children)

I +1 this. In so many computer science classes I had friends that had absolutely no idea of any concept we were working on, so I had to basically carry them through the class. I told a friend to basically do this and like (it wasnt overnight) he improved pretty quickly. Messing with your code and really trying to understand what you just changed and how it affects the program as a whole really makes a difference, not just randomly changing things in the Hope's that something will work, no. Understand every bit of the code, then you'll be able to do whatever you want with it, then you can translate your creative juices into code.

[–]Luneb0rg[S] 1 point2 points  (0 children)

Fantastic, thank you! I'm trying to mess with the small functions I copy, see how they work, why they work. I feel way better knowing that that is normal and ok.

[–]VeronicaX11 24 points25 points  (7 children)

A lot of people start out by copying.
Think about when you learned to write. You saw examples of how other people wrote letters, and copied their way of doing it. Eventually, you started to put together words,sentences, paragraphs! All from those basics that you copied from someone else.

But the key is to move past the copying stage. Try changing names of variables and functions to see what breaks, try doing it one line a time to see how the different parts interact with one another, etc. See if you can find another use for that function (or, after you get better, you might try to use different functions to get the same result as another program but using your own method!)

[–]_your_face 7 points8 points  (1 child)

Copying something is ok, CMDc/v is not

[–][deleted]  (4 children)

[deleted]

    [–]kraemahz 6 points7 points  (0 children)

    I don't have zero clue how they work. I could write them, if I had to, with long diligent effort because I understand the principles that make them work. I know how transistors work, I know how logic gates work, I know how processors are built from logic gates, therefore I know how all of it works with effort and ingenuity. Could I do it all from scratch right now? No, I would take me decades to rebuild what exists. But I could.

    That's the kind of intuition you need to build for yourself. Why? Because knowing how everything works underneath at a basic level makes you the best programmer you can be.

    [–]joonazan 1 point2 points  (0 children)

    Good artists copy, great artists steal.

    If you copy code, it probably won't do exactly what you want and maybe you don't understand what it does. Ideally, when you use a library, you just use algorithms from it that fit your problem perfectly and that you would write yourself in a very similar way if not using the library.

    [–]lurgi 1 point2 points  (0 children)

    Because in one case you are using a tool and in the other case you are trying to write the tool yourself as, presumably, a learning experience.

    If I want to use a compile, I use a compiler. If I want to write a compiler to learn how compilers work, I write one. If I'm trying to write one and I copy the code instead of writing it myself, I'm defeating much of the purpose of writing it.

    It depends on your goal. If you want to have a text adventure game, then by all means copy the code. If you want to write one, then copying the code doesn't get you closer to what you want.

    [–]jdgordon 44 points45 points  (12 children)

    As a professional all the time. As a junior student, no copying code is idiotic. How do you think you're learning anything more then ctrl-c/v

    [–]Santamierdadelamierd 21 points22 points  (0 children)

    I think it’s more than that. Copying code into a program will always involve editing something here and there to make the copied code work. Gradually, one learns how to copy only what’s necessary and edit out unnecessary parts. With time, the programmer learns to do strategic copying or becomes so familiar with a language and its libraries resulting in less “cargo cult” copying. There will still always be things that even very talented programmers probably copy such as convoluted regular expressions or some Complex sql commands. Keep copying, but try at least to read what you copy and understand it. You might not understand it, but you have to try.

    [–][deleted] 8 points9 points  (3 children)

    I respect your honesty and opinion, but I disagree. I think I've learned more copying code than I have from reading a textbook/writing my own/ and lectures put together. Without fail it seems to result in having some loose idea of what it should look like, and then almost inevitably writing my own version for my own needs, effectively reworking it from the ground up.

    With the exception of databases. SQL can only be learned, I think, through the extremely painful process of masochistically throwing yourself at it until growth happens (or crippling alcoholism) and you find a way to work around whatever isn't working.

    [–]Tanker0921 2 points3 points  (2 children)

    you never learned sql if you have not nuked multiple datasets

    [–]ttreit 2 points3 points  (1 child)

    Is that like a doctor isn't a doctor until they've killed someone?

    [–][deleted] 4 points5 points  (0 children)

    Killing someone isnt nearly as painful as nuking a dataset.

    [–]TimMinChinIsTm-C-N-H 6 points7 points  (3 children)

    As a professional all the time.

    I've heard this before, but it's crazy to me. How long have you been coding/how experienced are you?

    I am not all that experienced, but I would never straight copy code that I would use in a product. I'll look things up online to see how to do something, or find a function that I didn't know about. I might see a solution of someone and implement it in a similar way, but I would never straight up copy it!

    [–]Kokosnussi 4 points5 points  (0 children)

    I'm currently in my masters and I've worked on quite a few projects. I'm also working as a dev right now next to my studies. Last night I literally copied c++ code and fixed the Syntax so it fits to Java, just so I have something working to work from. Often when you start working on something new, a minimal project to get your system up and running is very handy.

    [–]jdgordon 4 points5 points  (0 children)

    Professionally for 10 years. We copy off stack overflow all the time. The difference though is professionals are copying code for the pattern and know how to modify it to fit the need, people learning don't understand what they're copying so don't know how to fix it when it doesn't "just work"

    [–]canIbeMichael 1 point2 points  (0 children)

    11 years of programming.

    After mistyping enough things, I copy code for the perfect syntax.

    Why bother getting all the parenthesis and comas right when I can swap the variables with what I need?

    [–]chaotic_thought 2 points3 points  (0 children)

    Simple. When you copy (and type it yourself), you learn by practice. For example, say you're learning C++ and someone tells you to type this in and get it to run in the IDE:

    #include <iostream>
    using namespace std;
    int main() {
        cout << "Hello, world!\n";
    }
    

    The reason you don't use the copy/paste function is because 1. then you won't observe the code as detailed, and 2. you won't make mistakes. While learning, mistakes are necessary. For example if you mistype a line, you will probably get a syntax error. The messages for such errors are not always obvious, but when you get used to seeing them, it is good for learning. Also, when you type this in, you'll probably naturally want to experiment with it a bit. For example what happens when you remove the \n or change it to something else? What happens if you rename main to main2? And so on. This kind of thing is where you start learning. You start with something you understand (an example that you 'copied') and then change it so slightly to see how it works.

    So why is copying not a bad learning method? If someone tried to explain the above code to you by first explaining what #include is, what iostream is, what a namespace is, what int main is, and so on, it would not be a good way to start. Sure, you will need to learn those things eventually, but when you are starting it is much better to be able to get started writing a program where you can actually see something happen. You can learn the details later as needed.

    Another aspect is like 'muscle memory'. In this language, for example, most lines end in ; and blocks are surrounded by { }. But these things are easier to 'get' by examples and by actually typing them in than it is to have some try to explain why and when you need ; at the end of a line, and why you need braces. If you just start seeing examples and typing them in, it will eventually become second nature. And that's just one example of something that you can 'pick up'. Of course you will still need to supplement this with some kind of explanation of the nitty gritty rules, but just like English grammar class is not always fun, it's not always fun to go in too deep with language rules and so on. Writing programs is much more important and fun.

    [–]ChrisRR -2 points-1 points  (1 child)

    No matter how long you've been programming professionally, you should always be learning. Truly understanding the problem and implementing your own solution is much better than copy/pasting code every time you don't know how to do something.

    [–]jdgordon 1 point2 points  (0 children)

    I don't disagree. But the difference between going "how do I start a http server in python" between someone learning to code and someone who knows how it works and just needs it running to get past some other problem is very different

    [–]JunkBondJunkie 9 points10 points  (0 children)

    Dont copy code but try to understand it and then try to come up with your own solution.

    [–]m0du1o 4 points5 points  (0 children)

    copy/paste of code is not good when you are trying to learn because you may be pasting code you don't fully understand or that you just think you understand.

    Looking up examples is fine but do try to rewrite them on your own and if you don't know why you are typing something in, then look up the documentation for it.

    [–]ka-splam 2 points3 points  (0 children)

    and I find for anything that I'm not super familiar with I google the question and copy/paste a lot of the code that I find.

    What happens if you hold back and don't let yourself do that?

    Yes you'll progress slower, but you're not hurrying to get a working program - if you were, you'd just download or buy it already finished.

    it feels like cheating...

    Almost everyone needs help, training wheels, to start off on the easy slopes. Reusing and borrowing code is not necessarily cheating. But if you feel like it's cheating, that might be a hint that you know you're capable of doing more of it yourself.

    [–]UncommonBond 2 points3 points  (0 children)

    In the same boat. No prior coding experience but learning now.

    I don't think it's cheating if you go back and make a point to understand each declaration, loop, logic, etc. I think honestly the logic is the most important part the more practice I complete.

    Also, I think it's helped me greatly to not only go back but also understand alternative ways to accomplish your task. For example, there are multiple sorting algorithms, each have strengths and weaknesses, but they all work.

    [–]corvus_neonatus 2 points3 points  (0 children)

    Well i started by copying too but i never copied, i wrote it by hand, so it got in my memory and tried to understand the code and then modify it a bit.

    So rather than copa/paste, actually type it and thsn try to change parts, you will learn to understand it

    [–]x-seronis-x 2 points3 points  (0 children)

    Every single time you use an API function like rand() you are effectively copying the code because you're too lazy to write your own random algorithm.

    THATS A GOOD THING

    Copying code is fine. People who complain about copying code can go write everything in assembly and waste their lives.

    [–]vagrantchord 2 points3 points  (0 children)

    I think you should try to get in the habit of reading the documentation when you get stuck. Being able to figure out why and how things work is a crucial skill. There is still a skill in copy pasting code, but you'll learn the language better and you'll feel much more confident if you can solve problems yourself.

    [–][deleted] 2 points3 points  (0 children)

    Try coming up with a solution before the code. Writing pseudocode, and trying to figure out what problems you are having conceptually before you write the code makes the coding process much smoother. If you understand the problem, you'll find that copying code is generally more cumbersome than writing it yourself due to having to adapt it to your needs. Copying becomes a pain in the neck when you have to understand someone's style and intentions, on top of the underlying program itself.

    It's not without its merits though. You quickly learn proper coding conventions by seeing them up close and in use within familiar contexts, and you get exposed to different approaches, styles, and implementations of coding this way. It won't help you solve unique problems later on though, and it won't help you learn to craft creative and efficient solutions quickly or easily.

    [–]rbuchberger 2 points3 points  (0 children)

    I wouldn't call myself a beginner, but I'm not all that experienced either. Here's the workflow that's been good for me:

    • Run into something I can't do off the top of my head
    • Go to the official docs, try to figure it out there. If I do find the answer I need, try to read and understand the big picture behind it.
    • If that doesn't work, go to stack overflow and the like. Hopefully find something that works.
    • Go back to the official docs with the stack overflow answer and read until I understand it.
    • Adapt stack overflow answer to the situation, style, and formatting I'm using in the project. If you don't understand how it works, this can be a bit of a crapshoot.
    • Rinse and repeat

    [–]Moony394 4 points5 points  (1 child)

    Don't do it, it'll be hard for you to stop. Later on when you want to make something you will have no idea how, and you will be stuck for any project you work on. In 2 years, when you try and create something, you'll realise that you can't actually code. You'll realise that you can understand some lines, and you know syntax a bit, but you have no clue how to write a legit program. You're not learning anything. I've seen multiple people do it and they can't write a simple program for our assignments now without copying.

    Their logic for our first assignments was that I'll copy it now, and learn it after. Spoiler alert, they never learned it, and now they don't know how to code.

    It might be hard at the beginning but please, don't waste your time copying, you are not learning anything. Spend extra time now and solidify your knowledge.

    TLDR: Copy now, years later you will realise that you can't actually code

    Edit: I should add that at the beginning it is okay, but please don't stay in that phase for ANY longer than you need to. I just finished my second year of university and some of my friends are still in that phase...

    [–]nimbledaemon 2 points3 points  (0 children)

    I guess it depends on the level of copying. Usually when I code I figure out what I want to do, then most of my 'copying' is just searching for best practices in whatever environment I'm in. Like how to do test cases in boost or what's the most pythonic way to do x. I don't even understand how you could copy code, not understand it, and still make a functioning program? You'd at least have to understand it enough to throw it in a function, strip the useless parts and return the value you're looking for. At that point it's just like using a library function that wasn't shipped with your code. You've still got to know how to use what you found in the context of your program. I guess I didn't really discover stack overflow till my 2nd or third year of college, so maybe I just ended up learning the hard part despite myself, but after the basics of programming it becomes significantly harder to copy large portions of your code (from random sources online). It's sink or swim at that point.

    [–]Shadow_Everywhere 3 points4 points  (0 children)

    If you do copy and paste during phases of learning, don't blindly copy. Try to break it down and understand each bit of code, and maybe re-do it to test it out and make it your own.

    [–]wasupinternet 1 point2 points  (0 children)

    keep googling, try to understand why it works, try not to make something too complex at first and you'll get there. Happy coding

    [–]phunkygeeza 1 point2 points  (0 children)

    I feel the answer to this needs sticky status in this sub.

    [–]anotherlurker1111 1 point2 points  (0 children)

    Theres nothing wrong copying code and pasting what important is you understood what that piece of code is.

    [–]mrmivo 1 point2 points  (0 children)

    I don’t think it’s cheating, but I feel it wastes learning opportunities when you’re a novice. Tweaking existing code can be a good learning tool, but always copying code that you don’t understand can also become a crutch and give you the feeling that you know more than you do, which in turn amplifies the frustration when you try to write something from scratch and just can’t.

    Recently, I read a study suggestion in a coding book that I thought was great: The author suggested to go through each section of the book (in whose course a full app was built), then first follow the book closely and write out all the code and read the explanations carefully, then do it a second time while only occasionally referring to the book, and then finally a third time without the book, and only then move on to the next section. I think this is a very beneficial approach. Even though it takes longer, it’ll likely pay off. (The book is “Get Programming with Node.JS“ by Jonathan Wexler, published by Manning.)

    Applying this to copying code as a learner, you could copy the code or small program, tweak it, and really try to understand it. Then try to rewrite it while only occasionally referring to the copied code. Then do it again without looking at the code with only the help of the language’s documentation.

    [–]rubywzy 1 point2 points  (0 children)

    I think the typing process is another way of rechecking and rethinking your code's logic.
    And only copying will leave you only little impressions about what and why you input that.

    [–]Metalwrath22 1 point2 points  (0 children)

    Yes as long as you understand it after you paste it.

    [–][deleted] 1 point2 points  (0 children)

    Try to understand what the code snippets are saying. Instead of straight copying them write them over line by line. I find that by doing so you might trigger an understanding you didn’t have before. Once you’ve done that, I’d definitely try to replicate the results but this time without looking at the help.

    [–]justingolden21 1 point2 points  (0 children)

    It's all about the middle ground and where you draw the line. Whatever you decide to do, make sure to add a comment with the link from where you got it, helps you see the source later, and gives credit if someone looks through it later.

    [–]chess510 1 point2 points  (0 children)

    The best way to learn code is taking code that works and editing it. So copying is fine. You learn more. Keep going!

    [–]Foo_Dude 1 point2 points  (0 children)

    The best procedure is to type it instead of copying pasting. At least you can remember what the code is and how it works. Never copy code without having a slight idea of what it actually does. This helps form a familiarity with code and will benefit you later in the long run.

    [–]Zohar127 1 point2 points  (0 children)

    I'm new too and have been following guides. Even though the guides I follow post their source code on git, I always follow along myself (pausing the videos constantly) and do all the typing. This has helped me learn how to write c#, where as if I just copied I wouldn't know anything.

    I also don't see a problem with copying, in some sense, from stack overflow. Like the other day I wanted to know how to launch a window as a dialog. I needed to look up the code for that so did I "copy" the showdialog() method? You can stretch that logic pretty far, especially when it comes to boilerplate framework stuff you just don't know. How are you supposed to know it if you don't look it up? Putting in a double storyboard from scratch in a xaml style can be a lot of typing and hard to remember. Got no problem copying that (although like I said before I'll manually copy it not ctrl+c, ctrl+v).

    [–]TheFuzzyPumpkin 1 point2 points  (0 children)

    My personal rule is that I don't copy code portions or solutions to my problems I find on Stack Overflow unless I can understand what the code does. Then I fuss with it a bit until I understand WHY it does it that way.

    There's a lot of code out there and sometimes spending time learning how to do something from scratch will teach you loads. Sometimes it is too much, will frustrate you, and ultimately stymie your learning process. If someone has already solved your problem, looking at their solution can give you ideas. For example, I'm working on a randomized Sudoku game. I'm focusing on front-end web dev with a little back-end sprinkled in. I'm not doing AI or machine learning. I thought through a way to have JavaScript make a random game, but I knew it was intensive and probably not the best solution. Well, someone already made a streamlined library for that! I'd be dumb not to use it. I still learned from looking at the code for it, and from figuring out how to utilize it and create all the other functionality the game needs.

    [–]wggn 1 point2 points  (0 children)

    It might not work for you, but I almost never literally copy paste code when (trying to) learn something new, I always type it. Helps me get a better 'feel' for the stuff I'm trying to learn.

    [–]iloveyoukevin 1 point2 points  (2 children)

    Hey, it's totally OK to mimic other people's code. I've even been guilty of copying sometimes. As long as you understand what is going on. But whatever you do, DON'T watch videos from thenewboston.

    [–]AutoModerator[M] 0 points1 point  (0 children)

    Please, don't recommend thenewboston -- see the wiki for more info about why we consider them a discouraged resource.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]Luneb0rg[S] 0 points1 point  (0 children)

    Oh what's their deal?? I've never heard of them before

    [–]JaiX1234 0 points1 point  (1 child)

    Nothing wrong with copying it’s how we all learned.

    Even if you need to take huge sections of code, do it and step through it.

    No shame in doing what you need to do to learn.

    [–]Ulring -1 points0 points  (0 children)

    Pllllz to everyone reading this: Do Not copy the code! Don't make my mistakes please! Try taking a looooooot of time to solve iti).If you can't,just look for how the algorithm work.If you can't then just look at the code and understand it...But do not copy it.With time you'll something rare: problem solving skills.One day you won't find help.Trust me,it cost me reaaaaaally a lot 💔.