1 00:00:00,690 --> 00:00:06,720 ‫We'll come back in this video and the next few videos I would like to show you how to use debugging 2 00:00:06,720 --> 00:00:08,220 ‫and Visual Studio. 3 00:00:08,220 --> 00:00:11,700 ‫And the idea behind debugging is to get rid of bugs. 4 00:00:11,700 --> 00:00:18,030 ‫And those bugs can sometimes be quite easy to spot because once you run the program and it starts to 5 00:00:18,030 --> 00:00:22,770 ‫crash, then you know, okay, you have a bug, but sometimes it's a logical bug, which means that 6 00:00:22,770 --> 00:00:31,680 ‫it will not really be seen by your ID, but it has to be spotted by you or by your users, which hopefully 7 00:00:31,680 --> 00:00:35,880 ‫will tell you that you need to improve it or need to get rid of this bug. 8 00:00:36,120 --> 00:00:43,200 ‫And in order to show you or demonstrate you the functionality of debugging in Visual Studio, I've created 9 00:00:43,200 --> 00:00:44,250 ‫a little program here. 10 00:00:44,250 --> 00:00:52,260 ‫I've prepared a little tool that we will use for this, and the tool is going to be a party friend's 11 00:00:52,800 --> 00:00:53,940 ‫invite tool. 12 00:00:53,940 --> 00:00:59,940 ‫So the idea is my daughter just turned three and we had a little party for her and she could invite 13 00:00:59,940 --> 00:01:05,340 ‫some friends and of course she would have loved to invite her a whole kindergarten, but we only allowed 14 00:01:05,340 --> 00:01:07,020 ‫her to invite three friends. 15 00:01:07,020 --> 00:01:10,590 ‫So now she had to decide which friends she wants to invite. 16 00:01:10,620 --> 00:01:15,060 ‫And of course it was an easy one because there are some friends she likes more than others, but in 17 00:01:15,060 --> 00:01:18,870 ‫the end I don't know who her favorite friends are. 18 00:01:18,870 --> 00:01:25,230 ‫So if I had to decide who she can invite, I would write a little program which says, okay, everyone 19 00:01:25,230 --> 00:01:28,710 ‫with a short name and up to a whole. 20 00:01:28,740 --> 00:01:33,720 ‫Yeah, up to a number of in this case, three can be invited to the party. 21 00:01:33,720 --> 00:01:35,670 ‫So she names all our friends. 22 00:01:35,670 --> 00:01:40,590 ‫And then I'm just going to say, all right, the ones with the shortest names, the three with the shortest 23 00:01:40,590 --> 00:01:41,790 ‫names will be invited. 24 00:01:41,910 --> 00:01:44,400 ‫And that's the tool here. 25 00:01:44,400 --> 00:01:46,170 ‫What I've written, that's exactly what it does. 26 00:01:46,170 --> 00:01:50,010 ‫So we have this list Frank, Joe, Michelle, Andy, Maria, Carlos and Angelina. 27 00:01:50,010 --> 00:01:57,270 ‫And we have this get party friends method which needs a list and the amount of people that it should 28 00:01:57,270 --> 00:01:58,770 ‫select from that list. 29 00:01:58,770 --> 00:02:05,610 ‫And then we have a for each loop which just writes down the names of those people or those children, 30 00:02:05,610 --> 00:02:08,220 ‫in my case, on the console. 31 00:02:08,220 --> 00:02:08,550 ‫All right. 32 00:02:08,550 --> 00:02:09,660 ‫Nothing too fancy. 33 00:02:09,660 --> 00:02:17,730 ‫And the algorithm which decides who is invited and who isn't is this method here and here we invite 34 00:02:17,730 --> 00:02:20,430 ‫a party friend or we find out who a party friend is. 35 00:02:20,430 --> 00:02:23,670 ‫We need, of course, a list of people that we can check. 36 00:02:23,670 --> 00:02:28,290 ‫Then we check the shortest name and we always compare. 37 00:02:28,290 --> 00:02:32,040 ‫So we go through all of the people in the list and we always compare. 38 00:02:32,040 --> 00:02:41,100 ‫Is the name going to be shorter than a specific length or into the one of the other people? 39 00:02:41,160 --> 00:02:43,280 ‫So in the end it's just going to give me the shortest names. 40 00:02:43,290 --> 00:02:46,440 ‫But here I internally have a bug here. 41 00:02:46,440 --> 00:02:46,830 ‫All right. 42 00:02:46,830 --> 00:02:51,390 ‫So I'm going to compare greater than shortest name that length. 43 00:02:51,390 --> 00:02:57,150 ‫So in this case, I'm just going to get all of the longest names, even though I wanted to get all of 44 00:02:57,150 --> 00:02:57,990 ‫the short names. 45 00:02:57,990 --> 00:03:03,630 ‫This is just an intent bug that I have implemented, because now you can see that this is something 46 00:03:03,630 --> 00:03:05,070 ‫that you might not notice. 47 00:03:05,070 --> 00:03:09,330 ‫Of course, it's a logical bug and if we run the code, it will still run. 48 00:03:09,330 --> 00:03:11,010 ‫There will be no bug or no error. 49 00:03:11,010 --> 00:03:14,310 ‫It will not crash, it will just give us the wrong names. 50 00:03:14,520 --> 00:03:19,800 ‫But now for us in this case, it's easy because we see, okay, Michelle, Angelina and Carlos are long 51 00:03:19,800 --> 00:03:21,690 ‫names and we know the list. 52 00:03:21,690 --> 00:03:24,930 ‫So we know that there was Joe and Anne, Maria and even Frank. 53 00:03:24,930 --> 00:03:29,040 ‫We have shorter names, but for some reason they weren't on the list even though we wanted to have the 54 00:03:29,040 --> 00:03:29,640 ‫shortest. 55 00:03:30,060 --> 00:03:30,410 ‫Yeah. 56 00:03:30,450 --> 00:03:33,300 ‫People or the people with the shortest names. 57 00:03:33,300 --> 00:03:36,840 ‫I don't know how short or how tall they are, but that doesn't matter here. 58 00:03:36,840 --> 00:03:43,650 ‫What's important is that here is an easy one because we have the list visible here as a developer. 59 00:03:43,650 --> 00:03:45,930 ‫But what if we get the list from somewhere else? 60 00:03:45,930 --> 00:03:52,080 ‫So let's say we get the list from an XML file, which is huge with thousands of names, or we get a 61 00:03:52,080 --> 00:03:58,110 ‫list from the internet and we can't see the list before it's actually processed by our software. 62 00:03:58,410 --> 00:04:05,250 ‫So then we wouldn't see that those names that were given to us are in fact not the shortest names, 63 00:04:06,450 --> 00:04:14,280 ‫but our software has this little bug, and now that's what the get part different method does. 64 00:04:14,280 --> 00:04:16,110 ‫It will return just the shortest name. 65 00:04:16,980 --> 00:04:23,010 ‫And then we have this get party friends plural, which just returns the list of all the people with 66 00:04:23,010 --> 00:04:29,100 ‫short names up to a certain amount that we can pay a pass here. 67 00:04:29,100 --> 00:04:32,490 ‫So we pass the number three, which means we will just get three friends. 68 00:04:32,490 --> 00:04:36,390 ‫If we pass the number four, we will get four friends and so forth. 69 00:04:36,390 --> 00:04:36,750 ‫All right. 70 00:04:36,750 --> 00:04:38,880 ‫So that's basically what our software does now. 71 00:04:38,880 --> 00:04:46,530 ‫We have commercial original cards with Frank, so we just have this empty party friends list. 72 00:04:46,530 --> 00:04:53,700 ‫And as long as this party friends list is has a lower count, then the number that was passed, we. 73 00:04:54,600 --> 00:04:57,660 ‫Simply check for the next party friend. 74 00:04:57,810 --> 00:05:01,590 ‫Then we go ahead and we add that to the party friends list. 75 00:05:01,590 --> 00:05:07,110 ‫And what we do is we remove from the list that was passed to us. 76 00:05:07,140 --> 00:05:13,530 ‫Now that's something that is very dangerous, but we do it because we're a beginner developer and we 77 00:05:13,530 --> 00:05:20,550 ‫just do this little trick where we think, okay, now we're just going to get rid of a variable or of 78 00:05:20,550 --> 00:05:23,190 ‫a element in the list. 79 00:05:23,190 --> 00:05:25,050 ‫And we do that programmatically. 80 00:05:25,050 --> 00:05:30,300 ‫And then step by step we have a short list and we only have the people left to compare, which are not 81 00:05:30,300 --> 00:05:31,410 ‫already in our list. 82 00:05:31,440 --> 00:05:31,800 ‫All right. 83 00:05:31,800 --> 00:05:33,720 ‫So that's what we do with this list. 84 00:05:33,720 --> 00:05:34,470 ‫Remove. 85 00:05:34,500 --> 00:05:38,100 ‫Even though that's not clean programming, we will see how to fix that later on as well. 86 00:05:38,250 --> 00:05:41,370 ‫But for now, we have the software and it works. 87 00:05:41,370 --> 00:05:42,360 ‫It works totally fine. 88 00:05:42,360 --> 00:05:44,960 ‫But how do we figure out how to fix this bug? 89 00:05:44,970 --> 00:05:51,030 ‫Because we now know that we have a bug and we can, of course, go through our code. 90 00:05:51,030 --> 00:05:54,000 ‫And in this case, it's easy because it's just 43 lines of code. 91 00:05:54,000 --> 00:05:59,370 ‫But imagine it's thousands of lines of code, many different files and many different people have worked 92 00:05:59,370 --> 00:06:01,470 ‫on it and you can't figure out what the bug is. 93 00:06:01,620 --> 00:06:07,980 ‫Now, what you can do is you can use the debugging capabilities of Visual Studio and in order to do 94 00:06:07,980 --> 00:06:11,300 ‫so, what you can do is you can just add a breakpoint. 95 00:06:11,310 --> 00:06:12,830 ‫Now let's start with a breakpoint. 96 00:06:12,840 --> 00:06:13,830 ‫We have this. 97 00:06:14,560 --> 00:06:16,560 ‫Little red dot here on the left hand side. 98 00:06:16,560 --> 00:06:21,330 ‫So in general, this gray bar that you have here, that's where you can add breakpoints. 99 00:06:21,330 --> 00:06:27,660 ‫You can also add a breakpoint by pressing F nine and I'm not going to do that because of nine is pausing 100 00:06:27,660 --> 00:06:29,910 ‫my video recording software. 101 00:06:29,910 --> 00:06:33,210 ‫So I need to do it by using my mouse. 102 00:06:33,210 --> 00:06:36,840 ‫So I go to debug and then I can press toggle breakpoint. 103 00:06:36,840 --> 00:06:39,480 ‫What that will do is it will create this breakpoint for me. 104 00:06:39,480 --> 00:06:41,550 ‫You could also of course press f nine. 105 00:06:41,550 --> 00:06:47,130 ‫So the shortcut is of nine and it's a lot easier with using shortcuts when you're debugging rather than 106 00:06:47,130 --> 00:06:49,320 ‫going to debug and clicking all the time. 107 00:06:49,320 --> 00:06:49,680 ‫All right. 108 00:06:49,680 --> 00:06:54,780 ‫So get used to using the buttons on your keyboard. 109 00:06:54,930 --> 00:06:55,290 ‫All right. 110 00:06:55,290 --> 00:06:56,880 ‫So then let's start debugging. 111 00:06:56,880 --> 00:07:03,810 ‫All right, so I go to debug and then I start debugging or I could just press F five to start debugging. 112 00:07:05,370 --> 00:07:12,060 ‫Now that we are, we have this little yellow arrow which indicates that this is where our software currently 113 00:07:12,060 --> 00:07:12,870 ‫is stuck. 114 00:07:12,870 --> 00:07:16,200 ‫And we made it stuck there because we created this breakpoint here. 115 00:07:16,200 --> 00:07:23,610 ‫So the software is breaking at that point and now we can move the software forward in our own pace so 116 00:07:23,610 --> 00:07:27,090 ‫we can go line by line and we can even dig deeper. 117 00:07:27,090 --> 00:07:28,250 ‫So let's do that. 118 00:07:28,260 --> 00:07:34,950 ‫Let's step over, press F10 or press this button here at the top in order to step over and get to the 119 00:07:34,950 --> 00:07:35,550 ‫next step. 120 00:07:35,550 --> 00:07:39,330 ‫By the way, all of these buttons, they are only available because I'm in debug mode, right? 121 00:07:39,330 --> 00:07:45,630 ‫So I started to run my software and also you can see step into step over and step out are all available 122 00:07:45,630 --> 00:07:46,350 ‫here as well. 123 00:07:46,350 --> 00:07:48,810 ‫So you have multiple different ways to. 124 00:07:49,020 --> 00:07:52,590 ‫Yeah, step over, step into and step out or press those buttons. 125 00:07:52,830 --> 00:07:53,190 ‫All right. 126 00:07:53,190 --> 00:07:56,700 ‫So what I'm doing now is I'm getting to the next line. 127 00:07:56,700 --> 00:07:57,900 ‫So I went to the next line. 128 00:07:57,900 --> 00:08:02,820 ‫And you can now see that friends is a value which has seven values. 129 00:08:02,820 --> 00:08:06,720 ‫So it's a list of seven strings Frank, Joe, Michelle and so forth. 130 00:08:06,720 --> 00:08:10,890 ‫At index zero, we have Frank at index one with Joe and so forth. 131 00:08:11,340 --> 00:08:16,830 ‫Now party friends itself is still an empty list, even though we are assigning a value to it. 132 00:08:16,830 --> 00:08:23,250 ‫But that's because when you're in line 11, line ten has executed, but line 11 hasn't executed yet. 133 00:08:23,250 --> 00:08:31,830 ‫So this means that we need to again step over so you can press f ten again to get to the next line. 134 00:08:31,830 --> 00:08:33,120 ‫And our line 13. 135 00:08:33,120 --> 00:08:37,500 ‫So line 12 was skipped because there is nothing there, there's no code there. 136 00:08:37,500 --> 00:08:39,420 ‫And it just went to this for each loop. 137 00:08:39,420 --> 00:08:41,310 ‫But now what if you want to dig deeper? 138 00:08:41,310 --> 00:08:46,170 ‫So what if you want to know what happened when this get party friends method was called? 139 00:08:46,260 --> 00:08:50,130 ‫What you can do is you can go back by dragging this arrow up. 140 00:08:50,130 --> 00:08:54,690 ‫So I'm dragging it back up and now I'm back at line 11 and now I can. 141 00:08:54,690 --> 00:09:01,650 ‫Instead of stepping over by pressing F ten, I can step into by pressing F 11 so I can press F 11 and 142 00:09:01,650 --> 00:09:09,000 ‫I step into the get party friends method, which means that now we are in line 18, which just means 143 00:09:09,000 --> 00:09:12,450 ‫that now we're inside of the Get Party Friends method call. 144 00:09:12,630 --> 00:09:14,370 ‫And here we can also go. 145 00:09:14,370 --> 00:09:15,180 ‫Step by step. 146 00:09:15,180 --> 00:09:17,430 ‫We can check out what party friends are. 147 00:09:17,460 --> 00:09:19,890 ‫We can check out the value of party friends. 148 00:09:19,890 --> 00:09:21,450 ‫You can see the count is zero. 149 00:09:21,450 --> 00:09:25,710 ‫So it's an empty list, which just means that there are zero entries in the list. 150 00:09:25,710 --> 00:09:31,380 ‫And then we have this wild loop where we compare stuff so we can go to the next line and we can see 151 00:09:31,590 --> 00:09:36,720 ‫party friends count was zero and we compare 0 to 4 and zero is less than four. 152 00:09:36,720 --> 00:09:40,470 ‫So the wild loop is or the wild is true. 153 00:09:40,500 --> 00:09:45,780 ‫So we can go to the next line and here we're calling Get Party Friend so we can go even deeper, so 154 00:09:45,780 --> 00:09:48,660 ‫we can step into get party friend. 155 00:09:48,660 --> 00:09:51,060 ‫And you can see now we are in this method here. 156 00:09:51,300 --> 00:10:01,260 ‫So stepping into means going into a method stepping out, which is the shift f 11 is jumping out of 157 00:10:01,260 --> 00:10:01,860 ‫a method. 158 00:10:01,980 --> 00:10:04,890 ‫But now let's have a look at this method call here. 159 00:10:04,890 --> 00:10:09,180 ‫We can see we have shortest name, which is Joe at this point. 160 00:10:09,210 --> 00:10:14,340 ‫Then we have this for loop and here we go, step by step in this for loop. 161 00:10:14,340 --> 00:10:17,460 ‫So first we set the variable, then we we compare. 162 00:10:17,790 --> 00:10:21,660 ‫Once the comparison is true, we go to the for loop or inside of the for loop. 163 00:10:21,660 --> 00:10:28,290 ‫Then we have this if statement we compare again and we can see as it seems, the length three is not 164 00:10:28,290 --> 00:10:33,540 ‫greater than the length or the shortest length of three. 165 00:10:33,570 --> 00:10:35,610 ‫So three is not great. 166 00:10:35,610 --> 00:10:41,520 ‫Three Now this if statement wasn't true, so it went to the next iteration of the for loop. 167 00:10:41,520 --> 00:10:44,640 ‫So now it's going to run the for loop again and again. 168 00:10:44,640 --> 00:10:51,180 ‫Now you can step out of and what this will do is it will step out of the execution of this method, 169 00:10:51,450 --> 00:10:53,640 ‫and then you can go from there. 170 00:10:53,640 --> 00:11:00,750 ‫But what you do by this is you don't execute the for loop as it usually would have. 171 00:11:01,110 --> 00:11:05,370 ‫So if I step out again, I'm getting an argument out of range exception. 172 00:11:05,370 --> 00:11:11,940 ‫So you have to be careful here not to step in and out all the time, but you have to worryingly use 173 00:11:11,940 --> 00:11:20,400 ‫these tools and you can of course just run from there or you can stop debugging, you can restart the 174 00:11:20,400 --> 00:11:24,840 ‫whole debugging process because now we have this little error and maybe you don't know how to fix it. 175 00:11:24,840 --> 00:11:31,770 ‫So we can just rerun the whole thing and we can now go, of course, again, step by step. 176 00:11:31,770 --> 00:11:33,090 ‫And we can. 177 00:11:34,000 --> 00:11:40,420 ‫Step over, step over, step over until we are at the point where it gives us the names. 178 00:11:40,480 --> 00:11:42,490 ‫So you can see now we are at Michelle. 179 00:11:42,790 --> 00:11:50,200 ‫Now it goes to the next name in the list now where Angelina goes to the next name in the list and it's 180 00:11:50,200 --> 00:11:50,770 ‫Carlos. 181 00:11:50,770 --> 00:11:58,090 ‫So you can see you can check every variable and the state of every variable at every single moment, 182 00:11:58,180 --> 00:12:01,810 ‫which is super important if you want to figure things out. 183 00:12:02,410 --> 00:12:02,770 ‫All right. 184 00:12:02,770 --> 00:12:08,020 ‫So that's the general idea of how to use breakpoints and how to start with the debugging process. 185 00:12:08,020 --> 00:12:15,430 ‫In the next video, we're going to look at how to fix this logical error by seeing the variables and 186 00:12:15,430 --> 00:12:17,470 ‫their states in a better way. 187 00:12:17,470 --> 00:12:24,100 ‫Because now I can, of course, hover over the variables and see what value they have at a specific 188 00:12:24,100 --> 00:12:24,640 ‫point. 189 00:12:24,640 --> 00:12:31,000 ‫But I want to compare multiple variables at the same time, or see the state of multiple variables at 190 00:12:31,000 --> 00:12:31,810 ‫the same time. 191 00:12:31,810 --> 00:12:33,820 ‫And that's what we're going to check out in the next video. 192 00:12:33,820 --> 00:12:34,870 ‫So see you there.