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