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
Why not let your IDE write some code for you? It knows the best practices.
Additional Information
- Field Naming Conventions which is the coding style I have been teaching in, but it is alas, just a style.
- Ternary operator (search for it).
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
When we
delivered our minimum viable product,
0:05
our MVP, for the karaoke project,
there was another story
0:07
that didn't quite make the cut,
and it got left in the backlog.
0:11
It was this, as a KJ, I should know
which singer requested the song
0:14
so that I can call them up to the stage.
0:19
As it turns out, after some usage,
most KJs were dying for that feature.
0:21
What happens is they keep calling the song
and no one comes up,
0:27
either because they forgot
or because they weren't paying attention.
0:31
So let's see if we can add this feature
request to our app real quick.
0:34
A song
request is definitely a model, right?
0:39
So let's navigate over there.
0:42
In the model package,
right click and choose
0:44
New, then Java Class.
0:47
Let's call it Song Request. Cool.
0:50
So we know we want a couple of things.
0:54
We want to have a private string
for the singer's name
0:56
and a private song reference.
1:02
Since song is in the same package,
1:08
we don't need to import it manually.
1:10
Now let's explore
something called code generation.
1:13
We want a constructor, right?
1:18
That passes in both the singer's name
and the song.
1:20
Good news!
1:23
IntelliJ can Create one for us.
1:25
Go to Code, then Generate,
1:27
or press Command N.
1:30
Choose Constructor and select both fields
1:33
by holding down Command and clicking them.
1:36
Then click OK.
1:39
Oops, almost perfect.
1:42
The parameters generated
are following our naming style,
1:45
where we prefix member variables with M
1:48
That just a coding style
and each project might use a different one
1:51
The editor can adapt to our style
we just need to tell it
1:56
So let command Z the constructor
it created.
2:00
Then let's click IntelliJ in the menu
2:04
bar, settings,
2:06
editor, CodeStyle Java
2:10
CodeGeneration
Great, here's the naming stuff.
2:15
So, whenever you have a field,
we're going to prefix it with an M.
2:20
Okay, that's really all you have to do.
2:25
Let's click Apply, then OK.
2:28
Now let's try
2:32
generating the constructor again
with Command N Constructor
2:33
and select both fields.
2:38
Boom!
2:41
There it is.
2:42
Now for the boring part,
adding getters and setters.
2:44
Did you see that in the list?
2:48
We can generate them.
2:49
Press Command
N, then Getter and Setter this time.
2:51
Then select both fields.
2:56
You'll notice there are templates
available for customization,
2:58
but the IntelliJ default template is fine
for now.
3:02
Let's press Command N again and see what
3:06
other code generation options there are.
3:09
Oh, it's a string. That's a handy one.
3:12
Let's generate
a toString method for both fields.
3:15
We want to concat them both.
3:18
You'll see IntelliJ even remembers
to add the at override annotation for us.
3:21
Nice.
3:26
Now, if we wanted to make our own template
for this, we could.
3:27
Let's command Z what it added for us
and go back to the two
3:31
string generation screen.
3:34
Up here
3:38
there's these templates
and we talked about that a little bit.
3:38
There different options that you could do
You can also make a new one
3:42
and if you really feel like nerding out
that something you can do too
3:46
You can even set your custom
template to be the default
3:51
and it will automatically generate
the same looking two string format.
3:54
The cool thing is when you save this,
it will be saved into the project file.
3:58
So everybody else can use the same
two string format.
4:03
For now though,
I think we're fine with the IntelliJ
4:07
default generated string concatenation.
4:10
Let's add that back in.
4:13
One thing we didn't talk about before
4:16
is overwriting equals and hash code.
4:18
This is especially important for classes
you'll use inside collections,
4:21
like HashSet or HashMap.
4:26
Why didn't we cover this earlier?
4:29
Because honestly, it's much easier
to let the IDE generate it for you.
4:31
Ready?
4:37
Let's do it.
4:38
Command-N, then equals and hash code.
4:39
Let's choose the IntelliJ default template
from the drop-down.
4:43
Then, make sure that the Get Class
Comparison Expression bullet point is
4:47
chosen.
4:52
We'll leave the Use Getters
When Available box unchecked
4:53
as we will use the private methods
instead and click Next.
4:57
We'll select both fields
to be included in equals, next,
5:01
both fields to be included in the hash
code, next,
5:05
and both fields added to the non-null
constraints and create.
5:09
Awesome.
5:14
Quick reminder, by default,
5:15
using double equals on objects only checks
if they are the same object in memory.
5:17
Overriding equals lets you define
5:24
what equality actually means
for your objects.
5:26
In our case, if the same person asks for
the same song, that's considered equal.
5:30
The hash code method returns
a unique integer aligned with its equals
5:36
logic Collections like HashMap and HashSet
5:41
depend heavily on hash code
5:45
It important to know
that if you override equals
5:49
you must also override
hash code and vice versa
5:52
Let's take a look at the generated code.
5:57
It checks memory
references, does proper casting,
5:59
and even equals on the song object.
6:03
Speaking of which,
we should probably go add an equals
6:07
and hash code to the song class too.
6:11
Time for another keyboard shortcut
that I wanted to show you.
6:14
You can use command O to search
for classes within a module quickly.
6:18
Let's type in song.
6:23
Great. Let's click on the song class.
6:25
In the song class, let's go ahead
6:28
and put the cursor on the line
above the current override.
6:30
override, then do command N
and choose the equals and hash code.
6:34
We want all the fields to be included in
both the equals and hash code.
6:39
Since
songs might have some nullable fields,
6:45
like missing artist names for example,
it's fine to allow for null,
6:48
so we'll leave the field
unchecked for non-null.
6:53
Great!
6:57
There's our new overrides
for the song class.
6:57
With the power of code generation,
we now have a song request class
7:01
with proper constructors, getters
and setters, toString,
7:06
equals, hashCode methods,
and an updated song class.
7:09
Now you can really appreciate
how much the IDE helps, right?
7:14
There are a few more code generation
tricks we'll encounter in the next videos.
7:18
But remember, everything is customizable,
7:22
so you can make generated code match
whatever Best Practice Your Team Follows.
7:26
Let's go ahead and get this new feature
implemented in the next video.
7:31
We'll need to do a little refactoring
to get there, but don't worry, the IDE
7:35
is going to help us.
7:39
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