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
0:03
to come through.
0:07
Small little chunks.
0:08
Well, let's do one of those.
0:10
In the past, our queue stored songs,
but now we needed to store song requests.
0:12
So let's go ahead and make that change
with the help of our IDE.
0:17
Okay, let's press double shift
0:22
and navigate to Karaoke Machine.
0:25
Alright, let's look up here at our queue.
0:28
It's currently a queue of songs.
0:31
Instead, we want that to be
a queue of song requests.
0:33
Okay, let's refactor that.
0:38
There's actually a menu for that.
0:40
Let's click Refactor,
and we're going to choose Refactor This.
0:42
When you choose Refactor This,
it shows all the different refactorings
0:48
that it can do, and there's a lot.
0:52
What we want to do here is type migration.
0:55
We're moving it from type
song to the type song request.
0:58
So let's do that.
1:03
It says migrate the type
1:05
of field mSongQ to be song.
1:07
Let's change that.
1:11
Instead to be song request.
1:12
Basically, any time this is used,
we want to refactor that.
1:16
Let's choose Refactor.
1:21
Okay, it's found some problems,
1:23
mainly places
where a song is being pulled out.
1:25
Let's go ahead and choose Ignore for now.
1:29
Okay, that type refactor has happened now.
1:32
You'll notice though that now there's
a red squiggly line on different things.
1:35
It means that there are errors.
1:40
There is definitely an error
in our Karaoke Machine file,
1:43
and up here it's also been red squiggled.
1:46
In order to find errors, press F2.
1:51
It will move to the first error
in the file.
1:53
Let's move to the next error
by pressing F2 again.
1:57
This error is pretty clear.
2:00
What's happened is it pulled out
the song request, but called it song.
2:02
It's not actually a song.
2:07
We can use another refactoring here
Click this one
2:09
then Ctrl T to refactor this choose
2:13
Rename and Rename Song to Song Request
2:17
It also changed all
2:22
these entries down here,
which made it look a bit messy.
2:24
We want to add something new here.
2:27
Let's use a multi-cursor trick.
2:30
Hold Option Shift and click
where you want your cursors to be.
2:33
to be. Look, three cursors.
2:37
Now let's type get song.
2:39
Great.
2:42
Now we want to add in the singer's name.
2:43
Let's say, ready%s?
2:46
And then we need to add
songrequest.getsingername
2:50
and don't forget the comma.
2:55
Cool.
2:57
You know what?
2:59
That looks a little verbose with all of
the repeated song request references.
3:00
Let's highlight songRequest.GetSong
3:05
and Ctrl-T again to refactor this.
3:09
And choose Introduce Variable.
3:13
Let's replace all three reoccurrences.
3:16
Boom!
3:19
It introduced the variable called song
and set its value to songRequest.GetSong.
3:20
Perfect!
3:26
Now let's take care of the other errors
because we still have
3:28
a few red squigglies.
3:31
So press F2. Here's another one.
3:33
If you put your
3:37
cursor here, you'll see
an intention action shows up.
3:38
And it's red.
3:42
That means it knows about an error.
3:43
Press Option Enter.
3:46
It says, make prompt song for artists
return a song.
3:48
That's exactly what we want.
3:53
So let's have it do that.
3:55
And double click song from the pop-up.
3:56
Cool.
3:59
The method signature has changed.
4:00
Let's hit F2 again
to move to the next error.
4:03
Okay, now what's happening
here is it's pulling
4:07
a song request out,
but we just changed it.
4:09
Press Option Enter and change
the variable ArtistSong to be a song.
4:13
Perfect! Now there's a new bug.
4:19
We adding a song to a queue
that expects a song request
4:22
We need to build a song request item
4:26
Let start by adding string
4:30
singer name equals prompt for singer name
4:32
We'll make a new method for that.
4:39
It's definitely complaining about it.
4:41
Press option enter.
4:43
It says create method prompt
for singer name.
4:45
That's exactly what we want.
4:49
Look at that!
4:52
It even knew
it was a string from the context.
4:52
Awesome! Now let's define it.
4:56
We'll do a simple system.out.print.
4:59
Enter the singer's name.
5:04
And we just want to return
5:08
whatever comes back from the read line.
5:09
Oops, it says there's an error.
5:14
Oh yeah, it throws an IO exception.
5:16
Click on read
line, and press Option Enter,
5:19
then choose Add Exception to Method
Signature.
5:23
Boom! Perfect.
5:27
Another thing we can do
is go back to where we defined this by
5:29
clicking on the Prompt for Singer Name
method name here,
5:33
then pressing Command B.
5:37
Now we can use the new Singer name
to create a new song request.
5:40
So here on this line
where it says mSongCue.addArtistSong,
5:45
we'll replace ArtistSong with new song
request, parentheses,
5:51
singer name, comma, ArtistSong.
5:56
Perfect.
5:59
Now everything's happy.
6:00
The red squiggles are gone.
6:02
You know what?
6:04
Why don't we prevent someone
6:05
from putting the same song
by the same singer into the queue?
6:06
Let's extract
the new song request into a variable.
6:11
Let's highlight the new song request,
6:15
Ctrl T, and choose introduce variable.
6:17
It has some naming suggestions for us.
6:22
I think I'll go with song request.
6:24
Next, let's check
if the song queue already contains it.
6:27
We know the song
queue has a method called contains.
6:32
And we going to see
if it contains the song request
6:37
which returns a Boolean
6:41
Another cool feature in IntelliJ
is pressing a dot
6:44
after a Boolean generating
method like dot contains.
6:47
It will suggest things
that you could wrap it with.
6:52
And we want to wrap it in an if statement,
right?
6:55
Perfect, that's exactly what we wanted.
6:58
We're gonna use a printf statement here.
7:01
Let's use the SOUF shortcut.
7:04
We'll say %N, %N, whoops, %S,
7:08
already
requested, %S, exclamation point, %N.
7:12
And we'll put in the singer's
7:17
name and the artist's song.
7:19
And since we're in a choose case,
we don't want to continue through this,
7:24
so we can break out of the choose case
there.
7:29
Awesome.
7:33
Let's try running it with Control-R.
7:34
Now let's do choose,
7:37
Laura,
7:40
Katy Perry, and Roar.
7:42
And try to request it again.
7:46
It should prevent the duplicate.
7:50
Whoops, Laura already
requested song Roar by Katy Perry.
7:52
Now notice that using .contains
7:57
makes use of the equals method
we created earlier.
8:00
Even though the song request objects
are not the same object,
8:04
they have different memory addresses,
however, they are equal
8:08
by our definition of sameness
with the equals and hash code we defined.
8:13
Pretty cool, right?
8:18
Awesome! We're done!
8:20
Did you feel how much the IDE had
your back?
8:22
It's looking out for you
and it really wants you to succeed.
8:25
You probably noticed
8:29
how he's suggesting things that earlier
you might not have understood.
8:30
Now you're able to intelligently respond
to its prompts.
8:34
I hope you're feeling good about yourself
because that was really impressive.
8:37
All right, I'm really excited to show you
the next advanced tool, the debugger.
8:42
Get ready right after this next exercise.
8:47
Oh, and by the way, you're going to need
the changes we made for the exercise.
8:50
So I hope you're following along.
8:54
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