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
"Optional is intended to provide a limited mechanism for library method return types where there is a clear need to represent "no result" and where using null for that is overwhelmingly likely to cause errors." Unfortunately they get misused, let's clear that up through some common pitfall clean-ups.
Follow along
The code for this workshop lives on GitHub in the Java Optionals Tiny Pencil repository. There is a branch named live that follows along.
-
0:00
[MUSIC]
-
0:04
Okay, so let's imagine for a second that we got hired to work at a Pot Pots,
-
0:09
a local mini golf place in our town.
-
0:11
Now, they have an app that's used to track games.
-
0:14
So a couple things you need to know is a party is a group of people, right.
-
0:17
So they're the people who come in and do the mini golf thing.
-
0:20
And the way things used to work, the analog way was we would give them a sheet.
-
0:24
And then these tiny little pencils actually and
-
0:26
the name of the package is tiny pencil, that's what this is based on, right.
-
0:29
So each of these players has the sheet and they keep their score and
-
0:32
they turn him in and we used to run them through this big dashboard thing.
-
0:35
And well, here let me just show you, let me show you the dashboard.
-
0:37
So they hand on their sheets at the end and we would calculate some stuff for
-
0:41
them, right.
-
0:42
So you can see here that there are some optional here.
-
0:46
This is kind of weird looking that we're kind in the middle of things right now.
-
0:50
And what's really nice about Pop Pots in my opinion is we
-
0:55
have the hardest windmill in town.
-
0:57
So people like to come and they challenge each other at that.
-
1:01
So we've said that each one of these holes that you go to attempt
-
1:04
should really only take you six.
-
1:05
And we don't want to make people feel bad because sometimes it takes like 25
-
1:08
times to go and get this through.
-
1:10
So we set things at six, so
-
1:11
but people like the company like to challenge themselves.
-
1:13
So they wanna keep track of their actual score and then we also adjust it for them.
-
1:18
So you'll see here that that's what the scores are, right.
-
1:20
So Dave, his actual score was 61 but if we adjusted it to 60.
-
1:24
So that's the sort of thing where he went over a little bit.
-
1:26
We recently just upgraded to Java 8.
-
1:29
We were in Java 5.
-
1:31
So we've got huge leaps, right?
-
1:33
Now the code base is half the size that it used to be and it's so much more legible.
-
1:38
Now, this dashboard here.
-
1:39
It used to just be done at the end of the game, when the party was all over.
-
1:44
But what we're hoping to do is get this so that it can be on people's phones.
-
1:47
And so that you'll know as you're going if you're winning or not.
-
1:50
That's where we're headed.
-
1:52
Now our original developer, his name's Arnold, he's still here.
-
1:55
He likes the new code but he is not happy about optionals.
-
2:00
It's making this transition super hard.
-
2:02
Actually let's go ahead,
-
2:03
I just heard him yelling from his cube about why does Welder say optional.
-
2:08
So let's take a look at what's going on in the dashboard and
-
2:11
see if we can't get him to calm down a little bit.
-
2:14
So I'm going to go here and I'm going to navigate to this dashboard code.
-
2:18
And you can always find Arnold's comments because they're in all uppercases.
-
2:23
And typically have a bunch of trailing exclamation points,
-
2:27
saying that he was yelling.
-
2:29
He's angry and rightfully so, this is kind of these options are kind of a little
-
2:33
weird to play with at first and people give him a bad rap.
-
2:36
I think a lot of times they don't understand what can happen.
-
2:39
So let's let's help him out here.
-
2:41
And I would help him out for two reasons.
-
2:42
One, because in order to finish this project,
-
2:44
we're really going to need to get Arnold on board.
-
2:46
Not only because we don't want these screaming comments on our code.
-
2:50
But we just wanted to understand how these optional should be working.
-
2:53
Because they're a little bit different than the imperative way of thinking.
-
2:56
So remember in our output, it showed it wrapped in an optional.
-
3:00
So we know this is returning an optional, so let's go ahead and
-
3:02
let's take a look at it.
-
3:04
So let's navigate into this getAdjustedWinner, okay.
-
3:08
So it's saying getAdjustedWinner but it's returning one of these sheets.
-
3:11
And these sheets where the score that's there,
-
3:13
so let's go ahead, let's see first where these are being used.
-
3:17
This is kind of the trick to understand,
-
3:19
let's see where does this getAdjustedWinner is being use?
-
3:22
It's just here, this is the only place that it is.
-
3:23
So really what we want here is, we want the winners name,
-
3:26
we don't want to print out the optional even at all.
-
3:29
So okay, so it is coming in and
-
3:31
saying if the remaining hole count is less than the par.
-
3:33
So if they played at all, then you could find the winner, the current winner,
-
3:36
so that's cool.
-
3:37
So we really should just be returning the name, right.
-
3:41
Actually that's interesting.
-
3:42
What happens if somebody hasn't played?
-
3:43
Let's take a look back at our app.
-
3:45
Let's turn this helpers off.
-
3:47
Let's go ahead and run this.
-
3:48
And let's see what happens.
-
3:50
What is said, who?
-
3:51
The winner is optional empty.
-
3:53
We don't wanna say that, right.
-
3:55
So let's go back to our party here and
-
4:00
we want this to be the name, right.
-
4:03
So if it exists, we want it to be the name.
-
4:07
So optionals have a method on them.
-
4:09
I'm going to chain and remember that this is not optional here.
-
4:11
This isn't the stream.
-
4:12
So minimum is going to return an optional and an optional has the ability
-
4:19
to say map and let's see, what do we want here.
-
4:24
We really just want to return the name, right?
-
4:27
So she has a name, the players names.
-
4:29
So we have get name.
-
4:31
So we'll do that and then you'll notice that it's saying, wait, you can't do that.
-
4:34
And it says that this is a little confusing for us, so
-
4:37
it's bad return type methods cannot convert Java string to you.
-
4:41
And so what that's talking about is because it's an optional sheet.
-
4:44
We want this to be a string, but we're still gonna have that problem, right.
-
4:50
So because the optional needs to do that, but there's a nice function of this.
-
4:54
So what happens if it didn't find that, right.
-
4:56
What happens when it's optional empty, that's what we need to tell it.
-
4:58
So, they have this great feature called orElse.
-
5:01
Now that seems a little threatening, right?
-
5:03
It's like give me that value or else!
-
5:06
So, we'll say what do we want to see?
-
5:09
We want to see, no winner currently, right?
-
5:11
When there's nothing and there's nobody there.
-
5:13
So what's going to happen there is the optional is going to return the name or
-
5:18
if it's not there, it will return No winner currently.
-
5:20
Now we've made it return a string and
-
5:23
we know the only usage of this is in the dashboard.
-
5:26
So it's right here.
-
5:27
So I think we just did this.
-
5:28
We're gonna make Arnold happy.
-
5:30
Let's do this.
-
5:32
Let's run that, let's see what we got.
-
5:33
So no one are currently broke because I have not commented out.
-
5:36
Let's bring our helpers back and if I start it up, here we go.
-
5:42
So the winner was Dave, each time this is random.
-
5:45
Okay so let's come back into our party here and let's see.
-
5:49
See if there's another option, there is.
-
5:51
So we got to remember that if we change one of these, right,
-
5:54
we've got it we kind of got to change them both.
-
5:56
So again, this will be good practice.
-
5:59
And just in case, Arnold goes to use this.
-
6:01
You can see that it's not used yet, it's never used, but
-
6:04
I'm going to fix this just in case, right.
-
6:06
Maybe he's planning on using it, so
-
6:08
I don't want him to fall into the same problem, so we'll say map.
-
6:11
And again, we want to get the sheet and
-
6:14
we use the method reference here to get the name of the sheet.
-
6:17
And again, if it doesn't exist,
-
6:22
we want to know winner yet there we go, perfect.
-
6:28
Speaking of angry look at this one, this is a common problem.
-
6:33
Let's take a look at this in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up