In this video, we’ll set breakpoints that suspend the execution of the code and allow us to inspect and modify the state of our application.
While debugging using logs and trace points works well for simple scenarios, 0:00 in more complex scenarios you'll probably find this technique to be cumbersome. 0:05 This is where breakpoints save the day. 0:09 So what exactly is a breakpoint? 0:11 A breakpoint is a way to indicate that code execution should be 0:13 paused at a specified place. 0:17 When a break point is set, and the code is run with the debugger. 0:19 Execution of the code is suspended right before the line of code 0:23 associated with the breakpoint is executed. 0:26 We can set many breakpoints throughout the code base. 0:29 Later, we'll discover how we can easily manage 0:31 all the break points set in our application. 0:34 In this video, we'll set breakpoints that will suspend the execution of the code and 0:36 allow us to inspect and modify the state of the application. 0:41 Let's set a breakpoint right here before we go into this while loop. 0:45 Now, let's run the debugger. 0:49 As you can see, the program has paused before printing out the menu options. 0:53 Now, we can step through the code line by line if we want to. 0:58 Because we're in the debugger, we now have a bunch more buttons here on the toolbar. 1:02 We have Show Next Statement, Step Into, Step Over, and Step Out. 1:07 We also have Restart and Stop here. 1:14 Step into, let's say step into any methods that are on the current line. 1:17 In this case, we have the prompt method. 1:22 So if we click Step Into, we'll go into the prompt method cooking step into 1:25 when there isn't a method to step into or just step us over the next line of code. 1:30 If we don't want to step into a method, we can click the Step Over button. 1:36 Let's say we don't want to go into the display method, 1:42 I'll click the step over button. 1:45 The display method still runs like normal, 1:47 but the debugger didn't pause inside of it. 1:50 And I'll click step over when I get to CLI.Prompt. 1:53 As you can see the CLI.prompt method was called and 1:57 I'm prompted to select an option. 2:01 I'll select one here. 2:04 Now, we're at the next line. 2:06 We can run the rest of this prompt method without pausing by clicking 2:08 the step out button. 2:12 This step says out of the current method and 2:13 now we're back where menu.prompt was called. 2:16 We might be looking around the code while the debugger is paused. 2:20 So if we ever lose track of where the debugger is paused we can click the show 2:24 next state button and we're brought back to where the yellow arrow is right here. 2:28 Also, we can click the Continue button to run until another breakpoint. 2:34 We can also have the program restart or stop it completely. 2:46 Breakpoints don't have to be set on entire lines. 2:52 A good example of this is when setting a breakpoint on an auto property 2:56 where the getter and setter are on the same line. 3:00 By default if we click to create a breakpoint on the Name property 3:03 only the getter is highlighted here. 3:07 This means this breakpoint will only be hit when getting the value 3:09 of the name property. 3:13 If we were to run this right now, 3:14 this breakpoint would not be hit when setting the value of the name property. 3:16 However, sometimes we want to pause before a property is set, 3:21 we can do that by first highlighting the set statement, right clicking, 3:24 going down to Breakpoint and clicking Insert Breakpoint. 3:30 We now have two breakpoints on this line, one on the getter and one on the setter. 3:34 This trick is also helpful when setting breakpoints on lines that may 3:39 have multiple expressions in them such as 3:42 ternary IF statements that are written on a single line. 3:45
You need to sign up for Treehouse in order to download course files.Sign up