• (2 Pages)
  • +
  • 1
  • 2

Debugging Discussion about Debugging with Breakpoints

#1 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 525
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Posted 19 August 2010 - 01:10 AM

*
POPULAR

Hello. I honestly don't know if someone has hit this topic yet but I'm getting tired of people not knowing the most important skill as a developer you must acquire... yes, debugging.

You have to know how to use the IDE's full power. You have to know the code. You have to know how to debug. Period.

Let's get started.

Open up your visual studio and create a new project. Seriously, follow the instructions all the way... open it up and create a new console project. Call it DebugDemo. You will copy the code I provide. You will place the breakpoints I specify. You WILL learn how to do this.

Still with me? Some may be put off by that paragraph. Some might be inclined to not follow instructions. Sorry, but this lab is 100% monkey see - monkey do or monkey quit programming (please).

Anyway, you have this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DebugDemo
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}



Get rid of all of your usings except system. Code now becomes:

using System;

namespace DebugDemo
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}



Good...

We're going to count to 10. Here's your code:

using System;

namespace DebugDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int start = 1;
            int stop = 10;

            for (int current = start; current <= stop; current++)
            {
                Console.WriteLine(current);
            }
            Console.ReadLine();
        }
    }
}



Run this now please. You should get this output:

Quote

1
2
3
4
5
6
7
8
9
10


With me still? I hope so.

You see that gray band in the left end of the code? I don't have a picture... you just have to find it yourself. Anyway, click it on a line there's some code. You should see a red dot appear. Click it again and the red dot disappears.

This is called a breakpoint. What is going to happen is when you run the code in debug mode (F5), it will stop execution on that line with the red dot. When it hits, the current line will have not executed yet.

We're going to walk this code all the way until it makes perfect sense why this skill is necessary to healthy development as a developer!

First, put a breakpoint on the line:

int start = 1;



Press F5 so that you run it debug mode. You will see this line light up immediately. If you haven't changed your theme, it's bright yellow - can't miss it.

Ignore locals for a minute if you have that window. Look at the current line. See, this line has not executed yet. Mouse over the word "start". You will see that start equals 0 because it has not been assigned.

The first trick we'll talk about is called STEP OVER. This is done with the keyboard shortcut F10. When you step over, you go to the next line down from what you were looking at and stop. So, press F10.

Mouse over start again. You will see that start is now equal to 1. It has executed. We are on the new line now:

int stop = 10;



Mouse over the word stop. Notice that stop equals 0. It has not been executed yet so the assignment has not happened. Press F10. Mouse over it again. Stop is now equal to 10.

Notice that the for loop is the next stop. In fact, just a segment of the for loop is in view of the breakpoint:

int current = start;



Mouse over current. Notice that it is equal to zero. Why? Because it has not executed yet. Mouse over start and notice it's equal to 1. Why? Because that line has executed already. It was our earlier assignment.

Press F10.

Mouse over current. Notice it is now equal to 1. Why? Because it was assigned the value of start which was already assigned to 1.

Notice how different discussing code becomes by debugging? Notice that none of the code you read has to be a mystery? We'll dig into new tricks in a little bit, but I need you to stay with me on this for now. Trust me, I'm doing this out of love even if my tone is harsh and I feel like a drill instructor. It's just really important.

Anyway, we are now on a boolean condition. This is the second part of the for loop. Our highlight should now be:

current <= stop;



Mouse over current. It equals 1. Mouse over stop. It equals 10. However, mouse over <=. It equals True. We get to see the evaluation before it happens.

Press F10.

Whoa! we jumped INTO the for code! Our current debug line is:

{



Why do you think that is? Well, logic in the for loop executes first and THEN the iteration code executes. You probably already knew that but now you see it working first hand.

Press F10

Console.WriteLine(current);



Mouse over current. Notice it still equals 1. It has not been iterated yet.

Press F10

Now, check your console:

Quote

1


Press F10

}



No surprise there.

Press F10

current++



Well, you can F10 through that a few more times if you like OR you can put a new breakpoint on Console.ReadLine() and press F5.

That's the next trick. We can jump to the very next breakpoint OR continue execution normally by pressing F5 after hitting a breakpoint.

To recap, F5 enters debug mode or resumes execution if already in debug. It also will jump to the next debug statement. F10 steps over code. That is, it will go to the next line that you are currently viewing.

Take a look at console output while on breakpoint of Console.ReadLine()

Quote

1
2
3
4
5
6
7
8
9
10


Press F5 again. Now press any key in console.

OK. We're out of execution.

The next trick is step into. This is achieved via F11. Obviously, your code will have method calls and such and what step into does is go into those method calls.

Here's some new code:

using System;

namespace DebugDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            CountNumbers();
            Console.ReadLine();
        }

        private static void CountNumbers()
        {
            int start = 1;
            int stop = 10;

            for (int current = start; current <= stop; current++)
            {
                Console.WriteLine(current);
            }
        }
    }
}



Notice that this is the same code but we moved it to another method.

Put a breakpoint on the line:

CountNumbers();



Press F5.

See it stop on the breakpoint. Press F10. Notice that the code we have did not even get entered. You should move to your console and press a key. Take note of the numbers printed out. Everything executed identical.

Press F5.

This time press F11. Notice how it breaks into the method call. You may follow the steps in the beginning to step over all the code as before if you want.

Class dismissed. You should now be well on your way to becoming debugging masters.

For homework, I want you to view and do my design tutorials. I want you to breakpoint every open curly bracket you find and then run the application. I want you learn the differences between stepping into and stepping over very well. I want you to become familiar with the process of debugging.

For more homework, I want you to put a breakpoint on a program that you wrote. I want you to step through it all the way so pick one that ends and isn't more than 100 lines.

For more homework, I want you to write a program like the one above (as in small) and I want you to debug it as you go along. I suggest a temperature converter or something equally simple but somewhat involved.

Here are the tutorials:

Strategy

State

Factory

Last, I want you to actually thank my post if it is helpful to you (including the other tutorials). This also includes all tutorials you read on the site. Seriously, I saw one with around 50K views and only 3 thanks. What's up with that? You should be inspiring tutorial writers to write these if you are gaining from them.

Take care. Sorry if I was hard on you.

Is This A Good Question/Topic? 35
  • +

Replies To: Debugging

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 463
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Posted 20 August 2010 - 05:40 AM

the most useful skill you must have if your going to be programmer. perhaps it`s nice to tell the people that they can change the debug shortcuts from tools -> options. set the keyboard mapping to what`s your primal language and you will debug with his shortcut keys. for example if you set it to Microsoft Visual Basic 6 step in will be with F8 not F11 when your debugging in C# :)
Was This Post Helpful? 2
  • +
  • -

#3 Smurphy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 367
  • Joined: 07-July 08

Posted 20 August 2010 - 06:13 AM

While I already knew how to do this, and use it, I will give you a big thanks for putting it up because this simple action could help many people.
Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Posted 21 August 2010 - 07:34 PM

This is a nice tutorial. So many people cannot fix their own code because they don't know how to use the debugger. Some might think the debugger is complicated. Well it's not. It's actually pretty basic.

I think I might have to add this tutorial as a link in my sig because it is a necessary skill that few people care to learn.
Was This Post Helpful? 0
  • +
  • -

#5 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 22
  • View blog
  • Posts: 553
  • Joined: 04-August 09

Posted 21 August 2010 - 08:53 PM

Thanks MentalFloss
in fact, when I saw your name on the tutorial before reading it I knew it will be amazing

and it was

and i agree with you that every successful programmer must debug his code thoroughly to discover and maintain errors

that what I do all the time
Was This Post Helpful? 0
  • +
  • -

#6 AnandVVN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-September 08

Posted 25 August 2010 - 12:23 AM

Very well written article, debugging I would say would be one on top 5 list essentail skills for a programmer, along with ethics, codind standardds etc
Was This Post Helpful? 0
  • +
  • -

#7 Ilya_Sh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 14-December 10

Posted 18 December 2010 - 02:22 PM

Is it possible to add conditions for a given breakpoint?
I can't find how to do it in my "Visual C# 2010 Express".
Maybe, "Express" is the keyword for my problem ? :)

Let us slightly modify the above example. Assume that we substitute
Console.WriteLine(current);

with
Console.WriteLine(manyCalculations(current));

for some complicated function manyCalculations, and then noticed that for some certain i the result is suspicious. So, how to catch breakpoint for the case (i==criticalValue) without changing the code?

This post has been edited by Ilya_Sh: 18 December 2010 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Posted 19 December 2010 - 03:00 PM

If you right-click on the breakpoint, is there no option to set conditions? I don't have VS 2010 Express, so I'm not sure.
Was This Post Helpful? 0
  • +
  • -

#9 Ilya_Sh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 14-December 10

Posted 20 December 2010 - 06:32 AM

I found out that it is impossible to use conditional breakpoints in
Visual Studio 2010 C# Express:

http://msdn.microsof...y/7sye83ce.aspx
Was This Post Helpful? 0
  • +
  • -

#10 tRose  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 13-April 11

Posted 13 April 2011 - 08:53 AM

Hey thanks for taking the time and posting those links to my discussion from last night and also for posting the tutorial to begin with. It definitely helped me see the bigger picture on debugging. I found that chapter which was two weeks ago to be not as entertaining and kinda dry but I see how crucial it is now to understand the errors and the inner workings of the program code. I haven't had a chance to read the other links yet but I definitely will but for now I have web development class. Thanks again and I clicked that your post helped since it did and since you took the time to write it.
Was This Post Helpful? 0
  • +
  • -

#11 foofo  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 19
  • Joined: 06-May 11

Posted 15 May 2011 - 03:25 PM

Interesting tutorial, furthermore you could expand. possibly out of the scope for this tutorial.
Providing a tutorial on tracers and watchers may be helpful to many...
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4311
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Posted 12 July 2011 - 02:18 PM

This tutorial has been added to our Learning C# Series!
Was This Post Helpful? 0
  • +
  • -

#13 litedrive  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Posted 14 August 2011 - 02:02 PM

Thank you. This did help me very much. I hate debugging, but I think all programmers do. It's something which, while tedious, is a necessity.

Edit:

For anyone using Linux to code C#, the Mono debugger is nearly identical to the Microsoft Visual Studio one.

This post has been edited by litedrive: 14 August 2011 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#14 QuantumCrayons  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 21-February 11

Posted 15 August 2011 - 04:01 PM

This tutorial helped a lot; I'd never really used breakpoints or stepping before, since no-one really cared to tell us about them in class.
Was This Post Helpful? 0
  • +
  • -

#15 cynix008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-October 09

Posted 29 February 2012 - 02:07 AM

Thanks :D
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2