Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Local Development Environments!
You have completed Local Development Environments!
Preview
IntelliJ IDEA has many common refactoring techniques that are right at your finger tips.
Note: Don't forget to check the project files under the downloads tab for files relating to this course.
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
So we've done a little bit
of refactoring before,
0:00
you know, changing the code just a little
bit to allow for new code to come through.
0:02
Small, little chunks.
0:06
Well let's do one of those.
0:07
In the past, our queue stored songs, but
now we need it to store song requests.
0:08
So let's go ahead and
make that change with the help of our IDE.
0:13
Okay, so
let's navigate to karaoke machine.
0:17
All right let's look
down here at our queue.
0:23
So it's a queue of Songs.
0:28
We want that to be
a queue of SongRequests.
0:32
Okay, so let's refactor that, so
there's actually a menu for that.
0:34
So let's say refactor and
we are going to chose Refactor this.
0:38
Okay, so when you choose Refactor this,
0:47
it shows all the different refactorings
that it can do, and there are a lot.
0:49
What we wanna do here is we wanna
do a type migration, right?
0:52
We're moving it from the songs,
the type Song, to the type SongRequest.
0:55
So let's do that.
0:59
Okay, so it says migrate the type
of field mSongQueue to be,
1:00
and that was in SongRequest.
1:06
And basically, this is any time this
is used, we want to refactor that, so
1:10
let's choose refactor.
1:14
Okay, so it founds some problems, and it's
mainly places where a song's coming out,
1:17
so let's go ahead and let's choose ignore.
1:21
Okay so that has happened now.
1:24
And now you'll notice,
1:26
see how over here there is a red
squiggly line on different things?
1:27
It means that there's errors, and there's
definitely an error on our file, and
1:31
it's also up here it's
been red squigglied.
1:34
and in our browser up here
it's been red squiggly lined.
1:37
So let's go ahead and in order to find
errors, and over here on the right too,
1:39
there's some, right?
1:44
So in order to find errors, if we pres F2,
it will move to the next error.
1:45
Let's move to the next error, okay,
so this is a really clear one here.
1:50
What's happened here.
1:53
is it's pulled out the songRequest,
and it's called it song.
1:54
It's not actually a Song.
1:58
So, we can use another refactoring here.
1:59
So let's go ahead.
2:01
I'm gonna click this one, and
I'm gonna do that refactor this.
2:02
And that was Ctrl+T.
2:06
So refactor this, and I'm gonna
rename this song to be songRequest.
2:08
Look, it was in the list there.
2:14
So, it knows that.
2:16
So I'm gonna call it songRequest.
2:16
Okay.
2:18
Great.
2:19
So it also changed all these entries
down here, which made it look bad, and
2:21
we kinda wanna add something new here.
2:25
So let's go ahead and
use that multi-cursor that we saw, right?
2:26
So this is really cool.
2:30
This is gonna blow your mind.
2:31
So you hit alt+shift and you click where
you want your cursors to be, okay?
2:32
So look, I've got three cursors there.
2:36
All right, cool.
Now I'm gonna type getSong.
2:38
Great.
2:41
So that's correct.
2:42
And we want to add in here, our singer.
2:44
So we'll say, are you ready %s?
2:48
And we'll add the songRequest.getSingerName().
2:51
Cool, and you know what,
that looks a little bit dorky,
2:57
with all that songRequest.
3:01
Why don't we.
3:02
Let's do this.
3:04
Let's highlight this and
then choose Ctrl+T again to refactor this.
3:05
Let's choose extract variable.
3:12
Oh, sorry.
3:18
See the grouping there?
3:19
Extract variable.
3:19
Okay?
And let's replace all three recurrences.
3:22
Boom, so see there it did.
3:25
It pulled out song, and
it knew I called it song.
3:26
Pretty great.
So I'm
3:28
gonna request that getSong() song, perfect.
3:29
Awesome.
So we have that working.
3:32
All right, so
let's go take care of the other errors,
3:34
cuz obviously there's still some.
3:36
This will go away once we get it.
3:38
So let's press F2, here it is.
3:39
And if we put our cursor in here,
3:42
you'll see the intention
action shows up and it's red.
3:44
And that means that it has
an error that it knows about.
3:46
So if I press alt+enter, it's going to say
make 'promptSongForArtist' return a Song.
3:49
And that's exactly what we want,
so let's have it do that.
3:54
Cool.
So that's working now and see,
3:57
the signature has changed.
3:59
So let's go and hit the next error,
cuz there's still one.
4:00
Okay, so now.
4:03
What's happening here is, it's pulling
a SongRequest out, but we just changed it.
4:05
So, let's change that as well.
4:09
Let's go ahead and
4:12
press Alt+Enter here, and let's change
variable artistSong to be a Song.
4:12
Perfect, okay.
4:18
Now there's a new bug.
4:20
We're adding a Song to a queue that
expects to have a SongRequest.
4:21
So we need to build
a SongRequest Item, right?
4:27
So let's go ahead and before all of this
happens, let's go ahead and say String
4:29
singerName equals promptForSingerName.
4:36
And we'll just make a new function here.
4:41
Okay.
And it's definitely complaining about it.
4:44
Let's see what is suggested.
4:46
So, again, I can press Alt+Enter,
or I can click this over here.
4:48
Oh, it says create method
promptForSingerName.
4:52
That's totally what we want.
4:54
Hey, look at that, it knew that
it was a string from the context,
4:56
what I was asking for.
5:00
Awesome.
5:02
So, let's go down here and we will say
5:02
system print.
5:08
We're gonna print out.
5:12
Actually, we just want to have print.
5:13
Enter the singer's name.
5:16
>> Cool.
5:21
And we wanna just return whatever
comes back from the read line.
5:22
Look, we've been doing it here, readLine.
5:26
Cool.
5:29
Oop, there's an error, what's it saying?
5:30
It says oh yeah,
remember those throw IO exceptions.
5:32
So let's go ahead and we'll do Alt+Enter
and add exception to the method signature.
5:34
Boom.
Perfect, right?
5:39
Great.
5:41
Now we can add that to
the new SongRequest.
5:42
So one thing else that we could do,
we can go back to where I defined this.
5:44
So if I put my mouse here,
or my cursor here,
5:48
and I press command b,
we'll go to the definition of it.
5:51
Cool.
5:55
Awesome, and
it shows us the only usage is right here.
5:55
That's great.
5:59
So now in this mSongQueue
we can say new SongRequest or
6:00
new object, and that takes
a singer name and an artist song.
6:06
Perfect.
6:12
Cool, now everything's happy.
6:14
Look, the red squigglies are gone.
6:15
Hey, you know what?
6:18
Why don't we stop someone from putting the
same song by the same singer in the queue?
6:19
Let's extract that new song
request into a variable.
6:23
So let's go ahead in here.
6:26
I'm gonna highlight this and
I'm gonna do Ctrl+T.
6:28
Let's extract that into a variable.
6:30
Perfect.
6:34
And it's got some suggestions.
6:34
I think we'll got with songRequest.
6:35
This ones a little bit weird, but
you'll get used to it the more you use it.
6:38
One thing that you want to do so,
the song queue.
6:42
We know the song queue has
method called contains.
6:44
And we're going to see if it
contains the song request.
6:47
Now, that returns a boolean.
6:53
So in this editor,
6:56
if you press a dot afterwards, it suggests
things that you could wrap it with.
6:59
And we want to wrap it with the if.
7:03
Right?
We want to say if the song queue
7:05
contains this request let's do that.
7:07
Cool.
So it wrapped it in an if, and
7:09
it gave us the parens there.
7:11
So it's kind of a backwards
way of thinking of it.
7:13
But when you're kind of processing things,
7:15
that is actually the way you thought
about, right, like it contains it.
7:17
So, if it contains it.
So it's an interesting approach.
7:21
So we're gonna use
a printf statement here.
7:23
So we use souf And we're gonna print F.
7:25
We'll say %n%n, whoops.
7:29
%s already requested.
7:33
%s.
7:38
And we'll put in the singer name,
and the artist song.
7:42
And since we're in a choose case,
we don't want to continue through this, so
7:50
we can break out of the choose case there.
7:54
Awesome, so let's go ahead and do Ctrl+R.
7:57
Cool, there we go and
8:05
if I try to choose, oh,
here's the singer's name, that's Craig.
8:06
And if I choose that Journey song, Don't
Stop Believin', and I try to do it again,
8:11
which I've been known to do,
I'm gonna choose the Journey song here,
8:16
Don't Stop Believin'.
8:22
It says whoops, Craig already requested
the song, Don't Stop Believin'.
8:24
Also, I guess we need to add
a new %n in here too.
8:27
We forgot that.
8:29
Awesome.
8:31
So, now I just wanna make sure that
you notice that we're using contains.
8:34
And that is making use of
the equals method that we made.
8:38
And one thing to remember is that
SongRequest is not the same one that's in
8:41
the queue, right?
8:45
We made a brand new one.
8:45
It's actually equal by our
definition of sameness, and
8:47
this sameness is the way that we
define things in the equals method
8:50
in the hash code, on the song request and
the song.
8:53
Pretty cool, right?
8:57
Awesome.
So we are done-zo.
8:58
>> I know, I know.
9:03
You just earned like ten
more dunk tank balls.
9:04
Did you feel how much
the IDE had your back?
9:07
It's looking out for you, and
it really wants you to succeed.
9:09
Did you see how it was stating things that
you most likely didn't understand before
9:12
kicking off onto your job adventures?
9:15
But now, you're able to intelligently
respond to its prompts.
9:18
I hope you're feeling good about yourself,
because that was really impressive.
9:21
All right, so
9:25
I'm really excited to show you the next
advanced tool, which is the debugger.
9:26
Get ready.
9:30
Right after this exercise.
9:30
Oh, and by the way, you're going
to need those changes we made for
9:32
the exercise, so
I hope you were following along.
9:35
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