Jump to content

Beginners, please learn to debug your programs

madknight3

Note: This isn't directed towards you if you're a complete beginner who has never started coding yet. Once you get started though, it wont be long before this information is relevant to you so come back after you can start making your own simple programs.

 

Introduction

 

We get a lot of topics here where someone posts their code that wont work and is (politely) asking us to fix it. Commonly they wont give us any extra information about the problem. Maybe you've done this yourself or were going to. Please read on.

 

It's great that there are people on these forums willing to help you out even if you don't do any debugging. However, it's to everyone's benefit, especially your own, if you start learning how to pinpoint and solve your issues yourself. This doesn't mean you'll be able to solve all your own problems, and we're still here for you, but at the very least you'll learn how to provide more information when asking for help.

 

A large part of programming is writing fresh new code to accomplish a task. Another large part of programming is figuring out why the code you, or someone else, wrote isn't working correctly. This process of identifying and removing errors is known as debugging. Every programmer should start learning to debug early because it's so helpful regardless of your skill level. So after you've went through a few tutorials and learned how to run some simple programs, it's a good time to jump into debugging.

 

There are 2 places where you can start debugging:

  1. Using print statements
  2. Using a debugging tool

In the future it will be useful to get into formal testing but don't worry about that at this stage.

 

Using Print Statements

 

Sometimes, no matter how advanced you are, all you need is a quick print statement to solve your issue. And it's a good place for beginners to start because they don't need to learn anything new. You may have already been doing this without knowing what the word "debugging" is and if so, great.

 

All you need to do is to put print statements in your code that provide you with useful information. There are far too many languages where each language may have multiple options for printing so obviously I can't list them all, but you'll usually learn one way of doing it in your first couple tutorials. Many tutorials start you out with the traditional "Hello World" example so in many cases you learn how to output text to the screen in lesson 1. Here are some examples.

// JavaSystem.Out.Println("...");// Python 2.7.xprint "..."// Python 3.xprint("...")// C#/VB.NETConsole.WriteLine("...");// Cprintf("...");// C++cout << "...";// PHPecho "...";// etc

The point of using print statements for debugging is to help figure out what is actually going on when you run your code. What is in that variable? What does that function return? Did I get the correct input from the user? Does the code enter the if block or the else block? What is happening in that loop? etc

 

It's a very simple method of debugging but it can still lead you towards your solution.

 

Using a debugging tool

 

Debugging tools offer many features and is a lot more useful than print statements in most cases. Essential, you get to pause your program at any line of code while it's running. While it's paused, you can have a look around at things like what value a variable currently holds. You can step through a program line by line, pausing at each line to make sure things are happening correctly or you can stop at only the specific lines you want. Sounds useful right? It'll take a little more work to learn how to use these tools, but once you do it'll make your coding life easier.

 

If you are using an IDE, like Visual Studio, Eclipse, IntelliJ, Code::Blocks, etc, then they will have a built in debugging tool. If you prefer text editors like Notepad++, Sublime Text 2, etc, they may have a debugging tool, perhaps through an extension, but if not you may be able to download a standalone option. For example GDB is a standalone command line debugger commonly used with C. Many browsers also have developer tools that have debuggers. For example you can debug Javascript inside Chrome and Firefox. 

 

The best way to learn to use these tools is to look for tutorials for the specific debugger that you use. It's also more likely to be in the language you're using which might make it easier for you to follow. Many debuggers are very similar though so if you learn to use one in say Visual Studio, you can probably then figure out how to use one in another IDE like IntelliJ.

 

Here are some debugger tutorials for reference but feel free to go out and find your own.

  • Visual Studio (1)
  • Code::Blocks (1)
  • Eclipse (1)
  • GDB (1)
  • Chrome (1)

If anyone wants to help me add to this list, contact me and I'll add it in. Being an IDE guy, I'm not familiar with many options in the text editor/standalone areas.

 

Conclusion

 

So now that you know you should be debugging, when you run into a problem with your code then give it a try. It's perfectly fine if you can't solve your problem yet, we are still here to help. It's the trying and information gathering that will help you learn and become a better programmer. Treat each error as an opportunity to learn something new.

 

So now that you're more informed, here are the things to post when requesting help:

  • Purpose - What is the code supposed to accomplish?
  • Code - The code itself (remember to use these forums [ code ] tags)
  • Error - What is the error message you are receiving when trying to build or run your program?
  • Line of Error - What line is the error message pointing at?
  • Any additional information you think might be useful that you found while debugging.

With debugging, even if you can't solve something yourself, you might find that your question becomes more narrow. You'll no longer be asking "Why doesn't my program work?" but instead asking more specific questions.

 

And even if you're just stuck on the "I can't even get my program to run so I can't debug it" problem, you can still post the first 4 points I mentioned and someone can better help you get things up and running.
 

 

If you have any questions/comments post below or send me a private message. Happy Coding!

Link to comment
Share on other sites

Link to post
Share on other sites

Great post! This should be very helpful as I see a lot of new programmers here in the forum. I would also like to add that if you are posting to the forum for help, please do not just upload your code and say I can't figure it out. Tell us the problem, what the goal is, etc. We want to help fix the problem, not do it for you.

Link to comment
Share on other sites

Link to post
Share on other sites

debugging is a pain in the ass but it must be done, thought the semi colon vs greek question mark issue still stands, ive fallen for it too many times.

cpu: intel i5 4670k @ 4.5ghz Ram: G skill ares 2x4gb 2166mhz cl10 Gpu: GTX 680 liquid cooled cpu cooler: Raijintek ereboss Mobo: gigabyte z87x ud5h psu: cm gx650 bronze Case: Zalman Z9 plus


Listen if you care.

Cpu: intel i7 4770k @ 4.2ghz Ram: G skill  ripjaws 2x4gb Gpu: nvidia gtx 970 cpu cooler: akasa venom voodoo Mobo: G1.Sniper Z6 Psu: XFX proseries 650w Case: Zalman H1

Link to comment
Share on other sites

Link to post
Share on other sites

Intellij is a great IDE. Not sure if it's great for beginners though, it is designed for quite large programs.

 

Also Sublime Text 2/3 and Notepad++ as text editors, although they are not strictly debuggers they will help identify errors more quickly.

 

PyScripter for python (will correct syntax errors).

 

 

Also, the Python 3 syntax for print works in Python 2. Printf will work in C++, it should be noted that the <stdio.h> import is required in both languages.

Feel free to PM for any water-cooling questions. Check out my profile for more ways to contact me.

 

Add me to your circles on Google+ here or you can follow me on twitter @deadfire19.

Link to comment
Share on other sites

Link to post
Share on other sites

Excellent post. However, using a debugging tool might be overkill for beginners.

Link to comment
Share on other sites

Link to post
Share on other sites

The only thing I would add is the work it out by hand method.

 

If your program isn't working find a simple test case where it fails.  Then go through each step by hand, and see whether you get the same "wrong" output.  If you do, you will learn more about your program, and if you get the right result you can revert to the print statements to figure out where you have gone wrong.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Excellent post. However, using a debugging tool might be overkill for beginners.

 

I don't think so. I've seen plenty of beginners struggling with very simple problems that would be easily resolved by stepping through the code.

 

The whole notion that debugging is not for beginners leads to the habit of "debugging" with print statements.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Very nice, this should definitely be pinned. I only have one thing to add, there's a third debugging option. One could debug by commenting out code.

 

I'm quite new to this forum and I've noticed that 99% of the posts in this section are "Please help! Code doesn't work". Which of course, is okay, but I'd like to see more releases, tips, tutorials, and work in progress.

Link to comment
Share on other sites

Link to post
Share on other sites

I don't think so. I've seen plenty of beginners struggling with very simple problems that would be easily resolved by stepping through the code.

 

The whole notion that debugging is not for beginners leads to the habit of "debugging" with print statements.

 

I couldn't agree more. The knowledge of how to go about debugging should really be engrained right from the very start if possible. This is a basic skill for anyone who desires to become a good programmer.

 

There's no excuse for writing a mass of code and then upon finding out that it 'doesn't work' or that there's some other kind of problem with it to then try to dump it on others (often without even a basic context) in the expectation that they will just fix it for them... In effect throwing ones hands up, walking away and saying: 'Well I wrote it... but well I dunno... sort it out'.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

I don't think so. I've seen plenty of beginners struggling with very simple problems that would be easily resolved by stepping through the code.

 

The whole notion that debugging is not for beginners leads to the habit of "debugging" with print statements.

 

 

I'm not sure what you mean by "stepping through the code" because that is basically debugging (Either that or the compiler/interpreter telling you where the error is).

 

Is there anything wrong with debugging with print statements? People don't need to use debugging tools for simple programs, which beginners are most likely going to write.

Link to comment
Share on other sites

Link to post
Share on other sites

I couldn't agree more. The knowledge of how to go about debugging should really be engrained right from the very start if possible. This is a basic skill for anyone who desires to become a good programmer.

 

There's no excuse for writing a mass of code and then upon finding out that it 'doesn't work' or that there's some other kind of problem with it to then try to dump it on others (often without even a basic context) in the expectation that they will just fix it for them... In effect throwing ones hands up, walking away and saying: 'Well I wrote it... but well I dunno... sort it out'.

 

My thoughts exactly. Along with that, actually planning before coding. I've seen programmers at all levels who just jump into a project without a clear idea of how they will do it.

 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not sure what you mean by "stepping through the code" because that is basically debugging (Either that or the compiler/interpreter telling you where the error is).

 

Is there anything wrong with debugging with print statements? People don't need to use debugging tools for simple programs, which beginners are most likely going to write.

 

Stepping through = debugging.

 

Print statements are ok for really simple programs, but it quickly becomes umaintinable. Writing print statements takes time, gets disorganized and confusing quickly and going to production requires that you comment out or strip all print statements. Much simpler to set watches in the debugger combined with unit tests to verify things are working properly.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Indeed, there's some situations where print lines are useful but realistically they shouldn't find their way into production code. What should happen in production code is that the 'print line' concept be deferred to a logging framework... often via way of an interface if the project warrants that level of generalization.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Stepping through = debugging.

 

Print statements are ok for really simple programs, but it quickly becomes umaintinable. Writing print statements takes time, gets disorganized and confusing quickly and going to production requires that you comment out or strip all print statements. Much simpler to set watches in the debugger combined with unit tests to verify things are working properly.

 

 

Of course, I'm not saying don't use a debugging tool. It depends on how big or small the project or the program is, which for beginners, is going to be quite small IMO.

Link to comment
Share on other sites

Link to post
Share on other sites

Of course, I'm not saying don't use a debugging tool. It depends on how big or small the project or the program is, which for beginners, is going to be quite small IMO.

 

Yeah, and my point is that the size of the program doesn't make debugging less useful and print statements easier; it's about building good coding habits that pay off in time, in addition to solving the problems at hand.

 

Link to comment
Share on other sites

Link to post
Share on other sites

  • 9 months later...

Aren't Try/Catch/Throw statements also useful? 

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×