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