Thursday, September 13, 2012

”I can do it myself!”

avoiding copy/paste programming tutorials.

I Just recently started working on creating my platform for a 3D engine using DirectX to use during my education as a base for trying to implement techniques and ideas that are relevant and new within the game industry. I quickly found a website with a lot of information and code about the subject. I started using this website and the code that it had on the subject, but I quickly realized that I was more just copy/pasting code than really understanding the concepts and how DirectX actually work and in the end, I couldn't really explain the code to my debugging friend (a horse with rainbow mane and a disco ball around his neck, check pic below). This made me realize, that in the end I wouldn't really learn anything from this and instead went for a new source of information: Frank D. Luna's books about DirectX(Link to book). It has been a great source and I highly recommend it if you want to get your hands dirty with DirectX. Not only does it talk about the fundamental logic behind it, but also explains the code and function calls very thoroughly and my understanding of the code I write and DirectX is much improved. 

(My rubber ducking friend, Tony Mane-ro)

I've always tried to avoid doing copy/paste from websites to solve problems that I meet, mostly to make sure that I understand the logic to the solution and also to avoid diving into a subject just to find the solution and then swim back out without really looking around and seeing a bigger picture within the subject. And therefore I thought I would write some ideas and tips to share to help you avoid this, and also, when you write blog posts or reply on threads regarding a problem to help your reader not to do these kind of dives.

When reading about solutions to certain problems that might occur when programming, you can find times where you see a small text with no explanation or very little explanation of the code and huge block of code. In the heat of the moment you can often say “great that chunk of code solves my problem!” (Perhaps with some modification) and you end up just copy and pasting the code right in to your code and continue on. Sometimes this is acceptable if it's a trivial problem you just forgot the name of a function and not really something you have to spend much time thinking of how the solution will really work. But even then, instead of just copy/paste, write the code yourself! Muscle memory can perhaps help you remember that function next time you need it! I would say that you should always retype code instead of just copy/pasting to avoid missing to rename a variable or any other mistakes that could be easily avoided. This also helps you see the actual flow of the code that solves your problem and you might find new ways to write code that treats something within the problem that you haven't seen before, you find better solutions to situations that you perhaps never saw as a problem. This is perhaps something you should also think about when starting a new project where you want to reuse some of your old code. Take a look at that code and evaluate it, is it possible to re-write it so it's faster/smarter/less error prone? Then do that before you copy/paste it or reference it into your next project.
   I believe this is very important when working with production code, it could be something you'll perhaps will need to give to someone else at a later stage of production. If you can't fully understand it, how should your co-worker relate to this? I had an experience like this once when one of my co-workers was working on RnD stuff and was coding towards an SDK he'd never worked with before. I was asked to take a look and see if a could see anything that could solve his issue he had at the time and so he handed me the code he had “Written”. I quickly realized that a lot of this code was just copied and pasted from some website since some of the comments and variable were in spanish... And in the end to solution to his original problem was incredibly simple and he would have stumble upon it if he would have taken the time and read 4-5 pages in the SDK documentation.
   What actually happened was that I re-wrote everything because of the design of the copied code was very strict and I knew that it wouldn't work later when we later woul have to expand the system. So not only did he copy/pasting take more time because of the refactoring, he would have also spotted his problem and solved it with some reading and gained a greater understanding of what he was actually doing.
   Some might see that re-writing code that someone else has posted is re-inventing the wheel, but if you later have to either change the wheel or perhaps remodel the wheel, it could be good to know why it looks like it does when you start.

What can you do to help others avoid the pitfall of copy/pasting code? It depends on what the person is asking for or who is your intended audience.
   If someone on a forum asks for a function within Maya or any other DCC, instead of typing it, point them in a direction where they can find more information about it(like the Maya documentation). This might help them find the solution to another one of their problems in the future. It's like when your trying to solve a puzzle and ask for a hint, you don't want them to give the solution, just a way to find the solution for yourself.
And if you're writing a blog post where you want explain how you solved a certain issue, I would recommend you to talk more about your approach in text than in code. Post the code in segments rather than in a big chunk and talk about those segments individually. Many of the articles you find at , I believe, does this very well!

So start copying code from the internet,
but do it a letter at a time.

No comments:

Post a Comment