Question

I'm an inexperienced programmer. I have been assigned to develop a Firefox plugin with no prior experience. So I followed the tutorial at MDN. I learned so many things, it's exciting and overwhelming at the same time.

When I finally started programming, I ended up using extension the tutorial gave me, and modify it. And after realizing there are tons of code out there which pretty much does everything my extension has to do, I ended up analyzing these codes and implant them to mine with some modifications to suit my own needs....

So yeah, basically what I'm asking is: Will spending a lot of your time observing people's code and modifying them instead making my own improve my skill as a programmer in general?

Was it helpful?

Solution

Reading other people's code is in fact a very good habit, since it's the best way to understand what's out there and what the programmer community will presumably be familiar with. Your code has to be understandable by you and by everyone else who will ever have to maintain it, so it's important to acquire an understanding of what is and isn't readable - even if you read really bad code it still serves as a counter-example.

As for reusing vs. implementing your own: reading other people's code and copying it without understanding, in the hope that it may fulfill your requirements, is bad, because it doesn't result in you learning something.

But detecting existing code that does do what you want and reusing it is good, because it's more efficient than writing your own version, and by finding it and determining that it does in fact do the desired thing, you have proven that you could, in principle, rewrite the solution on your own if you had to. The result is that you save time and don't miss out on learning. In fact, researching existing solutions may even be the critical point that teaches you how to do something, so that you learn something new and save time simultaneously.

The litmus test for copying someone else's code is this: do I understand what the code does and reuse it in order to save time, or am I simply throwing one version after another at the problem until I hit on something that works?

OTHER TIPS

Reading and using other peoples code is an excellent way to learn, however it can also be a trap. You don't want to turn into a Cargo Cult Programmer:

http://en.wikipedia.org/wiki/Cargo_cult_programming

The term cargo cult programmer may apply when an unskilled or novice computer programmer (or one inexperienced with the problem at hand) copies some program code from one place and pastes it into another place, with little or no understanding of how the code works, or whether it is required in its new position.

The key test is whether you understand the code you are using. If after taking and modifying the code could you if you needed to write it again from scratch without referring back to the code you copied?

I don't mean if you could write out the exact same code again, I mean could you apply the same algorithm in the same or a better way and achieve the same results.

So long as you properly understand the code then learning from others is an excellent thing to do. But you absolutely need to understand every line of code as you insert it into your program. Understand what it does, how it does it, and most importantly why it is there.

In "The Psychology of Computer Programming" Gerald M. Weinberg has written that programming is a form of writing. What do novelists do to become better novelists? They read a lot of books that have been written by very good writers, so that they can learn. The author is surprised by the fact that so little code is read by programmers.

As was written in other answers, understanding the code that we read is essential to gain knowledge about it.

A good source to find high quality code to read are the various open source projects found for example on github or google code. Beware: not all open source projects contain clean, well-written code. Of course, the quantity of those projects strongly depends on the language in which we write.

Actually it is a good practice to look for existing solutions. In the business world what matters: did you solve the problem? And if you did, how much time it took? Is it a good solution?

As for good (here: legal) solution like told in this comment, always be careful with what is copyrighted, licensed, and what isn't. The tutorials exist for giving examples of the usage of a code, and as you apply, it will serve as base of your program.

Copying existing solutions is an essential method to accelerate your work. Also if you don't do it, you will end up constantly reinventing the wheel.

As you get experience you will notice that some sort of works can be generated, automatized, generalized, etc... there is nothing bad in copying existing code if it is efficient, nice. As others said, it is vital to understand what you do copy, and you might be able to optimize on it. As you understand what you are really doing, you will be able to make generalization and you will avoid the "Copy Paste Programming" which you most probably fear of.

It is important to point out, finding the correct APIs for your work is part of a programmers' job. Actually to use an API and copy a code are similar work methods in this point of view.
It is matter of resource management, I doubt anybody would say, to use an API and solve a problem in 2 days and write a new code (generally doing the same as the API) in 30 days are indifferent.

You will find that almost 80% of the things you want to do are already done. There is a saying about this, "Do not re-invent the wheel. Unless you can do it better."

A large part of programming is not writing code, but studying behavior. Your users behavior, your behavior, your vendors behavior, your workplace behavior. Code review is a key part in this.

As a programmer, it is not your job to know how to code (that is certainly a necessity, but it isn't the core of the job) - the core of the job as a programmer is to be a researcher. To know how to read documentation. To know how to solve a problem. You'll find that you spend less time in your IDE coding than you do on google reading through blogs and tutorials, I suspect.

So no, I do not think this is a bad habit.

I'll do an analogy, how do you learn a spoken/written language better, learning all of possible words and structures or reading/listening?

How do the best writers learn? Possibly reading other great or not so great writers.

And the third and last question, what a kind of programming books have you read? I don't know you but me, books with examples.

Licensed under: CC-BY-SA with attribution
scroll top