Watches9:07 with Craig Dennis
Let's use watches to keep track of important interesting variables in each frame.
Okay quick, so I don't forget, watches. 0:00 So you can create expressions that constantly stay bookmarked, so 0:03 let's do that over here. 0:07 Okay, so I'm gonna click our cardCounts variable that we determined up here, 0:08 it was so important. 0:12 And I'm gonna right click it, I'm going to choose Add to Watches and 0:13 it adds a little glasses like on here with cardCounts. 0:17 There's a way that you can make a separate panel. 0:21 I like this version a little bit better. 0:23 They made it so that you can have more space but 0:25 I'm gonna click this and now you'll see that there are separate, 0:26 you got Variables and Watches as two separate things. 0:29 One of my favorite things about these watches is that they stick across frames. 0:32 So for instance, we're basically done with our current frame right? 0:36 We're gonna return this value and the frame is gonna be popped from the stack. 0:38 Are we ready? 0:42 Boom, and now we're back where we started. 0:45 Now notice our current execution point, 0:47 we haven't set the cardCounts variable yet and that's why this right here can't find 0:49 the local variable cardCounts because it doesn't exist yet, 0:54 so I'm gonna go ahead and step over and now that it exists, look it's stuck there. 0:56 So we can see know that there is the cardCounts value here and 1:01 it's the cardCounts values here as well but it's also showing over in our 1:04 Watches because we're remembering that that's what we wanna watch. 1:07 There could be tons of variables there but one thing I wanna point out is that 1:09 the reason why this is working is because the name is the same, okay? 1:13 So we're watching in expression called cardCounts. 1:16 Okay, so let's step in to our next method here which is getNextFlashCardBasedOnViews 1:20 and we're gonna pass in the object reference cardCounts. 1:24 Okay, so here we go. 1:27 So we'll step in and here, uh-oh, 1:28 it says cardCounts can't find the local variable cardCounts. 1:30 And that's because it's been changed. 1:35 It's called idToViewCounts, it's more specific, okay? 1:37 So, well, that actually helps a little bit, right? 1:41 So that's helping me understand what this is. 1:43 So its id must be the key and 1:44 the value must be how many times a card's been viewed. 1:47 That's my current assumption at least. 1:50 So what now though? 1:52 Our watch isn't picking up this value in the current frame. 1:53 Now I could, of course, add another watch super easily, right? 1:56 Like I could just right click that, choose Add to Watches, it's there, and 1:59 then if I switch between the frames here, when I'm looking at this, 2:03 this one can't find the variable and when I look at the other one, 2:06 it can't find the cardCounts, fixes the problem more or less, right? 2:09 That's probably enough for most situations, but there's a pretty nice work 2:13 around that we can use if you don't mind going on a slight detour with me. 2:17 Okay, so there's a trick, right? 2:21 It's possible that you can label or mark objects. 2:22 So the usage of labeling is for various reasons. 2:26 Maybe we don't like the way a variable's named or 2:28 we want to add specific metadata about that object specifically. 2:30 Or in our case, we want to see when it's pointing to the same object. 2:33 So when you mark an object, 2:38 you're marking the object, not the name, not the expression of it. 2:40 Okay, so we can do that. 2:44 So lets, you right-click and choose Mark Object and you choose a Label. 2:45 Now, we know what we're talking about, right? 2:50 We know it's card counts and we know that it's this idToViewCounts. 2:51 So let's call it counts because that's what we wanna see. 2:54 So now, see when I go back and 2:58 forth between the frames, the counts sticks, right? 3:00 So the counts is labelled there. 3:02 It's also labelled in our watch over here. 3:05 See that? 3:09 So the awesome trick is that you can actually watch 3:10 a label not the variable name. 3:13 You can make it be an expression, right? 3:15 So let's go ahead, let's remove our watches, you can do that, 3:17 you can press backspace or 3:19 down here, underneath this, you can press the minus key. 3:20 All right, so we're gonna add a new watch and 3:26 watches can basically be any expression. 3:28 So, I'm gonna say counts and 3:31 then intelligent was nice enough to expose them. 3:33 So, I can say DebugLabel, right? 3:36 And if I press Enter, now it's watching counts. 3:38 So anything that counts it, so I'm both, 3:41 there's one watch across multiple variables and it's using this count. 3:43 Cool, right? 3:48 The fact that you can add any sort of expression there is super powerful, 3:49 you can put whatever in there and 3:52 it will keep evaluating kind of like a spreadsheet cell. 3:54 So back to our originally scheduled program. 3:57 So we're just about ready to call this method, right, 4:00 this method is getNextUnseenFlashCard and we're gonna pass in our 4:03 idtoViewCounts.keyset which at the moment remember that was empty, right? 4:07 We're looking down here. 4:11 This counts object is empty. 4:12 So there are no keys, so it's going to return an empty set. 4:14 So what's gonna happen, terminology wise, when I step into this method call? 4:17 Can you recall the terms? 4:22 What's gonna happen? 4:23 That's right, it's going to push a new frame onto the stack for 4:25 this specific method call. 4:29 Okay, so yeah look at this. 4:32 The parameter name is seenIds and we've passed in the keys of that ID to count. 4:33 So I'm fairly certain that my assumption was correct, our accounts label here is 4:38 a map of IDs to the number of times that things were viewed, cool. 4:43 So let's walk this logic. 4:49 Okay, so there's no IDs at all yet as we haven't seen anything, 4:51 it just returns a list of all of them, 4:55 right, so yup, there's no seen so it's gonna turn all of them. 4:58 So unseen here should be every card, right? 5:03 Remember there's 8 cards. 5:05 Okay, and since there is no unseen, we're gonna go ahead and 5:07 come in here and it's gonna pull out a random card. 5:11 So I can just go ahead and like we saw before, 5:13 I can highlight this and we'll see that it's gonna pull out a number, right? 5:15 It's gonna pull out an index between zero and whatever the number is. 5:19 So if I do this, if I highlight all this, this should give us a random card. 5:24 Yeah, so there so I ran that expression and 5:31 pulled out a random card, pulled randomly the first time of all that you're gonna 5:33 need because it created a new random and it was somewhere in between zero and 5:37 the top of the array, so that's how you pull something random, cool. 5:40 So I'm gonna step over to that, and 5:43 the card that we got pulled out is Java EE Enterprise Edition. 5:46 Okay, so that card got pulled out. 5:51 So now our frame is gonna pop off the stack and 5:54 it's gonna return to the getNextFlashCardBasedOnViews. 5:56 Since we have one, I'm sorry getNextUnseenFlashCards, so 6:02 there was one that was unseen, so we're gonna make sure that we show that. 6:06 So that returns a card and that pops off and we're in the last one here, 6:11 and so we do have a card and we're gonna return redirect link. 6:17 And if you accidentally click step over like I did right there, 6:21 you'll be dropped into the magic of what's going on. 6:24 It will give you some great experience in how amazing 6:28 this open source stuff is that we're using because of all this code that's right 6:33 there no matter what we're thinking about but if you don't want to sit there and 6:36 spend the time walking through it, you just press this Continue button. 6:39 Now if we look back over, we'll see our Java EE. 6:42 So let's just finish the flow once through just to feel a sense of accomplishment. 6:46 Okay, so let's see what happens. 6:50 So it generated flash card ID which is mapped here so 6:52 let's drop this, we're gonna go ahead and call next. 6:57 And here we are we're stopped and now we'll see that our counts as a size of 1 7:03 and 5, I'm assuming must be the ID of the Java EE, 7:08 right, so it's calling getcardCounts, right, 7:13 cuz we have a breakpoint in our getcardCounts up here. 7:18 So it's calling that and it's gonna get it out of the session. 7:21 Cool, so Java EE has been seen once, right? 7:28 And so we're gonna return the card counts. 7:33 Step over that. 7:35 Okay, so what is this doing? 7:37 This is Java 8 new map compute function, okay? 7:39 So what that does is it takes a key and a remapping function. 7:44 So let's suppose that we wanted to break into this lambda right here to see 7:48 actually what is this doing so I'm going to click right here. 7:52 And you'll see that I get a choice between the lambda and 7:56 the line itself, so I'm gonna use the lambda and 7:59 I'm gonna click the run and now we're actually in here, okay? 8:03 So we're here, so the key, I got passed in is 4 which must be the new cad that 8:07 we're looking at, must have the idea 4 and it hasn't been seen yet before. 8:12 Okay, so what it's gonna do is, it's gonna say, if it is null, which it is, 8:18 it's gonna be 1. 8:22 Otherwise, it's gonna increment, cool. 8:23 So it will increment if it exists, and then if we continue out of this, 8:25 we'll see that the card gets pushed in the template so 8:30 the second was Java SE and it was seen one time. 8:33 Awesome, and let's click Next one more time and we'll see that our cardCount 8:39 has now has 4 which is that idea we saw and 5 and they both of you at one time. 8:44 Cool, all right. 8:49 So now that we've got the basic idea for the flow, let's take a quick break. 8:51 I'm gonna go jog around my yard real quick and get my brain working. 8:54 I find it's good to take breaks and let all that we just learned about the way 8:57 that our app is working kind of work its way into our brains. 9:01 Then when we're refreshed, let's go grab another one of those issues. 9:04
You need to sign up for Treehouse in order to download course files.Sign up