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