debugging for beginners

Started by
7 comments, last by Trienco 10 years, 1 month ago
I've gotten the hang of c++ and DirectX so far, but when I create my own projects I have trouble debugging when I run into an error. I know how to set breakpoints and check for local variables, but when I miss an initialization or declaration I have a hard time finding the error. Are there any tutorials or books based just on debugging practices? Or is it a skill that you have to learn as you go?

If you see a post from me, you can safely assume its C# and XNA :)

Advertisement
Most compilers generate a warning when you use a variable without initialization. If your compiler does not you might want to increase the warning level. It's usually not a bad idea to enable treating all warnings as errors as well (that might not be feasible when you take over a large project which does not build without warnings but it is a good habit to get into for a new project).

A lot of potential bugs can be eliminated by not ignoring warnings.

I'm not sure what you mean by "missing a declaration". In the context of C++ I don't see how this could be anything but a compiler error (which are thankfully the easiest to find problems which don't even need a debugger).

I would say that if you can't find a problem with an initialization then you are probably writing some pretty messy code.

All variables should be initialized as soon as possible. They should almost always be at the top of the section or the global area. The only time when this isn't the case is when if you don't do something then you're never going to use the variable so why define it... Of course that is just an opinion.

You should also indent and comment, explaining the code in the code.

You don't have to do these things, but it does make debugging and having readable code that much easier.


Or is it a skill that you have to learn as you go?

The reason programming is hard, and making games is hard, is because it is easy to make a mistake that is very hard to find. Sometimes the program just crashes, and sometimes nothing works. But there is no secret, shortcut, list of steps, or book that can help with this, for the same reason there is no set of steps for solving all word problems in math. There are things you can try, but every problem is different.

But it is this process of making mistakes and figuring out what is wrong and fixing it that makes you a programmer. Eventually you'll be working on a big project or something new and there will be a problem that you can't "Google" or get help with. If you haven't paid your dues finding and fixing these little errors you won't be prepared for the tough ones.

We all make silly mistakes like this when we started learning to code.

Some suggestion:

1. Make sure you understand EVERY SINGLE LINE of code. When starting out, just cut-and-pasting code won't help you learn what you're doing.

2. Using the debugger is good, but also, try debugging print statements. Make a guess as to what is wrong, or what isn't wrong, and then prove it by logging something to the console. You'll be surprised what you find. Then leave those print statements there, just commented out, so you can turn them back on again if something else goes wrong.

3. Try commenting everything out, and then adding one thing at a time, making sure everything is working as expected.

4. When all else fails, try describing your problem to some else and/or have them look at your code. It is really easy to miss something when you've been staring at the same function for four hours.

And hang in there. Programming isn't easy.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

This is part of the reason I asked this question about pseudocode:

http://www.gamedev.net/topic/654225-how-does-your-pseudocode-look/

Also part of the reason I posted a link to this article about coding horrors:

http://www.gamedev.net/topic/652277-ugh-your-shorthand-is-too-short/

I agree that how the code is organized helps make debugging easier.

Also the reason I wrote this small tutorial which, as BitMaster suggests, initializes variables first:

http://forum.maratis3d.com/viewtopic.php?id=793

They call me the Tutorial Doctor.

I know this is a bit late if your code is already bugged, but you should consider getting into the habit of unit testing your code as you write it.

For one thing it helps identify when new code breaks an existing system in an unexpected way. But also it will encourage you to really consider the awkward cases you might otherwise skip over. Often a test is the clearest specification of correctness, and if your test fails then you know you're not done. Conversely, if a robust and thorough test passes then have a beer.

Visit http://www.mugsgames.com

Stroids, a retro style mini-game for Windows PC. http://barryskellern.itch.io/stroids

Mugs Games on Twitter: [twitter]MugsGames[/twitter] and Facebook: www.facebook.com/mugsgames

Me on Twitter [twitter]BarrySkellern[/twitter]

I had posted a thread a while back about an error I was having within a view frustum class. It turns out that I had two lines switched; I was trying to use an object before it was initialized. Even with using breakpoints and checking the local variables it still took forever to figure out where I messed up

If you see a post from me, you can safely assume its C# and XNA :)

I have no idea how to use any debugger tool in the IDE so I'm used only to read the code and output results trough the printing function, the one that outputs "Hello Worlds".

This print function is my best friend so far.

That is how I debugg my games.

I had posted a thread a while back about an error I was having within a view frustum class. It turns out that I had two lines switched; I was trying to use an object before it was initialized. Even with using breakpoints and checking the local variables it still took forever to figure out where I messed up

The question to ask is: how can that even happen?

Basic rules: minimize variable lifetime. Do NOT get into the annoying C-habit of dumping all variables at the beginning of a block. 9 out of 10 times, the is no excuse to split declaration and first assignment (hint: "assigning something later" is NOT "initializing").

Simple example:


{
   int var; //Bad
   int foo;
   //doStuff
   var = getData();   
   foo = calc(var);
}
 
//What happens if you add a line working with var?
{
   int var; //Bad
   int foo;
   //doStuff
   draw(var); //BUG
   var = getData();   
   foo = calc(var);
}
 
//Always declare at point of first use
{
   //doStuff
   const int var = getData();   
   const int foo = calc(var);
}
 
//Now this wouldn't even compile, saving you lots of time wasted on debugging
{
   //doStuff
   draw(var);
   const int var = getData();   
   const int foo = calc(var);
}
f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement