One of the nicest things about programming is that there is an absolutely endless supply of examples and tutorials on the web to help you when you get stuck.
Unfortunately, all too often, I’ve seen programmers find a piece of code that does what they’re looking for, and they simply copy and paste it into their program. Copyright issues aside (that is an issue for another day) there are some good reasons not to do this.
Back when I was first learning OpenGL, I was trying to accomplish a task that I was unfamiliar with. I did a web search and found a code snippet that claimed to do exactly what I wanted. Now, I’m sure that in most cases, the method they used would work fine, but for some reason, it wasn’t working for me. I assumed that their code did what it was supposed to do, and that the problem was somewhere else in my code. After several hours of playing with the problem, I discussed it with a co-worker, who asked me a question about the sample code that I had pasted in there. I believe the question was, “Why are you passing a zero into that function?” I had no clue why. That was just what the sample code did. He told me I should figure that out first, before trying to solve the rest of the problem. Once I had looked it up, it became clear, quite quickly, that I needed to be passing something other than a zero to the function for what I was doing. I had just wasted several hours because of someone else’s sample code.
Now, I think we are all very glad that people post tutorials and sample code to help us with tasks. However, these examples were created as a demonstration, and should rarely be dumped directly into your program for a number of reasons.
Instead of copying and pasting, analyze the sample code, and the author’s comments, and figure out why it works. This gives you the knowledge you need to repeat it later without having to paste the sample code again.
If you know why it works, then you won’t be confused about parts of the sample code, like I was. Everything will make sense to you, and if you need to spend time debugging it, you will have the knowledge you need to do so.
Also, sample code is full of poor programming practices. Copying and pasting it puts those same problems into your program. For example, I see a lot of sample code that has functions named doIt() for simplicity. doIt is a horrible, horrible name for a function or method, because you have no idea what it does! I have been surprised how many times I have seen names like this in professional code bases, because the developers copied directly from a tutorial.
Now, there are times where it is acceptable to reuse code on the Internet. But these times are few and far between. (One valid example that comes to mind is reusing an entire library or API that has been developed elsewhere. You shouldn’t feel like you need to thoroughly understand and rewrite all of the code in an API.)
The key is though, when you go to find sample code, figure out the process–how and why it works. You–and your code–will be much better off for it.
View Comments
blog comments powered by