‫We'll come back in this video and the next few videos I would like to show you how to use debugging
‫and Visual Studio.
‫And the idea behind debugging is to get rid of bugs.
‫And those bugs can sometimes be quite easy to spot because once you run the program and it starts to
‫crash, then you know, okay, you have a bug, but sometimes it's a logical bug, which means that
‫it will not really be seen by your ID, but it has to be spotted by you or by your users, which hopefully
‫will tell you that you need to improve it or need to get rid of this bug.
‫And in order to show you or demonstrate you the functionality of debugging in Visual Studio, I've created
‫a little program here.
‫I've prepared a little tool that we will use for this, and the tool is going to be a party friend's
‫invite tool.
‫So the idea is my daughter just turned three and we had a little party for her and she could invite
‫some friends and of course she would have loved to invite her a whole kindergarten, but we only allowed
‫her to invite three friends.
‫So now she had to decide which friends she wants to invite.
‫And of course it was an easy one because there are some friends she likes more than others, but in
‫the end I don't know who her favorite friends are.
‫So if I had to decide who she can invite, I would write a little program which says, okay, everyone
‫with a short name and up to a whole.
‫Yeah, up to a number of in this case, three can be invited to the party.
‫So she names all our friends.
‫And then I'm just going to say, all right, the ones with the shortest names, the three with the shortest
‫names will be invited.
‫And that's the tool here.
‫What I've written, that's exactly what it does.
‫So we have this list Frank, Joe, Michelle, Andy, Maria, Carlos and Angelina.
‫And we have this get party friends method which needs a list and the amount of people that it should
‫select from that list.
‫And then we have a for each loop which just writes down the names of those people or those children,
‫in my case, on the console.
‫All right.
‫Nothing too fancy.
‫And the algorithm which decides who is invited and who isn't is this method here and here we invite
‫a party friend or we find out who a party friend is.
‫We need, of course, a list of people that we can check.
‫Then we check the shortest name and we always compare.
‫So we go through all of the people in the list and we always compare.
‫Is the name going to be shorter than a specific length or into the one of the other people?
‫So in the end it's just going to give me the shortest names.
‫But here I internally have a bug here.
‫All right.
‫So I'm going to compare greater than shortest name that length.
‫So in this case, I'm just going to get all of the longest names, even though I wanted to get all of
‫the short names.
‫This is just an intent bug that I have implemented, because now you can see that this is something
‫that you might not notice.
‫Of course, it's a logical bug and if we run the code, it will still run.
‫There will be no bug or no error.
‫It will not crash, it will just give us the wrong names.
‫But now for us in this case, it's easy because we see, okay, Michelle, Angelina and Carlos are long
‫names and we know the list.
‫So we know that there was Joe and Anne, Maria and even Frank.
‫We have shorter names, but for some reason they weren't on the list even though we wanted to have the
‫shortest.
‫Yeah.
‫People or the people with the shortest names.
‫I don't know how short or how tall they are, but that doesn't matter here.
‫What's important is that here is an easy one because we have the list visible here as a developer.
‫But what if we get the list from somewhere else?
‫So let's say we get the list from an XML file, which is huge with thousands of names, or we get a
‫list from the internet and we can't see the list before it's actually processed by our software.
‫So then we wouldn't see that those names that were given to us are in fact not the shortest names,
‫but our software has this little bug, and now that's what the get part different method does.
‫It will return just the shortest name.
‫And then we have this get party friends plural, which just returns the list of all the people with
‫short names up to a certain amount that we can pay a pass here.
‫So we pass the number three, which means we will just get three friends.
‫If we pass the number four, we will get four friends and so forth.
‫All right.
‫So that's basically what our software does now.
‫We have commercial original cards with Frank, so we just have this empty party friends list.
‫And as long as this party friends list is has a lower count, then the number that was passed, we.
‫Simply check for the next party friend.
‫Then we go ahead and we add that to the party friends list.
‫And what we do is we remove from the list that was passed to us.
‫Now that's something that is very dangerous, but we do it because we're a beginner developer and we
‫just do this little trick where we think, okay, now we're just going to get rid of a variable or of
‫a element in the list.
‫And we do that programmatically.
‫And then step by step we have a short list and we only have the people left to compare, which are not
‫already in our list.
‫All right.
‫So that's what we do with this list.
‫Remove.
‫Even though that's not clean programming, we will see how to fix that later on as well.
‫But for now, we have the software and it works.
‫It works totally fine.
‫But how do we figure out how to fix this bug?
‫Because we now know that we have a bug and we can, of course, go through our code.
‫And in this case, it's easy because it's just 43 lines of code.
‫But imagine it's thousands of lines of code, many different files and many different people have worked
‫on it and you can't figure out what the bug is.
‫Now, what you can do is you can use the debugging capabilities of Visual Studio and in order to do
‫so, what you can do is you can just add a breakpoint.
‫Now let's start with a breakpoint.
‫We have this.
‫Little red dot here on the left hand side.
‫So in general, this gray bar that you have here, that's where you can add breakpoints.
‫You can also add a breakpoint by pressing F nine and I'm not going to do that because of nine is pausing
‫my video recording software.
‫So I need to do it by using my mouse.
‫So I go to debug and then I can press toggle breakpoint.
‫What that will do is it will create this breakpoint for me.
‫You could also of course press f nine.
‫So the shortcut is of nine and it's a lot easier with using shortcuts when you're debugging rather than
‫going to debug and clicking all the time.
‫All right.
‫So get used to using the buttons on your keyboard.
‫All right.
‫So then let's start debugging.
‫All right, so I go to debug and then I start debugging or I could just press F five to start debugging.
‫Now that we are, we have this little yellow arrow which indicates that this is where our software currently
‫is stuck.
‫And we made it stuck there because we created this breakpoint here.
‫So the software is breaking at that point and now we can move the software forward in our own pace so
‫we can go line by line and we can even dig deeper.
‫So let's do that.
‫Let's step over, press F10 or press this button here at the top in order to step over and get to the
‫next step.
‫By the way, all of these buttons, they are only available because I'm in debug mode, right?
‫So I started to run my software and also you can see step into step over and step out are all available
‫here as well.
‫So you have multiple different ways to.
‫Yeah, step over, step into and step out or press those buttons.
‫All right.
‫So what I'm doing now is I'm getting to the next line.
‫So I went to the next line.
‫And you can now see that friends is a value which has seven values.
‫So it's a list of seven strings Frank, Joe, Michelle and so forth.
‫At index zero, we have Frank at index one with Joe and so forth.
‫Now party friends itself is still an empty list, even though we are assigning a value to it.
‫But that's because when you're in line 11, line ten has executed, but line 11 hasn't executed yet.
‫So this means that we need to again step over so you can press f ten again to get to the next line.
‫And our line 13.
‫So line 12 was skipped because there is nothing there, there's no code there.
‫And it just went to this for each loop.
‫But now what if you want to dig deeper?
‫So what if you want to know what happened when this get party friends method was called?
‫What you can do is you can go back by dragging this arrow up.
‫So I'm dragging it back up and now I'm back at line 11 and now I can.
‫Instead of stepping over by pressing F ten, I can step into by pressing F 11 so I can press F 11 and
‫I step into the get party friends method, which means that now we are in line 18, which just means
‫that now we're inside of the Get Party Friends method call.
‫And here we can also go.
‫Step by step.
‫We can check out what party friends are.
‫We can check out the value of party friends.
‫You can see the count is zero.
‫So it's an empty list, which just means that there are zero entries in the list.
‫And then we have this wild loop where we compare stuff so we can go to the next line and we can see
‫party friends count was zero and we compare 0 to 4 and zero is less than four.
‫So the wild loop is or the wild is true.
‫So we can go to the next line and here we're calling Get Party Friend so we can go even deeper, so
‫we can step into get party friend.
‫And you can see now we are in this method here.
‫So stepping into means going into a method stepping out, which is the shift f 11 is jumping out of
‫a method.
‫But now let's have a look at this method call here.
‫We can see we have shortest name, which is Joe at this point.
‫Then we have this for loop and here we go, step by step in this for loop.
‫So first we set the variable, then we we compare.
‫Once the comparison is true, we go to the for loop or inside of the for loop.
‫Then we have this if statement we compare again and we can see as it seems, the length three is not
‫greater than the length or the shortest length of three.
‫So three is not great.
‫Three Now this if statement wasn't true, so it went to the next iteration of the for loop.
‫So now it's going to run the for loop again and again.
‫Now you can step out of and what this will do is it will step out of the execution of this method,
‫and then you can go from there.
‫But what you do by this is you don't execute the for loop as it usually would have.
‫So if I step out again, I'm getting an argument out of range exception.
‫So you have to be careful here not to step in and out all the time, but you have to worryingly use
‫these tools and you can of course just run from there or you can stop debugging, you can restart the
‫whole debugging process because now we have this little error and maybe you don't know how to fix it.
‫So we can just rerun the whole thing and we can now go, of course, again, step by step.
‫And we can.
‫Step over, step over, step over until we are at the point where it gives us the names.
‫So you can see now we are at Michelle.
‫Now it goes to the next name in the list now where Angelina goes to the next name in the list and it's
‫Carlos.
‫So you can see you can check every variable and the state of every variable at every single moment,
‫which is super important if you want to figure things out.
‫All right.
‫So that's the general idea of how to use breakpoints and how to start with the debugging process.
‫In the next video, we're going to look at how to fix this logical error by seeing the variables and
‫their states in a better way.
‫Because now I can, of course, hover over the variables and see what value they have at a specific
‫point.
‫But I want to compare multiple variables at the same time, or see the state of multiple variables at
‫the same time.
‫And that's what we're going to check out in the next video.
‫So see you there.