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
We Java developers have been focussing heavily on imperative style, but declarative functions are available. Let's get used to the difference.
-
0:00
In order to start our journey on thinking functionally, one really important concept
-
0:05
to dial in on is the difference between imperative and declarative coding styles.
-
0:11
The imperative style focuses specifically on how to perform the operations.
-
0:15
Typically at a low level, taking the system it is running on into account.
-
0:19
Declarative style focuses more on
-
0:22
what specifically you are attempting to accomplish.
-
0:26
The imperative style is definitely more difficult to read as there are more lines
-
0:30
of code and declarative is usually very clear as to what is happening.
-
0:35
Let's walk through an example one that you've probably already seen and
-
0:38
it will help demonstrate the differences.
-
0:41
For those of you with people in your lives that have food allergies,
-
0:44
you know how difficult it is to find something that they can eat.
-
0:48
My daughter has an egg allergy.
-
0:50
It's a bummer.
-
0:50
It's pretty horrible.
-
0:51
It seems like everything has eggs in it.
-
0:54
Cupcakes, taffy, ice cream.
-
0:56
You know, everything a kid likes to eat.
-
0:59
Now as a parent, I am constantly needing to check
-
1:01
a list of ingredients to see if it indeed contains eggs.
-
1:05
Let's write a quick simulation of this in code, in both styles.
-
1:10
So here I have the current version of IntelliJ IDEA open and
-
1:13
I'm just going to make a new project.
-
1:15
I'm gonna click create new project.
-
1:18
Cool. And I'm gonna just leave
-
1:19
the defaults here.
-
1:20
I'm gonna click next and I do want to just make a quick command line.
-
1:26
Let's go ahead and call this exploration.
-
1:31
Okay so that's set everything up for us.
-
1:33
Let's go ahead and let's start write our code here and suggest it.
-
1:39
Oops I indent with two space I'm using the Google style.
-
1:42
So we'll say list the string and those are ingredients, right?
-
1:50
And we'll make a new Arrays.asList.
-
1:53
And we'll just go ahead and we'll enter those guys, that's what we want.
-
1:59
So let's see, we have flour, what else do we have?
-
2:03
We've got some salt.
-
2:06
Delicious cupcakes here, some baking powder, butter,
-
2:11
I don't know how to cook really, eggs and here comes the milk.
-
2:16
All right, and we will close off with a semicolon here.
-
2:22
Okay, so let's solve our issue of checking whether or
-
2:25
not our ingredients has eggs, okay.
-
2:28
I'm gonna go ahead and give us more space.
-
2:30
Get rid of all the windows that are open, Here we go.
-
2:33
Okay, so first thing's first.
-
2:35
We'll need a variable to store our state, right?
-
2:38
So we'll wanna say boolean hasEggs, and we'll just go ahead, and
-
2:42
we don't know yet.
-
2:42
We don't know if it has, and so we said false.
-
2:45
And now, what we wanna do is we wanna loop through each of those ingredients.
-
2:49
So we can do that by starting to define a for loop, right?
-
2:51
So we'll say for and now we need another one of those temporary
-
2:56
variables to store in the loop where we're gonna currently process.
-
2:58
So we'll say int i for index, kind of standard thing there, right?
-
3:05
And we'll make another temporary variable, right?
-
3:09
So now we've got 2, we've got hasEggs and i, two temporary variables.
-
3:14
And now we wanna make sure that we go through all of the ingredients.
-
3:16
So we wanna see if i is less than the ingredients.
-
3:20
And we wanna see if it's as long as it is which was size.
-
3:24
And we've gotta remember that it's zero-based, right?
-
3:27
It starts at zero, so it's gotta be less,
-
3:29
not less than or equal to, always have to think about that there.
-
3:32
And then we're gonna go ahead and we're gonna increment.
-
3:35
Then we need to check if that specific ingredient, so
-
3:38
what we need to do is we need to pull it out, right?
-
3:41
We need to pull that one out.
-
3:42
So we'll say String.ingredient equals ingredients.get, and
-
3:48
we're gonna pass in that index that we've been running through the loop with, right?
-
3:52
Okay so then we wanna see does this ingredient that we pulled out,
-
3:59
does that equal eggs, right?
-
4:06
Okay, and if it does we will for sure.
-
4:09
We'll say hasEggs, we're gonna set that to true.
-
4:13
You know what since we did know it hasEggs,
-
4:16
we don't need to look at the other stuff right?
-
4:18
We can finish here, so we can just go ahead and say break.
-
4:23
Let's look at this code here for a second.
-
4:25
That's a lot of code and brainpower needed just to understand what's happening.
-
4:30
Now we've done this a lot and probably see code like this quite a bit.
-
4:34
So it's probably feeling kind of familiar.
-
4:37
But just imagine for a second if you hadn't.
-
4:40
There's a whole lot of what is this doing going on, right.
-
4:44
So now that we have our variable, we can go ahead and see if it has eggs.
-
4:50
Gotta break the news to my daughter and say, sorry sweetheart.
-
4:56
It has eggs.
-
4:58
Sad face emoji.
-
5:00
Now of course there's been some evolution in looping.
-
5:04
So we can of course clean up this cognitive overload a little bit of all
-
5:07
these things, right?
-
5:08
So we can, we can just go ahead we can say for
-
5:11
the ingredients in ingredients, right?
-
5:17
This enhanced four loop.
-
5:19
So we can lose this index cause each one is gonna have a specific time, right?
-
5:24
We don't need to know how long the list is anymore.
-
5:27
This reads nicer, but it's still an awful lot
-
5:31
when all we really want to know is, does ingredients contain eggs.
-
5:36
We're still saying give me each of the ingredients and
-
5:39
I'll tell you when to stop.
-
5:40
So the more declarative way to ask this question,
-
5:44
is just to ask the question, right?
-
5:47
So, let's do this.
-
5:49
If ingredients.contains,
-
5:55
Eggs, Right?
-
5:59
We don't need this.
-
6:01
And there's nothing new here.
-
6:03
Contains has been around since the collections framework was introduced.
-
6:07
But it's declarative.
-
6:09
Look at how it explains what it is that we're trying to do.
-
6:12
It's not specifying how.
-
6:14
Now this can be optimized efficiently behind the scenes.
-
6:17
And this is how it works in the real world, right.
-
6:20
If I'm at a restaurant, and I wanna know about a possible cupcake for
-
6:23
my daughter, I don't go, can you please tell me every ingredient in this cupcake.
-
6:28
Now don't worry I tell you when to stop listing them and
-
6:30
the case that I happen to hear what I'm concerned about, right.
-
6:33
I simply say, does this contain eggs?
-
6:36
And the waiter or waitress has their own method of knowing.
-
6:39
How they get the answer isn't really something I need to concern myself with.
-
6:43
I'm really just concerned with the answer to my question.
-
6:47
So ready for it?
-
6:48
This is the start of the shift of your thinking.
-
6:51
Functional programming is declarative.
-
6:53
It'll take a little bit of shifting in you programming mind, but
-
6:56
we do it in real life.
-
6:57
So it should feel more normal than how we already force our imperative mind to work.
-
7:03
One more example of how this looks in something you've probably seen.
-
7:07
Is SQL or Structured Query Language.
-
7:10
It's all declarative.
-
7:11
Even if you haven't seen SQL before, this is how you write the statements.
-
7:15
The language allows you to declare
-
7:18
very specifically what you would like to get back.
-
7:21
In this case, it's some specific fields like first name, last name, and email.
-
7:25
For a student with the id of 123.
-
7:28
Notice how it doesn't specify how.
-
7:32
This allows the database to do whatever indexing or
-
7:35
caching it needs on whatever platform that implements the language specification.
-
7:42
Let's stay in the declarative mindset and introduce our first function.
You need to sign up for Treehouse in order to download course files.
Sign up