1 00:00:00,370 --> 00:00:01,320 ‫Well, welcome back. 2 00:00:01,350 --> 00:00:07,190 ‫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. 3 00:00:07,200 --> 00:00:10,530 ‫So in this example, it's a very basic one. 4 00:00:10,530 --> 00:00:16,320 ‫And we really know that we have this method that does the logic right. 5 00:00:16,320 --> 00:00:20,520 ‫So this is the one that figures out if a friend is a party friend or not. 6 00:00:20,730 --> 00:00:26,700 ‫And that's something that is generally something that will help you to debug because if you know what 7 00:00:26,700 --> 00:00:31,850 ‫a specific method will do, then you can create a breakpoint at that point. 8 00:00:31,860 --> 00:00:32,190 ‫All right. 9 00:00:32,190 --> 00:00:36,510 ‫So if you work in a program that you have never used before or seen before, then you, of course, 10 00:00:36,510 --> 00:00:40,770 ‫need to, first of all, look into it and understand what all of the methods do. 11 00:00:40,770 --> 00:00:46,800 ‫And that's why here, of course, it would be useful to have a summary of what's going on here. 12 00:00:46,800 --> 00:00:47,160 ‫All right. 13 00:00:47,160 --> 00:00:48,000 ‫So explain. 14 00:00:48,000 --> 00:00:48,230 ‫Okay. 15 00:00:48,300 --> 00:00:49,620 ‫What does list do? 16 00:00:49,650 --> 00:00:51,270 ‫What's the summary of this method? 17 00:00:51,270 --> 00:00:56,640 ‫So you can just use triple steps here and then you can add a little summary. 18 00:00:56,640 --> 00:01:06,960 ‫So this is the logic to figure out who is a party friend or who a party friend will be or something 19 00:01:06,960 --> 00:01:07,260 ‫like that. 20 00:01:07,260 --> 00:01:10,230 ‫It's not perfect English here, but I think you understand what the idea is. 21 00:01:10,230 --> 00:01:12,420 ‫So once you do that, you will know, okay. 22 00:01:12,420 --> 00:01:13,920 ‫All right, this is where the logic is. 23 00:01:13,920 --> 00:01:16,590 ‫And our program works by itself, right? 24 00:01:16,590 --> 00:01:23,250 ‫So it's working as intended more or less, except for, of course, that it shows us, the friends, 25 00:01:23,430 --> 00:01:25,650 ‫we have a long name and not the ones with short name. 26 00:01:25,650 --> 00:01:29,880 ‫So most likely the problem lies in the logic how we select something. 27 00:01:29,880 --> 00:01:31,860 ‫So here we could already see. 28 00:01:31,860 --> 00:01:32,640 ‫All right, probably. 29 00:01:32,640 --> 00:01:38,190 ‫This is the method where we have the problem so we can get rid of this breakpoint and this one as well. 30 00:01:38,190 --> 00:01:41,820 ‫And then we can have a look at our method here. 31 00:01:41,820 --> 00:01:47,550 ‫So let's just add a breakpoint and or get party friend method and then we can look into it. 32 00:01:47,550 --> 00:01:49,080 ‫So let's start with it. 33 00:01:49,080 --> 00:01:53,400 ‫And what's important are of course, the values, right? 34 00:01:53,400 --> 00:01:55,350 ‫So we want to know the values. 35 00:01:55,350 --> 00:02:02,100 ‫And in this case, we're interested in shortest name and also maybe in the length here because that's 36 00:02:02,100 --> 00:02:02,910 ‫the logic, right? 37 00:02:02,910 --> 00:02:06,330 ‫So what we would do is we then go to the next step. 38 00:02:07,380 --> 00:02:12,090 ‫And we can see, okay, the list consists of seven values, so that's fine. 39 00:02:12,090 --> 00:02:13,170 ‫So this seems to work. 40 00:02:13,170 --> 00:02:14,940 ‫We have all of our friends in there. 41 00:02:14,940 --> 00:02:17,900 ‫So until that point everything seems to be fine. 42 00:02:17,910 --> 00:02:19,260 ‫Then we go to the next step. 43 00:02:19,260 --> 00:02:22,170 ‫So we have our eye, which is our. 44 00:02:23,360 --> 00:02:25,110 ‫Counter and it's at zero. 45 00:02:25,130 --> 00:02:26,420 ‫All right, that's OC. 46 00:02:26,510 --> 00:02:29,960 ‫And the discount count is at seven, so we can go to the next step. 47 00:02:30,380 --> 00:02:33,230 ‫Now we have a comparison here. 48 00:02:33,230 --> 00:02:37,820 ‫If list I length greater shortest length. 49 00:02:37,820 --> 00:02:40,640 ‫And there we can see the shortest name is Frank. 50 00:02:40,640 --> 00:02:42,170 ‫So that's the one we started with. 51 00:02:42,170 --> 00:02:42,410 ‫Right? 52 00:02:42,410 --> 00:02:44,300 ‫So the list starts with Frank. 53 00:02:44,300 --> 00:02:48,830 ‫So at list I, which is zero, we will have Frank. 54 00:02:48,830 --> 00:02:50,020 ‫So that's fine. 55 00:02:50,030 --> 00:02:55,820 ‫Now let's go to the next step as it seems, this condition in the if statement wasn't true. 56 00:02:56,060 --> 00:02:56,420 ‫All right. 57 00:02:56,420 --> 00:03:02,300 ‫So then we can go to the next iteration or next step and it's comparing again. 58 00:03:02,900 --> 00:03:05,840 ‫Now we can see the list is still seven people. 59 00:03:06,050 --> 00:03:12,440 ‫And then we had a string length get returned with three and the other one was five. 60 00:03:12,440 --> 00:03:14,510 ‫So what's going on here? 61 00:03:14,510 --> 00:03:21,350 ‫We're looking at Joe and Joe should be the next one that's in the list because he has a shorter name 62 00:03:21,350 --> 00:03:22,010 ‫than Frank. 63 00:03:22,010 --> 00:03:24,620 ‫And that's something that we know because we wrote the program. 64 00:03:24,620 --> 00:03:26,690 ‫But for some reason, sure, this name is still Frank. 65 00:03:26,690 --> 00:03:29,150 ‫So then we could at this point realize our them. 66 00:03:29,150 --> 00:03:29,450 ‫All right. 67 00:03:29,450 --> 00:03:33,950 ‫So probably it's that we have a error here. 68 00:03:33,950 --> 00:03:41,420 ‫So we're comparing greater to even though it should be less than all right or smaller than. 69 00:03:41,900 --> 00:03:48,500 ‫And now if we get rid of the breakpoint and we run our program again, then we will see. 70 00:03:48,510 --> 00:03:51,950 ‫Okay, we get the shortest names, Joe and D, Frank and Maria. 71 00:03:52,040 --> 00:03:53,210 ‫All right, so that's great. 72 00:03:53,210 --> 00:03:55,640 ‫So now our program is back free. 73 00:03:56,120 --> 00:03:59,930 ‫Well, and of course, it's not because we still have plenty of problems here. 74 00:03:59,930 --> 00:04:04,910 ‫So one of them is that we have this list with Frank, Joe, Michelle and so forth. 75 00:04:04,910 --> 00:04:05,210 ‫All right. 76 00:04:05,210 --> 00:04:08,690 ‫So we created the list manually so we know what's in there. 77 00:04:08,690 --> 00:04:14,960 ‫But now let's say what we try to do is we try to invite ten people, but our daughter just gave us a 78 00:04:14,960 --> 00:04:16,160 ‫list of seven people. 79 00:04:16,160 --> 00:04:17,750 ‫So let's run this code. 80 00:04:19,030 --> 00:04:19,810 ‫And. 81 00:04:21,470 --> 00:04:22,640 ‫We have an exception. 82 00:04:22,640 --> 00:04:23,300 ‫Perfect. 83 00:04:23,300 --> 00:04:26,000 ‫So that's the great thing about exceptions. 84 00:04:26,000 --> 00:04:30,560 ‫You know that something is wrong with your code and you can fix it because sometimes, as we just saw, 85 00:04:30,590 --> 00:04:33,890 ‫if you don't see the bugs, they are super difficult to fix. 86 00:04:33,890 --> 00:04:40,220 ‫And here it's an argument out of range exception index was out of range must be non negative and less 87 00:04:40,220 --> 00:04:41,780 ‫than the size of the collection. 88 00:04:41,780 --> 00:04:44,870 ‫So you can view details about this. 89 00:04:44,870 --> 00:04:50,480 ‫You can see a little more about this exception and it will tell you all you need to know. 90 00:04:50,480 --> 00:04:50,780 ‫Right. 91 00:04:50,780 --> 00:04:53,900 ‫So a lot of information. 92 00:04:53,900 --> 00:04:55,400 ‫Most of the time it's too much. 93 00:04:55,400 --> 00:05:04,700 ‫But what this says is that we are trying to access an element in the list that is out of our range. 94 00:05:06,080 --> 00:05:08,870 ‫So what could it be? 95 00:05:08,870 --> 00:05:13,220 ‫Well, it could be the list that we're trying to access so we can go here. 96 00:05:15,290 --> 00:05:17,990 ‫And we can see that we have an exception. 97 00:05:17,990 --> 00:05:19,790 ‫We have a list, count zero. 98 00:05:20,620 --> 00:05:21,660 ‫So what's going on? 99 00:05:21,670 --> 00:05:23,420 ‫Why is the list zero? 100 00:05:23,440 --> 00:05:25,810 ‫Because we saw before the list was seven. 101 00:05:25,810 --> 00:05:26,080 ‫Right. 102 00:05:26,080 --> 00:05:28,690 ‫So why is the list suddenly zero? 103 00:05:28,690 --> 00:05:31,330 ‫Because we never really. 104 00:05:32,710 --> 00:05:36,040 ‫We looked at the list and deleted the list. 105 00:05:36,040 --> 00:05:36,430 ‫Right. 106 00:05:36,460 --> 00:05:37,030 ‫Well. 107 00:05:37,880 --> 00:05:40,490 ‫What we did is we removed an element of the list. 108 00:05:40,730 --> 00:05:42,770 ‫So we said List that remove. 109 00:05:44,780 --> 00:05:53,720 ‫And that is a beginner's mistake, so to say, because as I told you earlier, we're passing by reference. 110 00:05:54,170 --> 00:05:57,740 ‫We're not passing by value. 111 00:05:58,100 --> 00:06:01,370 ‫The difference is the following when we say. 112 00:06:03,360 --> 00:06:04,980 ‫That we want to have a list here. 113 00:06:05,070 --> 00:06:10,710 ‫And once this method is called, which is the case here, so get party friends is called here and we 114 00:06:10,710 --> 00:06:11,970 ‫pass a list called friends. 115 00:06:11,970 --> 00:06:17,760 ‫So we pass this list here, which doesn't mean that we pass the values we don't we actually pass the 116 00:06:17,760 --> 00:06:23,670 ‫reference to those values, which means that all of those values, they are somewhere in our memory. 117 00:06:23,670 --> 00:06:25,740 ‫So they they're in the memory of our computer. 118 00:06:25,740 --> 00:06:26,250 ‫Right. 119 00:06:26,250 --> 00:06:31,530 ‫And we're referencing to those values. 120 00:06:31,770 --> 00:06:37,050 ‫So we say, okay, I would like to go to this memory where you have all of your friends. 121 00:06:37,050 --> 00:06:40,680 ‫So I would like to go to Frank, I would like to go to Joe and so forth. 122 00:06:40,920 --> 00:06:46,920 ‫So once we work with this list, for example, we remove an element of that list. 123 00:06:46,980 --> 00:06:52,080 ‫We don't create a copy with the values, and then we go ahead and remove elements from the list. 124 00:06:52,080 --> 00:06:55,320 ‫But we're actually deleting in this memory. 125 00:06:55,320 --> 00:06:59,580 ‫So we're deleting Frank from the list, we deleting Joe from the list. 126 00:06:59,580 --> 00:07:06,390 ‫So you can quite easily see that if you use this for each loop to check out your friends. 127 00:07:06,390 --> 00:07:11,640 ‫Okay, so I'm going to reduce it again so that we can actually see something. 128 00:07:12,030 --> 00:07:15,210 ‫So I'm going through this for each loop. 129 00:07:15,210 --> 00:07:17,730 ‫And as you can see, only Angelina is displayed. 130 00:07:17,730 --> 00:07:18,690 ‫Why only Angelina? 131 00:07:18,690 --> 00:07:23,230 ‫Because Angelina is the seventh in the list, and I just removed six people from the list. 132 00:07:23,250 --> 00:07:25,350 ‫Let's do that again with four people. 133 00:07:25,620 --> 00:07:28,800 ‫Just so you see that Maria, Carlos and Angelina are still there. 134 00:07:28,860 --> 00:07:31,770 ‫So you see see, actually, it's Michelle. 135 00:07:32,310 --> 00:07:32,760 ‫All right. 136 00:07:32,760 --> 00:07:38,220 ‫So even better now we have a very weird situation where it's Michelle, Carlos and Angelina who are 137 00:07:38,220 --> 00:07:39,210 ‫still in the list. 138 00:07:40,170 --> 00:07:46,050 ‫So we are constantly removing someone from the list and that creates a bunch of problems because if 139 00:07:46,050 --> 00:07:56,220 ‫somebody else or if we use this friends list somewhere else, then it will be empty and it will be corrupted. 140 00:07:56,220 --> 00:08:00,570 ‫It will not have the initial values in there, and that's something that we should not do. 141 00:08:01,410 --> 00:08:04,620 ‫So what we can do is we can create a buffer here. 142 00:08:04,620 --> 00:08:12,390 ‫So I'm just going to create a new variable called buffer, and that will be a new list of strings. 143 00:08:14,640 --> 00:08:22,980 ‫And it will be my list, so I'm just going to pass my list that is passed to me at this point. 144 00:08:23,370 --> 00:08:23,730 ‫All right. 145 00:08:23,730 --> 00:08:25,230 ‫And I'm going to store it in buffer. 146 00:08:25,230 --> 00:08:26,790 ‫So it's going to be a new list. 147 00:08:26,970 --> 00:08:33,870 ‫And now what we can see is that we, of course, have to now remove from the buffer. 148 00:08:33,870 --> 00:08:36,830 ‫So we will remove from this copy of the list. 149 00:08:36,840 --> 00:08:39,000 ‫We won't remove from the list itself. 150 00:08:39,030 --> 00:08:39,330 ‫All right. 151 00:08:39,330 --> 00:08:42,200 ‫That's the important differentiation that we have to make. 152 00:08:42,210 --> 00:08:44,130 ‫We are removing from the copy now. 153 00:08:46,330 --> 00:08:52,390 ‫Now, of course, we also have to use the buffer here to get the party front. 154 00:08:52,390 --> 00:08:52,690 ‫All right. 155 00:08:52,690 --> 00:08:56,770 ‫Because otherwise we will never release anything from the list. 156 00:08:56,770 --> 00:08:58,420 ‫That will always be seven people. 157 00:08:58,420 --> 00:09:04,330 ‫So even in this buffer list, it will always be seven people and then it will not work correctly. 158 00:09:04,870 --> 00:09:05,170 ‫All right. 159 00:09:05,170 --> 00:09:08,590 ‫So that we are Frank, Joe, Michel, everything is still in the list. 160 00:09:09,010 --> 00:09:09,300 ‫Okay. 161 00:09:09,520 --> 00:09:12,010 ‫So the for each loop works out well. 162 00:09:12,100 --> 00:09:19,390 ‫And in comparison, what we could do is we could make another for loop and check it out with party friends 163 00:09:19,390 --> 00:09:23,620 ‫just to see if now we only have seven party or three party friends. 164 00:09:23,620 --> 00:09:27,040 ‫So you can see it's actually fourth party friends. 165 00:09:27,040 --> 00:09:32,440 ‫So all of them are in there and Joe and Frank and Maria are also still in there. 166 00:09:32,440 --> 00:09:37,900 ‫So we didn't delete the friends list, we deleted the elements from the buffer list. 167 00:09:37,900 --> 00:09:40,390 ‫And now, of course, we don't need this anymore. 168 00:09:40,390 --> 00:09:44,050 ‫So this was just for testing purposes and to debug. 169 00:09:44,170 --> 00:09:47,920 ‫But now we have a program that works well. 170 00:09:47,950 --> 00:09:50,500 ‫Now you could say, all right, we don't have any bugs anymore. 171 00:09:50,530 --> 00:09:55,840 ‫Well, but we still didn't fix that one bug that we had before, where if we enter ten here. 172 00:09:56,700 --> 00:09:57,630 ‫We would. 173 00:09:58,550 --> 00:09:59,960 ‫Got a bug or an error? 174 00:09:59,960 --> 00:10:02,720 ‫Actually, an argument out of range exception. 175 00:10:02,720 --> 00:10:04,250 ‫So what can we do here? 176 00:10:04,370 --> 00:10:07,970 ‫Because we're trying to access an element that does not exist. 177 00:10:07,970 --> 00:10:12,770 ‫So we're trying to access a list element even though our list is empty. 178 00:10:12,770 --> 00:10:14,390 ‫So list count is zero. 179 00:10:15,590 --> 00:10:20,960 ‫And that is something that we're going to fix in the next video because again, this video is more than 180 00:10:20,960 --> 00:10:23,360 ‫10 minutes and so don't want to make super long videos. 181 00:10:23,360 --> 00:10:30,440 ‫So I'm going to show you how to fix this in the next video because we have plenty of problems that result 182 00:10:30,440 --> 00:10:31,250 ‫from this. 183 00:10:31,460 --> 00:10:32,150 ‫All right. 184 00:10:32,150 --> 00:10:34,220 ‫So let's fix them in the next video. 185 00:10:34,220 --> 00:10:34,820 ‫See you there.