Using the Debugger10:47 with Craig Dennis
Learn how to explore and step through your code using the powerful debugger.
We've talked about bugs or errors in our code in the past, but 0:00 we've never really talked about where that term came from. 0:03 It's a bit of lore these days, but back in 1946, 0:08 Grace Hopper was having some problems with a program she was running on the Mark Two. 0:11 After trying to figure out the flaw in her code for a while, 0:15 she found a moth trapped in the relay. 0:17 Once the bug was removed, the program worked correctly. 0:21 [SOUND] The program was properly debugged. 0:23 In most of today's popular programming languages, 0:27 there are tools that are provided to assist the the removal of bugs. 0:30 They are called debuggers, and they give you a better 0:33 overall understanding of how the code is actually working. 0:37 Let's go learn some bug extermination tricks. 0:40 Okay, so the first tool I'd like to use to introduce you to your debugger 0:43 is something called breakpoints. 0:47 Now it's possible to tell the code to pause at a certain line, and 0:49 you can do this with what is known as a code breakpoint. 0:53 Adding breakpoints is simple, 0:56 you just click the side of the editor over here where you want it to stop. 0:58 This area is called the gutter, so I was thinking we could insight this 1:02 karaoke machine run method, we'll stop on this line where it prompts for 1:06 the action, before we prompt for the action. 1:11 So I just clicked over here in this gutter, and 1:13 I clicked and it added a little stop sign here which is a break point. 1:15 And so under the run menu there is an option to debug your 1:20 current configuration. 1:25 So I'm going to do that, I'm going to choose debug karaoke and that's 1:27 gonna pop up the debugger, and notice how the line is now highlighted in blue. 1:32 That's the way of letting us know that we are paused here, it's suspended. 1:37 So, you'll see there's a debugger tab down here on the bottom and 1:42 over here under frames, what it's doing is it's showing us how we got there. 1:47 So this is the stack of code. 1:51 You've probably seen these in the stack traces, right? 1:52 So, right now we're in run in karaoke machine, but we got there from here, 1:54 from main. 1:58 So if I click onto main, you'll see that it's flipped to the proper file and 1:59 it's showing me highlighted there in blue, of how we got in there. 2:03 So it's kinda a good way of oh, how did we get here? 2:06 And kinda trace your steps back up, walk up the stack. 2:08 So from here we can do a couple of things. 2:12 We can move line by line or we can dive deeper into the code. 2:14 Let's do a deeper dive first. 2:17 So, let's say that I wanted to look at this promptAction method 2:19 that's here, right? 2:22 So I can come down here and I can choose the step into method. 2:23 It's an arrow pointing, so step into, and that's F7, so 2:26 what that's going to do is that's going to go into the definition of prompt action 2:30 on the karaoke machine file, right? 2:35 So, we've now stepped in to this code. 2:37 Cool, so now you'll see that we're inside the prompt action method definition and 2:39 we're still suspended. 2:43 And you'll see over here in the frames, 2:45 you'll see that we're now in prompt action from run from main. 2:47 So there's our stack, we're all the way down into prompt action. 2:52 And you can also see in the view over here that we have access to these variables. 2:55 So, if we look at this here's the mSong book that's private to the karaoke machine 3:00 and it has a private variable that mSongbook does called mSongs. 3:05 And we can open those up and we can take a look at the different songs and 3:08 we can keep on drilling down. 3:11 It's pretty cool, right? 3:13 So, we're paused on this line here, and if we step into this one, 3:14 so it's a single line that's on multiple lines, but 3:20 it's one line of code that's just spaced out better. 3:23 So if we step into this we're going to go and do the getSongCount on the SongBook. 3:25 So let's do that. 3:30 So now over here we're in the getSongCount from promptAction from run from main. 3:31 So in getSongCount, 3:36 if we press the step into you might think that it's going to do the size but we're 3:37 going to be blocked cuz we're really only concerned with what our code is, right? 3:41 So it jumped back out because it returned that value. 3:46 If you wanted to step in the code, this red one here, 3:49 this Force Step Into, this will actually go into the Java code. 3:53 We're not gonna do that right now, but 3:57 I just want to let you know that that's available. 3:58 If you wanted to go do some more exploring of what the Java API's are actually doing, 4:00 cuz most of them are going to show you Java code and 4:03 you can kind of walk through and see what things are doing. 4:06 And this is a great way to explore the APIs and 4:09 demystify what's actually happening. 4:11 Okay, so let's just go ahead and step into this for loop, here. 4:14 Oh, so now it's gonna run the line where it does the print. 4:19 Okay, so I'm gonna go ahead and I'm gonna quick step into, one more time, and 4:22 we're gonna step into the for loop. 4:25 And so now remember what this loop is doing is its looping through 4:27 each of the menu options we have and its setting the variable option for each loop. 4:30 So, now we can look at this first one is add, and so 4:35 lets just go ahead and we'll step into it step into it. 4:38 And you'll see as you move it puts the value of whats going on over there. 4:43 It's pretty cool. 4:47 So, we'll step into that. 4:50 Okay cool, so now we're on the next one. 4:52 Here which should give us play, which is the next option on there. 4:56 So, okay, so we kind of understand what this is doing, right? 5:02 So, we don't need to watch the whole for loop go through, so 5:04 what we could do is we could put our cursor outside and this function here. 5:07 Run to cursor will make the code all run until we get to there. 5:13 Cool, right? 5:18 So now we're still paused inside of the prompt action, but 5:19 we jump through that for loop and all that code ran. 5:22 But we didn't need to step and watch each one of them. 5:25 So, we can also step out of methods and go back to the frame where we started, okay? 5:28 So, that's this arrow, of course, the one stepping up and that's called Step Out and 5:32 that's Shift F8 to get out. 5:36 So, we're just gonna go up the stack. 5:37 Okay, and now it looks like it paused so there's no frames that are available. 5:41 So, What's going on? 5:45 Oh, that's right. 5:47 Prompt action, right here, this line called read line. 5:48 It's waiting for us to answer the action. 5:51 So, let's go ahead, and let's say choose. 5:53 That's the thing that we wanna do from the menu, we wanna choose to sing a song. 5:56 Cool, okay. 6:01 Now bam. Now we're back, and 6:01 the debugger's stopped. 6:03 We're back in run, we're at this choice line here. 6:04 Cool, so let's just go ahead and let's step here and again, 6:07 so now we typed in the choice to be choose, so we're on the switch statement, 6:12 so let's go ahead and let's step into this choose statement. 6:16 Okay, so we just wrote this prompt for singer name. 6:20 I don't really want to see how it works, so 6:22 the next feature is here, there's this step over, and that's F8. 6:25 Okay, so if I press F8, it's gonna step over that, and 6:30 it looks like again like it paused. 6:34 Oh, that's right it's prompting, so I'm gonna flip over to the console, I'm gonna 6:36 enter the singer's name which is me, and now I'm gonna go back to the debugger. 6:40 Oh there, okay we're stopped. 6:45 I'm gonna press F8 again, stopped again, it's prompting for the artist. 6:46 All right, let's say Wilson Phillips, what do you say? 6:52 Okay, so I think I pressed enter without pressing nine. 6:55 All right, so let's come back. 7:02 Let's undo this break point, and let's do this break point. 7:05 Okay, so we're gonna get right to here. 7:09 Actually, let's get to the point where we ask for the song, okay? 7:14 So we can continue through and let it stop, okay? 7:17 So I'm gonna start that again. 7:19 So up here, on the debugger, there's karaoke play, 7:20 there's also a little debug icon here, so I can click this. 7:22 Okay, so what do you wanna do? 7:28 We wanna choose a song. 7:30 It's me, I wanna to listen to some Wilson Phillips, boom and it's stopped. 7:32 Awesome, right? 7:37 Okay, so let's step over the prompts song for artists and 7:38 again flip over to the counsel, let's hold on for one more day. 7:43 So, now we're at the song request. 7:48 Let's go ahead and step over that. 7:52 It's going to build a the new song request object. 7:54 Okay, awesome. 7:58 So, in our debugger we have this song request. 7:59 And I want to show you a little tool that's a lot like the ripple that we saw 8:03 that we can use to run our own code. 8:08 So if you choose run and then you choose evaluate expression. 8:10 Okay, so option F8. 8:17 You can actually run some codes. 8:19 So, remember what we were looking at here is we're looking at song requests. 8:21 Right, so if we say song, oh look at that, it auto completes here too. 8:24 Song request, type in singer name. 8:30 It will run and this is the result that came back. 8:35 So, that's very similar to the ripple that we were using, right? 8:38 So we can actually even change the state here in the program. 8:41 So, that's also a nice fun feature. 8:45 So I'm going to go ahead and I'm gonna redefine what song request is, okay? 8:47 So remember we're stopped on this line here so 8:54 I'm gonna say songRequest = new SongRequest. 9:00 Once actually sign Ben up to sing that, right? 9:04 He'd probably sing that one really good. 9:08 Okay, so I'm going to press enter, and bam, so it got set. 9:11 Okay, so now I come back here and I look at song request, 9:14 Ben is the singer, even though the singer name that I said before was. 9:18 So, it's kinda cool, so you kind of punch variables into different situations if 9:24 you're trying to catch a certain thing in there. 9:27 Okay, so we can press the button over here to resume program execution. 9:30 Okay, and that's gonna keep on going and we're stuck here at the console again. 9:35 So if we did choose, we'll sing another one. 9:38 This time let's sing The Cure. 9:43 Okay, bam. So it stopped again. 9:45 So it'll keep on going. 9:47 So you can also toggle these on and 9:49 off using the Cmd+F8 key, will toggle the breakpoint on and off. 9:50 Or Ctrl+F8 on Windows. 9:55 So now when I press resume, it will keep going and not stop. 9:59 Cool, welcome to your debugger. 10:06 Okay, so that's a nice basic introduction to some of the key concepts 10:08 of the powerful debugger tool. 10:12 This is one of those things that most of the popular IDE's have and 10:14 they all kind of work the same way. 10:17 It is possible to have the debugger kick in when specific exceptions are thrown. 10:20 As you can imagine, this is super handy. 10:24 It's also possible to launch it when certain conditions are met, 10:27 even when fields are set or accessed. 10:29 I've added some notes on where to learn more of this. 10:32 Check below in the teacher's notes. 10:34 It's a little out of the scope of this course, but 10:36 I'd love to dive deeper into more advanced features in an upcoming workshop. 10:38 Let's take a look at how to work with a team using Intelliga, 10:42 right after this exercise 10:46
You need to sign up for Treehouse in order to download course files.Sign up