‫Well, welcome back.
‫In this video, we're going to fix the bug that we had and we're going to see how we can figure it out.
‫So in this example, it's a very basic one.
‫And we really know that we have this method that does the logic right.
‫So this is the one that figures out if a friend is a party friend or not.
‫And that's something that is generally something that will help you to debug because if you know what
‫a specific method will do, then you can create a breakpoint at that point.
‫All right.
‫So if you work in a program that you have never used before or seen before, then you, of course,
‫need to, first of all, look into it and understand what all of the methods do.
‫And that's why here, of course, it would be useful to have a summary of what's going on here.
‫All right.
‫So explain.
‫Okay.
‫What does list do?
‫What's the summary of this method?
‫So you can just use triple steps here and then you can add a little summary.
‫So this is the logic to figure out who is a party friend or who a party friend will be or something
‫like that.
‫It's not perfect English here, but I think you understand what the idea is.
‫So once you do that, you will know, okay.
‫All right, this is where the logic is.
‫And our program works by itself, right?
‫So it's working as intended more or less, except for, of course, that it shows us, the friends,
‫we have a long name and not the ones with short name.
‫So most likely the problem lies in the logic how we select something.
‫So here we could already see.
‫All right, probably.
‫This is the method where we have the problem so we can get rid of this breakpoint and this one as well.
‫And then we can have a look at our method here.
‫So let's just add a breakpoint and or get party friend method and then we can look into it.
‫So let's start with it.
‫And what's important are of course, the values, right?
‫So we want to know the values.
‫And in this case, we're interested in shortest name and also maybe in the length here because that's
‫the logic, right?
‫So what we would do is we then go to the next step.
‫And we can see, okay, the list consists of seven values, so that's fine.
‫So this seems to work.
‫We have all of our friends in there.
‫So until that point everything seems to be fine.
‫Then we go to the next step.
‫So we have our eye, which is our.
‫Counter and it's at zero.
‫All right, that's OC.
‫And the discount count is at seven, so we can go to the next step.
‫Now we have a comparison here.
‫If list I length greater shortest length.
‫And there we can see the shortest name is Frank.
‫So that's the one we started with.
‫Right?
‫So the list starts with Frank.
‫So at list I, which is zero, we will have Frank.
‫So that's fine.
‫Now let's go to the next step as it seems, this condition in the if statement wasn't true.
‫All right.
‫So then we can go to the next iteration or next step and it's comparing again.
‫Now we can see the list is still seven people.
‫And then we had a string length get returned with three and the other one was five.
‫So what's going on here?
‫We're looking at Joe and Joe should be the next one that's in the list because he has a shorter name
‫than Frank.
‫And that's something that we know because we wrote the program.
‫But for some reason, sure, this name is still Frank.
‫So then we could at this point realize our them.
‫All right.
‫So probably it's that we have a error here.
‫So we're comparing greater to even though it should be less than all right or smaller than.
‫And now if we get rid of the breakpoint and we run our program again, then we will see.
‫Okay, we get the shortest names, Joe and D, Frank and Maria.
‫All right, so that's great.
‫So now our program is back free.
‫Well, and of course, it's not because we still have plenty of problems here.
‫So one of them is that we have this list with Frank, Joe, Michelle and so forth.
‫All right.
‫So we created the list manually so we know what's in there.
‫But now let's say what we try to do is we try to invite ten people, but our daughter just gave us a
‫list of seven people.
‫So let's run this code.
‫And.
‫We have an exception.
‫Perfect.
‫So that's the great thing about exceptions.
‫You know that something is wrong with your code and you can fix it because sometimes, as we just saw,
‫if you don't see the bugs, they are super difficult to fix.
‫And here it's an argument out of range exception index was out of range must be non negative and less
‫than the size of the collection.
‫So you can view details about this.
‫You can see a little more about this exception and it will tell you all you need to know.
‫Right.
‫So a lot of information.
‫Most of the time it's too much.
‫But what this says is that we are trying to access an element in the list that is out of our range.
‫So what could it be?
‫Well, it could be the list that we're trying to access so we can go here.
‫And we can see that we have an exception.
‫We have a list, count zero.
‫So what's going on?
‫Why is the list zero?
‫Because we saw before the list was seven.
‫Right.
‫So why is the list suddenly zero?
‫Because we never really.
‫We looked at the list and deleted the list.
‫Right.
‫Well.
‫What we did is we removed an element of the list.
‫So we said List that remove.
‫And that is a beginner's mistake, so to say, because as I told you earlier, we're passing by reference.
‫We're not passing by value.
‫The difference is the following when we say.
‫That we want to have a list here.
‫And once this method is called, which is the case here, so get party friends is called here and we
‫pass a list called friends.
‫So we pass this list here, which doesn't mean that we pass the values we don't we actually pass the
‫reference to those values, which means that all of those values, they are somewhere in our memory.
‫So they they're in the memory of our computer.
‫Right.
‫And we're referencing to those values.
‫So we say, okay, I would like to go to this memory where you have all of your friends.
‫So I would like to go to Frank, I would like to go to Joe and so forth.
‫So once we work with this list, for example, we remove an element of that list.
‫We don't create a copy with the values, and then we go ahead and remove elements from the list.
‫But we're actually deleting in this memory.
‫So we're deleting Frank from the list, we deleting Joe from the list.
‫So you can quite easily see that if you use this for each loop to check out your friends.
‫Okay, so I'm going to reduce it again so that we can actually see something.
‫So I'm going through this for each loop.
‫And as you can see, only Angelina is displayed.
‫Why only Angelina?
‫Because Angelina is the seventh in the list, and I just removed six people from the list.
‫Let's do that again with four people.
‫Just so you see that Maria, Carlos and Angelina are still there.
‫So you see see, actually, it's Michelle.
‫All right.
‫So even better now we have a very weird situation where it's Michelle, Carlos and Angelina who are
‫still in the list.
‫So we are constantly removing someone from the list and that creates a bunch of problems because if
‫somebody else or if we use this friends list somewhere else, then it will be empty and it will be corrupted.
‫It will not have the initial values in there, and that's something that we should not do.
‫So what we can do is we can create a buffer here.
‫So I'm just going to create a new variable called buffer, and that will be a new list of strings.
‫And it will be my list, so I'm just going to pass my list that is passed to me at this point.
‫All right.
‫And I'm going to store it in buffer.
‫So it's going to be a new list.
‫And now what we can see is that we, of course, have to now remove from the buffer.
‫So we will remove from this copy of the list.
‫We won't remove from the list itself.
‫All right.
‫That's the important differentiation that we have to make.
‫We are removing from the copy now.
‫Now, of course, we also have to use the buffer here to get the party front.
‫All right.
‫Because otherwise we will never release anything from the list.
‫That will always be seven people.
‫So even in this buffer list, it will always be seven people and then it will not work correctly.
‫All right.
‫So that we are Frank, Joe, Michel, everything is still in the list.
‫Okay.
‫So the for each loop works out well.
‫And in comparison, what we could do is we could make another for loop and check it out with party friends
‫just to see if now we only have seven party or three party friends.
‫So you can see it's actually fourth party friends.
‫So all of them are in there and Joe and Frank and Maria are also still in there.
‫So we didn't delete the friends list, we deleted the elements from the buffer list.
‫And now, of course, we don't need this anymore.
‫So this was just for testing purposes and to debug.
‫But now we have a program that works well.
‫Now you could say, all right, we don't have any bugs anymore.
‫Well, but we still didn't fix that one bug that we had before, where if we enter ten here.
‫We would.
‫Got a bug or an error?
‫Actually, an argument out of range exception.
‫So what can we do here?
‫Because we're trying to access an element that does not exist.
‫So we're trying to access a list element even though our list is empty.
‫So list count is zero.
‫And that is something that we're going to fix in the next video because again, this video is more than
‫10 minutes and so don't want to make super long videos.
‫So I'm going to show you how to fix this in the next video because we have plenty of problems that result
‫from this.
‫All right.
‫So let's fix them in the next video.
‫See you there.