Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by MainMa, gnat, Ampt, William Shakespeare, psr Jul 7 at 23:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6  
Your third paragraph is garbled, please fix at as a courtesy to the people writing answers. –  Kilian Foth Jul 7 at 8:08
6  
Will most likely be closed as either "opinion based" or "education advice". Anyway: this is totally ok. There is so much to learn that you simply can't do any different. Also reading code from good programmers is a great way to improve your own skills. Just avoid Cargo Culting where you just plug together stuff you do not understand at all and hope that it will work 'somehow'. –  thorsten müller Jul 7 at 8:09
3  
1  
also bear in mind the copyright/licensing issue implied by copying someone else's code –  jk. Jul 7 at 11:34
2  
Your title and your third paragraph are asking pretty much opposite yes or no questions. If somebody says yes to your title, they will mean no to your third paragraph, and vice versa. Those two need to be reconciled. –  Panzercrisis Jul 7 at 12:29

6 Answers 6

up vote 42 down vote accepted

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?

share|improve this answer
6  
Actually, studying and perhaps contributing to some free software project would teach you a lot ! –  Basile Starynkevitch Jul 7 at 8:19
3  
You have to understand the code. If you use it without understanding it, when it doesn't work, you will be given a wierd look when you can't fix the code yuo "wrote" –  Ramhound Jul 7 at 11:34
    
I would say that the best thing that advanced me in my skill was reading other people's code. It's like the same thing learning another language - you can continously use them and unconsciously use wrong grammar for the sake of using them. But start listening to other people and you increase your vocabulary. –  Secret Jul 7 at 12:33
    
Using other people's code where allowed is also helpful in learning the patterns in the industry. You'll frequently come across things that you don't like, or like a great deal. Using existing code is one way of learning how to identify these, and also giving you a ground to step on to start digging backwards through them. I am a firm believer that the role of a programmer is to solve problems, not just write code. –  Ciel Jul 7 at 12:46
1  
Someone edited the title of the original question, you may need to adjust your first sentence –  igor milla Jul 7 at 14:29

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.

share|improve this answer

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.

share|improve this answer
1  
great answer, now the question might become finding a good source of good writing (code) to read. –  InstructedA Jul 7 at 10:38
    
@randomA thanks! I have updated my answer adding informations about places to find good writing. –  Vitalij Zadneprovskij Jul 7 at 10:49
    
Quote from one novelist that I hold in rather high esteem: "Just write. Every 100,000 words, you improve by one level". –  gnasher729 Jul 7 at 11:03
3  
Bob Frankston, who co-created VisiCalc, once said that reusing pieces of code is like picking off sentences from other people's stories and trying to make a magazine article: it might make your point, but not very well. I'd also add that just because code is open source or in GitHub or Google Code doesn't mean it's good code; I've seen plenty from all of those sources that has a high WTF/minute rate. –  Blrfl Jul 7 at 12:08
    
@gnasher729 you also need to review or make review to somebody else what you have written. Otherwise you it is very difficult to make progress. –  Vitalij Zadneprovskij Jul 7 at 13:46

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.