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