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
You can use conditional breakpoints to stop only when you actually want to. This is a time saving trick you should get familiar with.
Learn more
- Branching Statements from the Java SE tutorial
-
0:00
All right, so let's get working on this next issue here.
-
0:04
So, first actually look before I gotta close this, so
-
0:07
we can open up this, this one we should always close.
-
0:09
It feels so good to close issues.
-
0:11
Let's go back and look at the issues.
-
0:13
Now, it's only five and the next one that's open is the counting gets
-
0:17
bogus after every card has been viewed once.
-
0:21
Since we asked for the cards to be spread out evenly, but
-
0:25
strange things are afoot on the card page.
-
0:28
The steps to reproduce, look at all the cards once and
-
0:30
watch the numbers of viewed jump around.
-
0:33
Cards are seen multiple times before all of the cards are seen for the second time.
-
0:37
We want equality.
-
0:39
Gotcha, okay, let's go ahead.
-
0:41
Let's restart the server and make sure that we know what that's talk about.
-
0:44
I wanna go ahead and I wanna mute all these break points,
-
0:48
I wanna restart the debugger.
-
0:50
And one thing that we need to do is we need to make sure that we
-
0:55
approach this like we're a new user each time.
-
0:59
And something I can get a little hard to do.
-
1:02
One thing that you can do,
-
1:03
a nice trick is, the sessions that we're using are cookie-based, right?
-
1:08
So I'm gonna open up the Chrome toolbar here, I'm gonna go under Resources.
-
1:14
And I'm gonna go under Cookies, I'm gonna go under localhost.
-
1:16
I'm gonna delete this JSESSIONID.
-
1:18
So that's one way to make a brand new session appear.
-
1:21
So now this person has only seen that one time.
-
1:24
So we keep clicking next, two, three, four, five, six,
-
1:29
seven and I wanna try to pause it before we see him again.
-
1:32
So set, it was at eight.
-
1:35
Shoot, I missed it.
-
1:38
Shoot.
-
1:39
Whoa, look at that, yes, you see there going up and over and around and we're.
-
1:44
Okay, so I don't wanna have to count each time as you saw,
-
1:47
I’m not very good at that.
-
1:49
So what we want to do is take a look at our current breakpoints, so
-
1:53
let's take a look.
-
1:56
We don't really need that one anymore as we know what the card counts are.
-
2:00
We don't need to know what happens in the showFlashCard.
-
2:03
We're kind of interested in what happens when the showNextFlashCard happens, right?
-
2:07
So you wanna come here and let's undo the muting.
-
2:11
Now we don't want this to stop every time, right, cuz it's just gonna stop or
-
2:14
after I click play and then stop and click play and stop and click play.
-
2:18
It would be really nice if we can make it just stop when there is eight, right?
-
2:21
So that's like a condition.
-
2:23
So check this out.
-
2:24
So I'm gonna right-click and in the condition here,
-
2:26
we're gonna choose, we want when there's eight, right?
-
2:29
So when cardCounts, sorry, let's do that.
-
2:33
We're in the wrong line here.
-
2:35
We need to get have a hold of cardCounts first, so we're gonna come here.
-
2:38
The condition is cardCounts.size is greater than or equal to 8.
-
2:47
Every time it's past that, we'll stop.
-
2:50
That should work, right?
-
2:51
So we'll drop that there.
-
2:53
Let's go ahead, we'll kill this cookie.
-
2:55
We'll be like a brand new user.
-
2:57
And I should be able to.
-
3:02
Hm.
-
3:04
The condition didn't stick?
-
3:05
Yeah, the condition didn't stick.
-
3:07
That happens sometimes.
-
3:08
CardCounts greater than or equal to 8.
-
3:15
Sorry, that didn't go away.
-
3:16
That one, okay.
-
3:18
That's what was happening.
-
3:20
Okay, so keep going, keep going and
-
3:23
it should stop as soon as I think it's this one yeah, awesome.
-
3:27
So did you see that?
-
3:28
It went up to eight and then it stopped.
-
3:30
So that's super handy when you have something that needs to process a certain
-
3:33
amount of times and you want to stop only then.
-
3:35
Or if you only want it to happen when it's this one specific card of the specific
-
3:39
thing, that's a nice feature to have.
-
3:41
So let's go ahead and make sure that our object is marked as counts still.
-
3:47
If we take a look, we've got eight views here and look,
-
3:49
every single one of the ids has been viewed once, perfect.
-
3:53
Okay, I am ready to step in and find our bug cuz as we know,
-
3:57
we saw it's not working, right, so let's do that.
-
4:00
So let's step in, okay, so it's gonna find,
-
4:04
get the next unseen flash card and we've seen a mole.
-
4:09
So there should be nothing that comes back, right, cool.
-
4:12
So nothing came back.
-
4:13
So if the cards not equal in all.
-
4:14
Okay, so now we're in some new code here in some new territory.
-
4:19
Boy, there are some continues and breaks at play.
-
4:23
If you haven't seen these before, they are known to make some squirrelly logic and
-
4:28
sometimes infinite loop.
-
4:29
So let's run it until we hit one and then we'll discuss.
-
4:32
Okay, so we're gonna initialize a leastViewedId,
-
4:36
we're gonna loop through each of the entries.
-
4:38
So remember the key of that is the id and
-
4:41
the value is the how many times has been viewed.
-
4:44
Okay, so if leastViewedid is equal to null, which it is.
-
4:47
Okay, cuz it was instantiated outside of here.
-
4:49
We're gonna come in and we're gonna set the key which was 1.
-
4:53
We're gonna set the leastViewedId to that.
-
4:56
I guess just set it to something, right?
-
4:59
Okay, and here's the continue keyword.
-
5:01
Okay, so in loops what this does is it allows you to say go ahead and
-
5:06
skip all the remaining code down here.
-
5:08
And just come back up to the top, right?
-
5:11
Come back up to the top of the for loop, the rest of this code will not run.
-
5:15
There's some more in the teacher's notes, but let's go ahead, let's step over this.
-
5:19
We're gonna step into the continue, watch what happens.
-
5:22
Boom!
-
5:22
It pop right back up to the for loop.
-
5:25
And we're on the next iteration of it, we have the next entry.
-
5:30
Okay, so, this time leastViewedId is set, okay?
-
5:34
And then we're gonna come in, we're gonna say,
-
5:36
we're gonna pull out what the lowest score is based on what the leastViewedId is.
-
5:41
So, it's gonna get the leastViewedId.
-
5:44
And the idToViewCounts, it's gonna pull up the smallest card there, okay?
-
5:52
Okay, so the lowest score is one and it's gonna say if the current entry,
-
5:57
the entry id 2 is greater than or equal to the lowest score.
-
6:02
Now are equal cuz they're both one, it's gonna come in and it's gonna say break.
-
6:08
So what break does is it breaks out of the current loop, okay?
-
6:12
So when this line is hit, will exit out of the for loop.
-
6:15
No matter what's happening and
-
6:17
it's aptly named to use of break often breaks your code.
-
6:22
So some people claim that you shouldn't use continues and
-
6:24
breaks because it creates easy to miss logic mistakes.
-
6:27
So watch what happens here when I click step over.
-
6:29
So I'm gonna run this break statement, we're inside of the for loop.
-
6:33
We're not finished processing everything and there was a quick step over and
-
6:37
see how it popped out of the for loop.
-
6:39
And now we're gonna return the card that has the leastViewedId which was one.
-
6:46
Wait a second though, how could we be certain that this was the leastViewedId,
-
6:50
we didn't look at all of them.
-
6:52
That seems fishy.
-
6:54
I'd love to be able to go back in time and watch that flow one more time.
-
6:58
Well, guess what, there's a pretty good trick that is handy for times like this.
-
7:02
You can actually drop of this frame, so it'll be kind of like it never happened.
-
7:06
Now I say kind of because the code in frame could have
-
7:09
in fact change some global state.
-
7:10
But we just walked this code and nothing changed the global state,
-
7:14
nothing came outside of this.
-
7:16
So what do you say we go back in time,
-
7:17
now instead of walking into say a telephone booth.
-
7:22
We can just come here and click this, this drops the current frame, okay?
-
7:28
So, when I click it and boom, it's like nothing happened.
-
7:33
So we can again step into it and we'll be at the top again.
-
7:37
So, let's go ahead and let's put our cursor right here and
-
7:42
we'll say it run to cursor.
-
7:44
Cool, it's like we were never here before.
-
7:46
Most excellent.
-
7:48
Okay, so let's think this through once more.
-
7:51
If the views are greater than or equal to the lowest score,
-
7:55
then [LAUGH] we don't wanna break here, we wanna continue.
-
8:00
We don't wanna break out, we wanna say if it's greater than that,
-
8:04
we wanted to go again and find the next one, right?
-
8:07
So really what this is saying instead of using control flow logic.
-
8:12
Why don't we bump this into the if here.
-
8:18
We move this up and
-
8:21
change the logic, right?
-
8:26
So if the value is less than the lowest score, then it's the leastViewedId, cool.
-
8:33
So let's go ahead and let's restart.
-
8:41
Let's move this up, let's mute the breakpoints.
-
8:46
And if we come over here, I delete the session, cool, we'll click Next.
-
8:53
There they go, they're all even, awesome.
-
8:56
So it was just a little logic flow there.
-
9:00
We should do, we should make a new branch, right?
-
9:02
Cuz we just made some changes to the code and it's working.
-
9:04
So let's come back to our code here and we'll make a new branch.
-
9:11
And let's call this one strange-counting.
-
9:15
Yeah, well let's stick with the theme here, let's say bogus-counting.
-
9:19
That's what it said on the ticket, bogus-counting.
-
9:23
Okay, and let's go ahead and
-
9:25
let's commit and once let's take a look at our version control.
-
9:29
And we got some local changes, we want to commit this guy.
-
9:35
And let's say fixes number,
-
9:39
what is she was there, five.
-
9:43
Fixes #5 by making the cards displayed more standard.
-
9:54
And I'll go ahead and Commit and Push.
-
9:57
I'm gonna push bogus-counting up.
-
10:01
Okay, and so if I flip over to GitHub and I take a look here.
-
10:05
We'll see that I push the new branch.
-
10:06
I'm gonna go ahead and make a new pull request for my teammates.
-
10:10
I'm gonna make sure that I'm going into a mine and not the master one here.
-
10:15
So fixes number five by making the cards displayed more standard, that sounds good.
-
10:21
I think I wanna let you know here,
-
10:26
I was gonna say fixed the breakage by
-
10:31
removing the break, most excellent.
-
10:37
Okay, so when I go to create the pull request, you of course we'll take this,
-
10:41
look at it.
-
10:42
Think, all right and you click Merge pull request.
-
10:47
And you're gonna confirm the merge.
-
10:49
There we go.
-
10:50
So now, master is up to date and
-
10:54
one thing that you might wanna do is we can delete old branches.
-
10:58
So I'm gonna click this Delete branch, there we go.
-
11:01
I just realized that there are situations that you're dropped into where you don't
-
11:04
fully understand what's going on.
-
11:06
Either at the library or at the framework level.
-
11:08
Now we're currently in a spring boot application and
-
11:11
that might be familiar to you.
-
11:13
But let's assume just for a second that it wasn't.
-
11:15
Since spring uses a lot of dependency injection, you might be totally confused
-
11:19
as to [LAUGH] how some of these things are working.
-
11:21
For instance, if we come back to this file here,
-
11:23
this is probably super confusing, right?
-
11:25
How is this flashCardRepository here, how is this getting sent even?
-
11:30
So one thing that I wanted to show you,
-
11:33
because of the dependency injection that Spring uses, it might not be clear at all.
-
11:38
So one thing that you can do is you can come here and you can click this.
-
11:42
And in our Debug window here we must have these muted.
-
11:46
So we come in here and I'm gonna right-click this and
-
11:50
I'm gonna click Field access and Field modification will be done.
-
11:54
And what will happen then is every time somebody sets this or
-
11:59
accesses this it will come in.
-
12:01
So, let's go ahead and do that.
-
12:03
I'm gonna restart the debugger and
-
12:06
just assume that you didn't know where that was coming from.
-
12:08
Well, you didn't know how this worked.
-
12:09
So boom, right away this got kicked off and here the setFlashCardRepository.
-
12:15
And if you took a look at what was happening, you might be able to navigate
-
12:19
down here and find AutowiredAnnotationBeanPostProcessor.
-
12:26
So if you come back here and you look, you could see, look,
-
12:30
this is marked with Autowired.
-
12:31
And maybe you could start learning how that works.
-
12:34
So, okay, interesting.
-
12:39
Anyway I was just trying to show off kind of simulate how you can find things
-
12:43
that you're not sure exactly what's happening.
-
12:46
It helps you kind of explore what the framework is doing for you.
-
12:50
Okay, so let's take a break and why don't you imagine what it'd be like to demo our
-
12:53
changes that we just made to our client.
-
12:54
And then let's pick up in the next video with their imaginary feedback.
-
12:58
Sound good?
You need to sign up for Treehouse in order to download course files.
Sign up