5 Ways You can Learn Programming Faster
转自http://www.cprogramming.com/how_to_learn_to_program.html
Learning to program isn't something you can do in an afternoon, but it doesn'thave to be a life's work, either. There arelots of things you can do to make it easier on yourself when you are learningto program. You already know about The 5 Most Common Problems New Programmers Face--And How You Can SolveThem. Now, discover how to get the most out of your learning.
One common theme across many of these tips is:
don't go toofast; get it right before moving on.
When I was teaching C, there were always a few students who came into the classknowing a bit about programming. Inevitably, some of these students did greatin the first few weeks only to fall further and further behind as the coursewent on. Why? They went too fast through the introductory part of the course,thinking they knew it all--but they rarely did. They knew some of thematerial, but not enough to have a strong grasp of the fundamentals.
At the same time, you must not stop making progress--you can go too slow aswell as too fast. Don't avoid a topic after you've mastered everything leadingup to it. By facing more challenging ideas, you'll help cement your grasp ofthe basics.
1. Look at the Example Code
Reading is usually about the words on the page, but learning to program isabout code. When you're first learning to program, you should make sure tolook at, and try to understand, every example. When I first learned toprogram, I would sometimes read the code examples before the text, and try tofigure out what they did. It doesn't always work, but it did force me to lookat the example very carefully, and it often helped make the writeups clearer.If you want to see what sample code looks like, you can read this site's introductory programming tutorial. This tutorial spends a great deal of timetalking about the sample code to help you work through exactly what the codedoes.
2. Don't Just Read Example Code--Run It
But when you're reading a programming tutorial (or book), it's easy to look at thesample code and say "I get it, I get it, that makes sense". Of course, youmight get it, but you might not get it, and you just don't know it. There'sonly one way to find out--do something with that code.If you haven't already, get a compiler like Code::Blocks set up.
Then type the sample code into a compiler--if you type it, instead ofcopying and pasting it, you will really force yourself to go through everythingthat is there. Typing the code will force you to pay attention to the detailsof the syntax of the language--things like those funny semicolons that seem to go after every line.
Then compile it and run it. Make sure it does what you think it does.
Then change it. Software is the most easilychanged machinery on the planet. You can experiment easily, try new things,see what happens; the changes will happen almost immediately, and there is norisk of death or mayhem. The easiest way to learn new language features is totake some code that works one way, and change it.
3. Write your Own Code as Soon as Possible
Once you understand something about the language--or even if you'restillgetting your head around it--start writing sample programs that use it.Sometimes it's hard to find good ideas for what programs to write.That's OK, you don't have to come up with every idea at the beginning.Youcan find some programmingchallenges on this site.
You can also reimplement the examples from the book or tutorial you arereading. Try to do so without looking back at the sample code; it won't be aseasy as it seems. This technique can work especially well if you tweak the sample code.
If you can't think of a small program to write, but you have in mind a largerprogram you want to implement, like a game, you could start building smallpieces that you can later use for a game. Whether you use them later or not,you will get the same useful experience.
4. Learn to Use a Debugger
I already talked about the importance of debugging in The 5 MostCommon Problems New Programmers Face--And How You Can Solve Them. But itbears repeating; the sooner you learn good debugging techniques, easier it willbe to learn to program.The first step in doing so is to learn how to use atool called a debugger,which allows you to step through your code.
A debugger will allow youto step line by line through a piece of code. It will let you see the valuesof variables, and whether the code inside an if statement is executed.
A debugger can help you quickly answer questions about what your code is doing.
int main() { int x; int y; if( x > 4 ) // <-- what is the value of x here? { y = 5; // <-- did this line of code execute? } }
A final word about debuggers: the first time you learn about a debugger, itwill take you longer to fix the problems with your code. After the tenth or sobug, it will really start to pay off. And believe me, you will have way morethan ten bugs in your programming career.
I often saw students unwilling to use a debugger. These students really madelife hard on themselves, taking ages to find very simple bugs. The sooner youlearn to use a debugger, the sooner it will pay off.
5. Seek out More Sources
If you don't understand something, there's a good possibility the way it wasexplained just didn't click.First, look for alternative explanations. The internet is filled withinformation about programming, and some explanations work better for differentpeople; you might need pictures, someone else might not. There are also lotsof good books with detailed explanations.
But if that doesn't work, the easiest way to figure out where yourmisunderstanding lies is to ask someone else. But try to go beyond saying, "Idon't understand. Please explain." You're likely to get a link back to thesame text you didn't understand. Instead, rephrase your understanding of thetext in your words. The more your question reveals about what you arethinking, the easier it will be for a knowledgeable expert to answer it.Programmers sometimes have a reputation for being grumpy about answeringquestions, but I think the reason is that they want to make progress ina conversation, and that requires both sides to put in effort. If you ask asmart, detailed question that shows you are thinking, you will generally getgood results.
There are plenty of places you can go to ask questions. You can always email me, or post on our message board, or ask an expert.
In the coming days, I'll be writing more about how to learn to programeffectively. Stay tuned, by subscribing to our RSS feed,signing up for email notifications, or following @alexallain on twitter.