The 5 Most Common Problems New Programmers Face--And How You Can Solve Them
by Alex Allain
http://cprogramming.com/beginner_programming_mistakes.html
When you're just starting out with programming, it's easy to run into problems that make you wonder how anyone has ever managed to write a computer program. But the fact is, just about everyone else who's learned to code has had that experience and wondered the same thing, when they were starting out. I've helped teach several introductory programming classes, and there are some problems that trip up nearly every student--everything from getting started to dealing with program design.
I'll prepare you to get past these challenges--none of them are insurmountable.
Getting set up
Learning to program is hard enough, but it's easy to get tripped up before you even begin. First you need to chose a programming language (I recommend C++), then You need a compiler and a programming tutorial that covers the language you chose and that works with the compiler that you set up. This is all very complicated, and all before you even start to get to the fun parts.
If you're still struggling with getting the initial setup, then check out our page on setting up a compiler and development environment (Code::Blocks and MINGW) which walks you through setting up a compiler with a lot of screenshots, and gets you up to the point of having an actual running program.
Thinking Like a Programmer
Have you seen the State Farm commercials where the car wash company returns the cars to customers with the soap suds still on the car? The company washes the car, but it didn't rinse it. This is a perfect metaphor for computer programs. Computers, like that car wash company, are very very literal. They do exactly, and only, what you tell them to do; they do not understand implicit intentions. The level of detail required can be daunting at first because it requires thinking through every single step of the process, making sure that no steps are missing.
This can make programming seem to be a tough slog at first, but don't despair. Not everything must be specified--only what is not something the computer can alreay do. The header files and libraries that come with your compiler (for example, the iostream header file that allows you to interact with the user) provide a lot of pre-existing functionality. You can use websites like http://www.cppreference.com or our own function reference to find information on these pre-existing libraries of functionality. By using these, you can focus on precisely specifying only what is unique about your program. And even once you do that, you will begin to see patterns that can be turned into functions that wrap up a bunch of steps into a single function that you can call from everywhere. Suddenly complex problems will begin to look simple. It's the difference between:
引用
Walk forward ten feet
Move your hand to the wall
Move your hand to the right until you hit an obstacle
...
Press upward on indentation
and
引用
Walk to door
Find light switch
Turn on light
The magic thing about programming is that you can box up a complex behavior into a simple subroutine (often, into a function) that you can reuse. Sometimes it's hard to get the subroutine done up just right at first, but once you've got it, you no longer need to worry about it.
You can go here to read more about how to think about programming, written for beginners.
Compiler Error Messages
This may seem like a small thing, but because most beginners aren't familiar with the strictness of the format of the program (the syntax), beginners tend to run into lots of complaints generated by the compiler. Compiler errors are notoriously cryptic and verbose, and by no means were written with newbies in mind.
That said, there are a few basic principles you can use to navigate the thicket of messages. First, often times a single error causes the compiler to get so confused that it generates dozens of messages--always start with the first error message. Second, the line number is a lie. Well, maybe not a lie, but you can't trust it completely. The compiler complains when it first realizes there is a problem, not at the point where the problem actually occurred. However, the line number does indicate the last possible line where the error could have occurred--the real error may be earlier, but it will never be later.
Finally, have hope! You'll eventually get really really good at figuring out what the compiler actually means. There will be a few error messages that today seem completely cryptic, even once you know what the real problem was, that in a few months time you will know like an old (if confused) friend. I've actually written more about this in the past; if you want more detailed help, check out my article on deciphering compiler and linker errors.
Debugging
Debugging is a critical skill, but most people aren't born with a mastery of it. Debugging is hard for a few reasons; first, it's frustrating. You just wrote a bunch of code, and it doesn't work even though you're pretty sure it should. Damn! Second, it can be tedious; debugging often requires a lot of effort to narrow in on the problem, and until you have some practice, it can be hard to efficiently narrow it down. One type of problem, segmentation faults, are a particularly good example of this--many programmers try to narrow in on the problem by adding in print statements to show how far the program gets before crashing, even though the debugger can tell them exactly where the problem occurred. Which actually leads to the last problem--debuggers are yet another confused, difficult to set up tool, just like the compiler. If all you want is your program to work, the last thing you want to do is go set up ANOTHER tool just to find out why.
To learn more about debugging techniques, check out this article on debugging strategies.
Designing a Program
When you're just starting to program, design is a real challenge. Knowing how to think about programming is one piece, but the other piece is knowing how to put programs together in a way that makes it easy to modify them later. Ideas like "commenting your code", "encapulation and data hiding" and "inheritance" don't really mean anything when you haven't felt the pain of not having them. The problem is that program design is all about making things easier for your future self--sort of like eating your vegetables. Bad designs make your program inflexible to future changes, or impossible to understand after you've written. Frequently, bad design exposes too many details of how something is implemented, so that every part of the program has to know all the details of each other section of the program.
One great example is writing a checkers game. You need some way to represent the board--so you pick one. A fixed-sized global array: int checkers_board[8][8]. Your accesses to the board all go directly through the array: checkers_board[x][y] = ....; Is there anything wrong with this approach? You betcha. Notice that I wrote your accesses to the board all go directly through the array. The board is the conceptual entity--the thing you care about. The array happens to be, at this particular moment, how you implement the board. Again, two things: the thing you represent, and the way you represent it. By making all accesses to the board use the array directly, you entangle the two concepts. What happens when you decide to change the way you represent the board? You have an awful lot of code to change. But what's the solution?
If you create a function that performs the types of basic operations you perform on the checkers board (perhaps a get_piece_on_square() method and a set_piece_to_square() method), every access to the board can go throug this interface. If you change the implementation, the interface is the same. And that's what people mean when they talk about "encapsulation" and "data hiding". Many aspects of program design, such as inheritance, are there to allow you to hide the details of an implementation (the array) of a particular interface or concept (the board).
Now go eat your spinach!
A good follow-up to learn more about these issues is to read about programming design and style.
For a more advanced article on this topic, you can go here and read about object oriented class design.
Another way to make your program more easily modified in the future is to clearly comment it.
I'll be writing more on learning to program in the next few days, so stay tuned using our RSS feed, subscribing to cprogramming.com by email, or following @alexallain on twitter.
分享到:
相关推荐
"Common Sense C: Advice and Warnings for C and C++ Programmers" is a comprehensive guide aimed at helping programmers navigate the complexities and pitfalls of the C and C++ languages. Written by Paul...
Written for programmers with a background in another high-level language, this book uses hands-on instruction to teach today’s most compelling, leading-edge computing technologies and programming in ...
这本书《The Programmer's Idea Book》由Martyr2所著,是专为编程人士设计的创意项目集。它提供了200多个小型项目的想法和实现技巧,旨在帮助程序员拓展知识、练习编程,并将其应用到实际的软件开发中。 ### 数学与...
Written for programmers with a background in another high-level language, C++20 for Programmers applies the Deitel signature live-code approach to teaching Modern C++ and explores the C++20 language ...
It is JAVA Interface description
Standard Template Library Programmers Guide-1999 Standard Template Library Programmers Guide-1999 Standard Template Library Programmers Guide-1999
Standard Template Library Programmers Guide-1996 Standard Template Library Programmers Guide-1996 Standard Template Library Programmers Guide-1996
How do the experts solve difficult problems in software development? In this unique and insightful book, leading computer scientists offer case studies that reveal how they found unusual, carefully ...
If you’re a new programmer, these challenges will help you learn what you need to break into the field, and if you’re a seasoned pro, you can use these exercises to learn that hot new language for ...
and systems programmers can learn how to extend, enhance, and interface to the system. The authors provide a concise overview of FreeBSD's design and implementation. Then, while explaining key design...
Offering full coverage of Linux in one source, this book documents the most commonly needed topics for new and experienced Linux users and programmers - including over 100 utilities and their common ...
Throughout, Shaw stresses process so you can get started and build momentum, creativity to solve new problems, and quality so you’ll build code people can rely on. Manage complex projects with a ...
Use the new utility additions to the standard library to solve common problems developers encounter including string_view, any , optional and variant types In Detail C++ is one of the most widely ...
With HTML5 Programmer’s Reference at their elbows, programmers can quickly learn and master these new technologies, and maybe even surprise themselves with what they can do with them. The book dives...
卷三:• Volume III describes the MIPS32® Privileged Resource Architecture which defines and governs the behavior of the privileged resources included in a MIPS32® processor implementation • Volume...
This unique combination of its simple approach and the wide variety of techniques that are covered makes "Shaders for Game Programmers and Artists" a one-of-a-kind guide that can serve as both a ...
至于压缩包内的"The-Programmers-Idea-Book.pdf",这是该书的电子版,方便读者在电脑或移动设备上阅读。PDF格式保证了内容的清晰度和排版的完整性,使得读者可以随时随地沉浸在编程的世界中,不受实体书的限制。 总...
You’ll master general computer performance best practices and tools, which can help you identify the reasons behind low performance, and the most common performance pitfalls experienced when using ...