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