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
Sometimes it's best to not expose them at all and use their declarative helper methods to achieve the task
Learn More
- Watch Stuart Marks discuss Optionals during the Virtual JUG (Java User Group) 24 hour conference.
- Here is a nice cheat sheet on Optionals from your friends over at ZeroTurnaround.
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
Okay so let's try to help Arnold
be a little less upset so
0:00
he's talking right here
about this he says.
0:05
How is this any different
than an all check?
0:06
Optionals make me do that face palm thing.
0:08
So, basically he's right.
0:12
Right, so this is a little dance here that
you need to do so this get sheet four
0:14
is returning an optional and that means
it could either be present or absent.
0:18
So he needs to check first
before he calls this get and
0:22
if you haven't seen this before
it's a little surprising.
0:25
So let's let's calls one of the so
you can see.
0:27
Well he probably did before he put that
protection there so let's say hearty and
0:30
once they get for it.
0:34
Now you can see that only Andrew,
me, David, Elaine are playing.
0:35
So let's put something in
there who's not in there.
0:38
So we'll get this sheet for Tiger and
I'm sorry go ahead and I run this.
0:41
That is just getting the optional out but
if I go ahead and I call get on that.
0:45
Though we basically have an all
point exception he's right right.
0:51
So it says no such element exception no
value is present because I tried to access
0:54
a value that wasn't there.
0:58
So get looks like it's the right thing to
use and of course we're gonna try to do it
0:59
and it's gonna blow up like this and
you are going to face palm probably.
1:03
So I'm wanna go ahead, let's get
rid of that and let's go back and
1:07
let's see if we can't do anything.
1:10
So again he's checking is there?
1:11
If it is and
1:13
only then can I go get it so otherwise
throw this illegal argument exception.
1:13
Now, really what should be happening,
in my opinion, is if he's
1:18
calling this get sheet, or it should
be returning in an optional here and
1:21
then later during exception, the get
sheet for should be thrown exception and
1:24
maybe he doesn't know
when this was written.
1:28
Maybe this wasn't thought about now.
1:31
This probably actually could use
a little important refactor and
1:33
this should probably be a map.
1:35
Let's just fix this and then we can
come back later and refactor it.
1:37
So findFirst is going to
return an optional, right,
1:40
because what happens if
a name's not found, right?
1:43
Like we just saw, right?
1:45
So it'd return an empty optional.
1:46
So if one is found return that.
1:47
Otherwise so
1:51
let's make this not be a sheet
anywhere with your regular sheet here.
1:52
And of course it's angry now
because it's not an optional.
1:56
So if it can't find it let's use very
somewhere URL we're going to say or
1:59
URL throw.
2:03
And what the stakes here is a supplier.
2:07
So we are going to need
to return the exception.
2:10
And that was as we saw
here was this exception.
2:14
So I'm going to go ahead and
just cut this out of there.
2:18
And drop this in here.
2:20
Okay, and
let's make sure that name is past in.
2:21
So it's not an option in order
words that's an invalid comment.
2:30
Sorry Arnold?
2:34
Hopefully this will help you
not do the face palm anymore.
2:35
So we're going to just do a get sheet for
and
2:38
it looks like we can just do this little
bit of chaining off of this get sheet for.
2:43
Should help him out there a little bit,
right?
2:53
So we're gonna throw, if we can't find
the sheet, so we're pretty certain that
2:58
there's a sheet there and we're gonna
set the score on there as it goes.
3:02
So let's go ahead and let's run,
make sure that we're still working.
3:06
Awesome.
3:08
So now let's take a look down here and
I see this OptionalDouble.
3:12
So that's interesting.
3:16
OptionalDouble is not optional of double.
3:18
It's an OptionalDouble.
3:22
So it's a primitive type.
3:23
So that's coming from the course average.
3:24
So let's go ahead let's
take a look at that.
3:27
If we're in the course average or so
3:30
this line here is using the sheet of the
getCourseAverage, so let's go over there.
3:32
And it is returning an average.
3:37
And look,
it's returning an OptionalDouble.
3:39
And, of course, the reason for that.
3:41
Look here, so it's going to
filter anything that's not zero.
3:43
So again if we're trying to
make this app where it's going,
3:45
as we're going right as we're walking
through it instead of at the very end.
3:48
There's not gonna be any score so
what's the average of nothing?
3:52
What's would happens there?
3:55
We get an OptionalDouble empty.
3:57
Okay, so I'm going to preemptively
take care of Arnold, right?
3:58
So we could go ahead and
check in our dashboard over here.
4:04
We have the option here we could
say if the course average is empty
4:08
then print this out but there's a cleaner
little way here this kind of fun.
4:13
It's a declarative way.
4:16
So if we say sheet .getCourseAverage there
is a method on there called if present.
4:17
So what this will do is it's only going
to do this if the value is present.
4:27
So we know that the average
is going to come through and
4:32
from there we just want to print it,
right?
4:35
We want to that's that's the action that
we want to do and this is a course a side
4:38
effect but we are in here inside of
a consumer which we're allowed to do.
4:41
So let's go ahead,
let's drop this onto a new line,
4:46
make this a little bit prettier and
we'll say plus average.
4:49
And now what's going to happen
is that's only going to show up.
4:55
Should we need a second missing
printer which is common.
4:59
All right so let's do the same for
this adjusted course average
5:02
because it is also an OptionalDouble but
we only want to show it.
5:07
If there is an average or
so there is a if present.
5:10
And again, it's a consumer, right,
5:15
so we know that it's going to take
a double because it is an OptionalDouble.
5:17
So it's going to pass across
a primitive type there.
5:20
So if we wanted to do
anything special here like,
5:23
highlight it with a different color or
whatever, we could do.
5:25
So now he's never going to see that empty.
5:29
So let's run that again.
5:31
So we don't have any scores now so
5:33
see there's no in this detail
here we don't ever see that.
5:34
Let's flip back to app let's populate
it and make sure that it's still coming
5:38
up because it should show if it's
present right let's run it over time.
5:41
And then we have the averages back and
it's not wrapped in that optional
5:47
anymore because it's unpacked and it
knows that it's there so it shot it out.
5:50
So no more optional in our dashboard here.
5:53
And I think that should be a lot less face
palming for our buddy, Arnold, right?
5:56
[LAUGH] Arnold the Face Palmer,
Arnold Palmer.
6:03
That was a professional dad joke.
6:06
Okay, so the moral of the story is
that optionals can be frustrating.
6:09
And remember that they are really only
made to be used to be for a return values.
6:13
Don't fall for other misuses.
6:18
There's have been some crazy things that
people have done where they've tried to
6:19
use them in parameters or in classes.
6:22
Don't do that just use
them in return value.
6:24
And I hope from this you can see that you
don't actually even need to expose them to
6:26
your API as long as however you're using
it as what you're trying to use it for.
6:30
Just take the optional as it comes out and
do with it what you should, right,
6:35
like treat it declaratively don't
try to treat it imperatively.
6:39
Don't make other people deal with it.
6:42
If you are only ever going
to use it in a certain
6:44
way you will see a lot of people angry
just like Arnold about optionals and.
6:47
A lot of times it's a misunderstanding.
6:52
So check the teachers notes for more
information I have a link to some great
6:54
talks one from the API designer
on how to use optional properly.
6:57
It's a great talk you should check it out.
7:00
It's from the virtual jug which you
should also be watching as well it's
7:02
a great job users group that no
matter where you are in the world.
7:05
You can take a look and
see what help people are using job.
7:08
As always I am looking for
your feedback on workshops like this
7:11
love to know what you
thought about this format.
7:15
Please keep me posted.
7:18
I'd love to get more content
like this in your hands.
7:19
So keep at it.
7:23
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