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
Why not let your IDE write some code for you? It knows the best practices.
Additional Information
- I touch on equality in the Workshop - The Thing About Strings
- Android 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
[MUSIC]
0:00
>> So when we delivered our minimum viable
project, or MVP, for the karaoke project,
0:04
there was another story that
didn't quite make the cut and
0:08
it got left in the backlog.
0:11
It was this.
0:13
As a KJ I should know which
singer requested the song so
0:15
that I can call them up to the stage.
0:18
Well as it turns out, after some usage
most KJs are dying for that feature.
0:21
What happens is this.
0:26
They keep calling the song and no one
comes up because they either forgot or
0:27
they are not paying attention.
0:30
So let's see if we can add this
feature request to our app real quick.
0:32
>> A song request is
definitely a model right?
0:35
So let's navigate over there.
0:39
And, in the model package we'll right
click, and say New, Java Class.
0:42
And let's call it SongRequest.
0:49
Cool.
0:51
This is some default setting.
0:54
I'm gonna get rid of it.
0:56
Okay.
So, let's see.
0:58
We know we want a couple of things.
1:00
Right?
We wanna have a private
1:01
String let's call it mSingerName.
1:04
We want a singer and let's go ahead and
pop the song in there so private
1:08
Song and mSong.
1:14
Now remember, it was in the same package
as a song so we didn't need to import it.
1:20
So I want to explore something
now called code generation.
1:25
So we wanna have a constructor, right?
1:29
The good new is is that
it can create one for us.
1:31
Because remember, what we want it to do is
we want it to pass in the singer name and
1:34
the song when you get created.
1:37
Let's go ahead and we'll go up to Code.
1:38
Then we'll choose generate.
1:40
Yeah, we're gonna generate a constructor.
1:42
We want it to do both of those, right?
1:45
We want it to do both those things.
1:46
Okay.
1:48
Whoops.
1:49
Okay, so almost.
1:51
The problem is that the parameters here
are following our naming structure,
1:52
where we put the member variables
in at the front, right.
1:56
Remember, that's just a coding
style that we're doing and
1:59
each project that you work on
might adhere to different styles.
2:03
Well the cool thing is that
your editor knows that,
2:06
we just need to tell it our style.
2:09
So I am going to go ahead and I am going
to Cmd+Z this, get rid of that and
2:10
let's go under File,
Other Settings, Default Settings.
2:13
And if we go under Editor,
Code Style, Java and Code Generation.
2:21
Here's the naming stuff.
2:28
Whenever we have a field,
we're gonna prefix it with an m.
2:29
Okay, that's really all you have to do.
2:33
Say Apply.
2:36
Let's go take a look and
see if that worked.
2:38
So now if we come over here,
the generating code thing,
2:41
I don't know if you saw it, it was Cmd+N.
2:44
So I'm gonna generate a constructor and
we're gonna use both of those.
2:46
Blam, there it is.
2:53
So, setting that up.
2:54
Song got moved down there.
2:56
Let's move him back up to where he was.
2:57
There we go.
2:59
Okay.
3:02
So now the boring part of
adding getters and setters.
3:03
Did you see that in the list?
3:07
We can do that too.
3:08
We don't need to do that anymore.
3:08
Let's choose getter and setter and
let's make them for both.
3:10
You'll note here that there's templates.
3:15
We're gonna leave them.
3:17
That's fine.
3:18
Look at that.
3:19
We didn't have to write all that code.
3:20
What else is in that generate thing?
3:22
Let's see.
3:25
toString, that's something handy.
3:26
So we will generate toString for
both of those, yeah?
3:29
We're going to both of those.
3:33
Okay well that's one way to do it.
3:36
Well look, it remembered to do
the override for us, which we learned.
3:38
The override annotation.
3:42
And if we wanted to change this to make
our own template, we could, right?
3:43
So let's go ahead and I'm gonna undo that,
and we will again, Generate toString.
3:47
Up here there's these templates and
we had talked about that a little bit.
3:52
So there's different
options that you could do.
3:58
You can also make a new one,
if you wanted to.
4:00
If you want to come here
you can add a new template.
4:02
Okay, and if you really feel like nerding
out that's something that you can do.
4:05
To make it read however you want to,
4:08
to be the default it will automatically
generate the same looking toString.
4:10
The cool thing is when you save this it
will be saved into the project file so
4:14
everybody else can use
the same toString format.
4:18
I think we're fine with
the final string concatenation.
4:22
Or let's.
4:25
Yeah, I think that's fine.
4:27
Okay.
4:30
Great.
4:31
Okay, so one thing that we didn't
talk about before is equals.
4:32
And you can actually override
the equals method and
4:37
this pretty much should be done on every
class that we create that we plan to use,
4:39
especially ones that we
use in the collections.
4:43
Now one reason that I didn't show you
how to do this in the last course
4:46
is because it's much easier to
have the IDE do it for you.
4:49
Ready let's do it and
then we'll chat about it.
4:52
Okay?
So, I'm gonna do,
4:54
again, Cmd+N and I'm gonna
generate the equals and hashCode.
4:55
And let's just use the IntelliJ Default.
5:01
And there's a couple of options here.
5:05
I don't think we should use getters.
5:06
We should use the private methods and
we will go over this in the future.
5:08
Yeah, so we want both of these and
we want both of these in the hash code.
5:13
And the non null fields.
5:19
Let's not create one of
these without nulls.
5:24
So, those are non nulls.
5:26
Okay, so remember that using double
equals on objects just means
5:29
that they point to
the same object in memory.
5:34
So what we really want to write here is
we want to determine what equality means.
5:38
In this case, if the same person asks for
5:43
the same song,
I mean that's pretty much equal, right?
5:46
It's sameish.
5:49
So things like hash maps and hash sets
use a thing called the hash code.
5:51
We talked about this a little bit.
5:55
And under the covers,
it stores things sensibly.
5:57
So it returns a uniquieish integer,
that follows the same logic that equals.
6:00
So therefore, if you overwrite equals or
hash code, you should override them both.
6:04
So let's both of those on the page.
6:08
So basically,
this generated code just checks to see
6:10
if things are equal in memory.
6:12
Right, if they are the exact same object,
than of course they're equal.
6:14
And then otherwise it just goes ahead and
does the casting that we had done before,
6:18
and then it upcalls equals.
6:22
And oh, look here.
6:24
It's upcalling equals on Song.
6:24
So, you know what we should probably do,
6:28
we should probably go write the equals for
Song.
6:30
So let's go over there to Song.
6:33
So I wanna show you another option.
6:34
So we could do Cmd+O.
6:36
And this is basically doing searchable,
but this is only for classes.
6:39
So if you have a whole bunch of classes,
this is kind of handy.
6:43
Cmd+O is right in your module as well.
6:46
So in the song, let's just go ahead and
let's generate equals and hashCode.
6:49
And we'll do the same thing,
intelliJ default.
6:53
All those fields are valid right?
6:56
It's gonna be equal.
6:58
Yeah, all of those are true in the hash
code and anybody could create one of these
7:00
songs and it's possible that they could
put a null value in there, right?
7:05
The absence of that, so I am going
to go ahead and leave those as null.
7:08
Okay, so it's generated those for
us as well.
7:15
So the hashCode always
looks a little bit strange.
7:18
One thing that might look
a little bit weird to you and
7:21
is a little bit new is this
thing called a ternary operator.
7:23
So the way that this works is if
this is true then this happens.
7:26
It returns whatever the result is,
if this is true.
7:33
Otherwise, it returns whatever this is.
7:36
Okay, so, pretty cute, right?
7:39
I mean, sometimes it's the most
clear way to express things,
7:41
but often, it proves less readable.
7:43
You're bound to run into them, so
I'm glad that you got to see them, but
7:46
I just wanted to let you know
about ternary operators.
7:49
I'll put a link in the teacher's notes.
7:53
Cool.
7:55
So now we have our
generated code over here.
7:55
I think we're ready to use it.
7:59
>> I know, I know I owe you like
five more dunk tank balls for
8:01
making you type all that stuff before.
8:03
But how much do you appreciate it now?
8:06
Right?
8:07
There's a couple more code generation
tricks that we'll encounter in the next
8:08
couple of videos.
8:11
Remember, everything is customizable,
so you can make generated code adhere to
8:13
whatever your team determines
to be the best practice.
8:17
So, let's get this feature implemented.
8:20
We're gonna need to do a little
refactoring to get there, but
8:22
the IDE is gonna help us.
8:24
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