更快学习编程的5个方法

编程不是一个下午就可以搞定的事,但也用不着花一辈子的时间。学习编程的过程中可以做很多的事情使这个过程更轻松。你已经知道了程序新手的5个最常出现的问题(http://www.cprogramming.com/beginner_programming_mistakes.html)——而且怎样去解决它们了。现在从你的学习中发掘更多的东西。

许多小建议的共同内容是:不要赶进度,目前的搞对再前进。
我教授C的时候有一些来上课的学生不知道怎么编程。最终是前几周下了不少功夫但还是离课程进展越拉越远。为什么呢?他们在课程介绍部分过的太快,觉得他们都知道这些——但他们知之甚少。他们只知道一些材料但还不能对基础有个坚实的领会。

同时也不能停止编程——过的有多快那可以编的有多慢。在掌握在一个主题之前的所有东西后也不要放过它。通过面对一些很有挑战的想法,可以帮助巩固基础的理解。
1.看示例代码
阅读是看页面的字而学习编程是则是看代码了。当开始学编程的时候一定确保仔细查看并尝试理解每个例子。我开始学编程的时候,先读一些示例代码再写程序代码,先理解清这些程序要干什么。尽管不经常奏效,但这个方法迫使我看代码非常仔细,而且让所写的东西(writeups)更清晰。
如果想看示例代码是什么样的,可以看看网站上的编程介绍教程。这个教程花了很多时间讨论示例代码从而帮助搞清这些代码是干吗的。
2.别只看不运行
看编程教程或者书的时候很容易看着示例代码就说“我明白了,我明白了,就这么回事”。当然你可能明白了,但也可能啥也没明白。只有一个办法可以验证——运行代码。
如果你还没运行,那就搞个像Code::Blocks的编译器吧。把代码敲进编译器里——如果你是敲的而不是复制粘贴的那么你就会迫使自己了解所有的细节。敲代码也会让你注意到语言的语法细节——像有趣的分号几乎在每一行后面。
接着编译并运行代码。看是否它做的和你想的一样。
再改改代码。软件是这个星球上最容易被改的装置了。你可以实验一下,试试新的代码看会发生什么;改变几乎是立即的却没有死亡或者伤害的危险。学习语言的新特性的最简单的方法是以一种方式运行代码再改变它。
3.尽可能写自己的代码
一旦你理解了像语言这样的东西——甚至如果你仍然想着这些东西——那就写些用这些东西的程序吧。有时很能想到写一个程序的好点子,没事,你不必在开始想出所有的想法。
网站上可以找到一些有挑战的程序。还可以从你看的教程和书上重新实现这些示例程序。试着不要看示例就会发现不像它看起来那么容易了。这种方法对示例代码尤其适用。

如果你想不出小程序要写那就想象要写一个很大的程序,比如一个游戏。你可以开始建立一些游戏中能用到的小部件。不管以后用得着用不着你都可以获得有用的经验。
4.学着使用调试器
在程序新手常见的5个问题中提到过调试器的重要。但主要是靠重复;你越早学好调试技术,就会越容易学会编程。
首先一步是学会那个叫调试器的东西,它可以跟踪代码。
一个调试器会允许你一行一行的通过程序片断,可以看到变量的值if语句中的代码是否执行。
[code]
int main()
{
    int x;
    int y;
    if( x > 4 )  // <-- what is the value of x here?
    {
        y = 5;   // <-- did this line of code execute?
    }
}
[/code]
最后再说调试器:第一次学调试会花很长的时间定位代码中的问题。[]我经常看到不情愿用调试器。这些学生真的自己让生活痛苦,花费无数时间去找一个简单的bug。越早使用调试器,越早得到回报。
5.找到更多的源代码
如果不理解一些事情,那么很可能是解释的方式不到位。首先,查看多个解释。网络充满了编程的信息,对不同的人群一些解释会更起作用。你可能需要图片,别的人却不一定。同样有很多关于细节解释的书。

但如果没起作用,那最简单的找到自己是否误解的方式就是去问别人。但别光说,“我不明白,给我说说”那样只可能得到同样不能理解的文本。用自己的话重新叙述一下对文本的理解。你的问题暴露你的所想越多,对一个专家就会越容易回答。程序员有个不好的名声:在回答问题时很暴躁,原因我想是他们希望在谈话中有所获得,这就需要双方都作些努力。如果你问一个机敏细致的问题就会说明你在思考,就一定会得到理想的结果。

有很多地方可以发问。可以email给我,或者发贴到消息公告栏问专家。

 

http://www.cprogramming.com/how_to_learn_to_program.html

 

原文:

5 Ways You can Learn Programming Faster
by Alex Allain

Learning to program isn't something you can do in an afternoon, but it doesn't have to be a life's work, either. There are lots of things you can do to make it easier on yourself when you are learning to program. You already know about The 5 Most Common Problems New Programmers Face--And How You Can Solve Them. Now, discover how to get the most out of your learning.

One common theme across many of these tips is:

don't go too fast; get it right before moving on.

When I was teaching C, there were always a few students who came into the class knowing a bit about programming. Inevitably, some of these students did great in the first few weeks only to fall further and further behind as the course went 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 the material, 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 as well as too fast. Don't avoid a topic after you've mastered everything leading up to it. By facing more challenging ideas, you'll help cement your grasp of the basics.
1. Look at the Example Code
Reading is usually about the words on the page, but learning to program is about code. When you're first learning to program, you should make sure to look at, and try to understand, every example. When I first learned to program, I would sometimes read the code examples before the text, and try to figure out what they did. It doesn't always work, but it did force me to look at 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 time talking about the sample code to help you work through exactly what the code does.
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 the sample code and say "I get it, I get it, that makes sense". Of course, you might get it, but you might not get it, and you just don't know it. There's only 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 of copying and pasting it, you will really force yourself to go through everything that is there. Typing the code will force you to pay attention to the details of 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 easily changed machinery on the planet. You can experiment easily, try new things, see what happens; the changes will happen almost immediately, and there is no risk of death or mayhem. The easiest way to learn new language features is to take 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're still getting 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.

You can find some programming challenges on this site.

You can also reimplement the examples from the book or tutorial you are reading. Try to do so without looking back at the sample code; it won't be as easy 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 larger program you want to implement, like a game, you could start building small pieces 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 Most Common Problems New Programmers Face--And How You Can Solve Them. But it bears repeating; the sooner you learn good debugging techniques, easier it will be to learn to program.

The first step in doing so is to learn how to use a tool called a debugger, which allows you to step through your code.

A debugger will allow you to step line by line through a piece of code. It will let you see the values of 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, it will take you longer to fix the problems with your code. After the tenth or so bug, it will really start to pay off. And believe me, you will have way more than ten bugs in your programming career.

I often saw students unwilling to use a debugger. These students really made life hard on themselves, taking ages to find very simple bugs. The sooner you learn 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 possiblity the way it was explained just didn't click.

First, look for alternative explanations. The internet is filled with information about programming, and some explanations work better for different people; you might need pictures, someone else might not. There are also lots of good books with detailed explanations.

But if that doesn't work, the easiest way to figure out where your misunderstanding lies is to ask someone else. But try to go beyond saying, "I don't understand. Please explain." You're likely to get a link back to the same text you didn't understand. Instead, rephrase your understanding of the text in your words. The more your question reveals about what you are thinking, the easier it will be for a knowledgeable expert to answer it. Programmers sometimes have a reputation for being grumpy about answering questions, but I think the reason is that they want to make progress in a conversation, and that requires both sides to put in effort. If you ask a smart, detailed question that shows you are thinking, you will generally get good 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.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值