How to Learn a Programming Language
Decide what you want to do.
Some programming applications with strong Web presence and good materials for beginners are game programming, Web site creation, automation of common tasks ("scripting"), text processing, and scientific problem solving. If you just think programming would be cool to learn and don't have any specific applications in mind, that's okay, but thinking about what you want to program in advance will help you make informed decisions during your learning experience. Also remember that programming can be a frustrating job if you don't pay proper attention or make too many mistakes while writing code.
Choose a programming language.
When you first begin to learn, choose an easy-to-learn, high level language such as Python. Later, you may move on to a lower level language such as C or C++ to better understand how exactly programs run and interact.Perl and Java are languages for beginners. Research your target application to learn if there are languages you should definitely know (e.g. SQL for databases) or avoid. Don't be confused by jargon like "object-oriented", "concurrent", or "dynamic"; these all mean things, but you won't be able to understand them until you actually have some programming experience.
Find learning resources.
Search the Web for good places to start on the languages mentioned above, and be sure to check the language's home page (if it has one) for an official guide or handbook. Also, find someone who already knows how to program. Online tutorials are nice, but they can be frustrating at times if you can't get answers to specific questions. Sometimes library and videos help a lot.
Start small.
You can't expect to write a bestselling 700-page masterpiece if you have no practical writing experience; programming is the same way. Start with basic constructs and write small programs (10 to 30 lines) to test your understanding of the concepts. Stretch yourself, but don't try to run before you can walk.
Put in the time.
It takes many hours of practicing problem-solving skills on different types of problems before you can call yourself an expert. Project Euler has many small programming assignments, ranked roughly by difficulty, that are useful for honing your skills and keeping in practice. Also learn making flowcharts.
Keep at it.
Programming can be very frustrating, but successfully completing a program can be intensely satisfying and pleasing. Don't give up if you don't understand a concept; programming can be a very abstract thing to learn. When working on a particularly intricate problem, take periodic breaks to let your brain relax and relegate the problem to your subconscious mind. Make a good schedule for working.
Keep learning.
Knowing one programming language is good, but knowing four or five is better. Regardless of what language you use most often, having knowledge of others to draw on will make you a better programmer and better able to understand common constructs and problems in the abstract. So learn several programming languages, especially two or three with different design philosophies, such as Lisp, Java, and Perl. But learn each of them properly.
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.
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.
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.
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.
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.
Seek out More Sources
If you don't understand something, there's a good possibility 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.
Tips
• When you learn something new, it is often helpful to implement it yourself and then tweak the design, predicting the results, to make sure you understand the concept.
• Tutorials of any sort are not sufficient to learn a language well. Try to find people around you who have the same interests, and learn from one another. Browse and join message boards so you can be exposed to the techniques and discussions of a dynamic programming community.
• If you are interested in game programming, investigate Python, C++, and Java. Of the three, C++ is probably the best performer, Python by far the easiest to learn, and Java best able to run on Windows, Mac OS, and Linux without change.
• Learn about Free software. Study the source code of the programs available at the Free software directory. Why re-invent the wheel when you can make it better? Just make sure you understand what you're programming.
• Get involved in a language standardization effort. It could be the ANSI C++ committee, or it could be deciding if your local coding style will have 2 or 4 space indentation levels. Either way, you learn about what other people like in a language, how deeply they feel so, and perhaps even a little about why they feel so.
• For automating tedious tasks ("scripting") on Windows, look up C# (which is similar to Java), Visual Basic ; for other platforms, Perl, Python, and bash (or other shells) are common.
• Talk to other programmers; read other programs. This is more important than any book or training course.
• Print books can be a valuable resource, but ask an experienced programmer for recommendations before dumping money on what may be very well-hyped fluff.
• Languages commonly used in Web development include Python, Ruby, PHP, ASP, and Java. Web developers also need to have a strong basis of HTML and JavaScript (which is not the same thing as Java), and are likely to find SQL useful as well.
• Make use of up-to-date application programming interfaces and official reference materials available from the software publisher.
• References are there to help you. Don't be ashamed if you don't remember everything by heart; that comes with time. The important thing is knowing where to find reference material.
• For most people, programming something that interests them or that they can use is more interesting than textbook examples. Use a search engine to find out about projects that interest you.
No comments:
Post a Comment