1 00:00:00,460 --> 00:00:05,120 While debugging using logs and trace points works well for simple scenarios, 2 00:00:05,120 --> 00:00:09,320 in more complex scenarios you'll probably find this technique to be cumbersome. 3 00:00:09,320 --> 00:00:11,480 This is where breakpoints save the day. 4 00:00:11,480 --> 00:00:13,780 So what exactly is a breakpoint? 5 00:00:13,780 --> 00:00:17,160 A breakpoint is a way to indicate that code execution should be 6 00:00:17,160 --> 00:00:19,480 paused at a specified place. 7 00:00:19,480 --> 00:00:23,160 When a break point is set, and the code is run with the debugger. 8 00:00:23,160 --> 00:00:26,500 Execution of the code is suspended right before the line of code 9 00:00:26,500 --> 00:00:29,120 associated with the breakpoint is executed. 10 00:00:29,120 --> 00:00:31,980 We can set many breakpoints throughout the code base. 11 00:00:31,980 --> 00:00:34,490 Later, we'll discover how we can easily manage 12 00:00:34,490 --> 00:00:36,920 all the break points set in our application. 13 00:00:36,920 --> 00:00:41,140 In this video, we'll set breakpoints that will suspend the execution of the code and 14 00:00:41,140 --> 00:00:44,100 allow us to inspect and modify the state of the application. 15 00:00:45,460 --> 00:00:49,480 Let's set a breakpoint right here before we go into this while loop. 16 00:00:49,480 --> 00:00:49,980 Now, let's run the debugger. 17 00:00:53,951 --> 00:00:58,540 As you can see, the program has paused before printing out the menu options. 18 00:00:58,540 --> 00:01:02,050 Now, we can step through the code line by line if we want to. 19 00:01:02,050 --> 00:01:07,040 Because we're in the debugger, we now have a bunch more buttons here on the toolbar. 20 00:01:07,040 --> 00:01:13,120 We have Show Next Statement, Step Into, Step Over, and Step Out. 21 00:01:14,170 --> 00:01:17,480 We also have Restart and Stop here. 22 00:01:17,480 --> 00:01:22,490 Step into, let's say step into any methods that are on the current line. 23 00:01:22,490 --> 00:01:25,250 In this case, we have the prompt method. 24 00:01:25,250 --> 00:01:30,990 So if we click Step Into, we'll go into the prompt method cooking step into 25 00:01:30,990 --> 00:01:35,500 when there isn't a method to step into or just step us over the next line of code. 26 00:01:36,690 --> 00:01:42,060 If we don't want to step into a method, we can click the Step Over button. 27 00:01:42,060 --> 00:01:45,930 Let's say we don't want to go into the display method, 28 00:01:45,930 --> 00:01:47,870 I'll click the step over button. 29 00:01:47,870 --> 00:01:50,190 The display method still runs like normal, 30 00:01:50,190 --> 00:01:52,290 but the debugger didn't pause inside of it. 31 00:01:53,620 --> 00:01:56,184 And I'll click step over when I get to CLI.Prompt. 32 00:01:57,360 --> 00:02:01,850 As you can see the CLI.prompt method was called and 33 00:02:01,850 --> 00:02:04,380 I'm prompted to select an option. 34 00:02:04,380 --> 00:02:06,520 I'll select one here. 35 00:02:06,520 --> 00:02:08,630 Now, we're at the next line. 36 00:02:08,630 --> 00:02:12,380 We can run the rest of this prompt method without pausing by clicking 37 00:02:12,380 --> 00:02:13,930 the step out button. 38 00:02:13,930 --> 00:02:16,950 This step says out of the current method and 39 00:02:16,950 --> 00:02:20,820 now we're back where menu.prompt was called. 40 00:02:20,820 --> 00:02:24,280 We might be looking around the code while the debugger is paused. 41 00:02:24,280 --> 00:02:28,770 So if we ever lose track of where the debugger is paused we can click the show 42 00:02:28,770 --> 00:02:33,340 next state button and we're brought back to where the yellow arrow is right here. 43 00:02:34,830 --> 00:02:39,200 Also, we can click the Continue button to run until another breakpoint. 44 00:02:46,979 --> 00:02:51,210 We can also have the program restart or stop it completely. 45 00:02:52,810 --> 00:02:56,460 Breakpoints don't have to be set on entire lines. 46 00:02:56,460 --> 00:03:00,120 A good example of this is when setting a breakpoint on an auto property 47 00:03:00,120 --> 00:03:03,010 where the getter and setter are on the same line. 48 00:03:03,010 --> 00:03:07,420 By default if we click to create a breakpoint on the Name property 49 00:03:07,420 --> 00:03:09,930 only the getter is highlighted here. 50 00:03:09,930 --> 00:03:13,200 This means this breakpoint will only be hit when getting the value 51 00:03:13,200 --> 00:03:14,580 of the name property. 52 00:03:14,580 --> 00:03:16,410 If we were to run this right now, 53 00:03:16,410 --> 00:03:21,050 this breakpoint would not be hit when setting the value of the name property. 54 00:03:21,050 --> 00:03:24,990 However, sometimes we want to pause before a property is set, 55 00:03:24,990 --> 00:03:30,220 we can do that by first highlighting the set statement, right clicking, 56 00:03:30,220 --> 00:03:34,050 going down to Breakpoint and clicking Insert Breakpoint. 57 00:03:34,050 --> 00:03:39,030 We now have two breakpoints on this line, one on the getter and one on the setter. 58 00:03:39,030 --> 00:03:42,630 This trick is also helpful when setting breakpoints on lines that may 59 00:03:42,630 --> 00:03:45,110 have multiple expressions in them such as 60 00:03:45,110 --> 00:03:47,680 ternary IF statements that are written on a single line.