1 00:00:00,012 --> 00:00:08,202 Last time I showed you how to use the how to design data recipe to design a simple 2 00:00:08,202 --> 00:00:16,026 data definition. In this video what I'm going to do is 3 00:00:16,026 --> 00:00:20,174 show you how to use the how to design functions recipe to design a function 4 00:00:20,174 --> 00:00:25,590 that consumes data defined by your own data definition. 5 00:00:25,590 --> 00:00:29,920 So this'll be a function that consumes what's called non primitive data. 6 00:00:29,920 --> 00:00:33,480 In this case it'll be a function that consumes the type city name. 7 00:00:35,440 --> 00:00:38,688 In this problem we're asked to design a function based on having a data 8 00:00:38,688 --> 00:00:41,938 definition. So the problem says, use the city name 9 00:00:41,938 --> 00:00:45,534 data definition below to design a function that produces true if the given 10 00:00:45,534 --> 00:00:51,150 city is the best in the world. And we're allowed to decide whatever best 11 00:00:51,150 --> 00:00:55,769 city we want. So what's going on here is that we know 12 00:00:55,769 --> 00:00:59,663 that city name itself, that's consumed by the function is going to be represented 13 00:00:59,663 --> 00:01:03,680 as a city name. City name type here. 14 00:01:03,680 --> 00:01:07,610 So that let's us get started right away on our signature. 15 00:01:07,610 --> 00:01:12,610 This function's going to consume a city name. 16 00:01:12,610 --> 00:01:16,540 And let's see, we're producing true. So it's a yes no question. 17 00:01:16,540 --> 00:01:21,064 We're representing it with a Boolean. So, that's going to be produce a Boolean. 18 00:01:21,064 --> 00:01:25,857 Now, what do we gotta do here? Well, we produce true if the given city 19 00:01:25,857 --> 00:01:30,645 is the best in the world. You could start with this as a purpose. 20 00:01:30,645 --> 00:01:38,194 Produce, I'll just copy it. Let's see, the next step of the recipe is 21 00:01:38,194 --> 00:01:42,776 the stub. We'll call this function best question 22 00:01:42,776 --> 00:01:45,020 mark. Remember there's a convention that 23 00:01:45,020 --> 00:01:48,732 predicates, the name of predicates, functions that produce a boolean value 24 00:01:48,732 --> 00:01:53,403 should end in a question mark. So we'll say best of some cn and for the 25 00:01:53,403 --> 00:01:57,592 stub I'll say false. And I'll just label this as the stub. 26 00:01:57,592 --> 00:02:00,663 Some and now we can get on to the check expects. 27 00:02:00,663 --> 00:02:04,569 And when we design a function using a data definition, this is a good time to 28 00:02:04,569 --> 00:02:09,110 go back to the how to design data definitions web page. 29 00:02:09,110 --> 00:02:14,324 Remember I get to that web page by first going to the design recipes page and then 30 00:02:14,324 --> 00:02:20,615 going to how to design data. And then this an atomic data definition, 31 00:02:20,615 --> 00:02:25,181 a simple atomic data definition, so I'll go there. 32 00:02:25,181 --> 00:02:30,857 And in this web page, each kind of data definition has a section called guidance 33 00:02:30,857 --> 00:02:35,970 on examples and tests. And this gives us some hints about making 34 00:02:35,970 --> 00:02:39,645 check expects for this particular kind of data definition. 35 00:02:39,645 --> 00:02:43,531 So, whenever you design a function using a data definition, go to this part of how 36 00:02:43,531 --> 00:02:48,190 to design data definition webpage and get some of this guidance. 37 00:02:48,190 --> 00:02:51,355 For our purposes now I'll tell you what it says as we keep working. 38 00:02:51,355 --> 00:02:54,630 Lisksys says, that one or two examples are enough. 39 00:02:54,630 --> 00:02:57,660 And that also is true of a function that produces a Boolean value. 40 00:02:57,660 --> 00:03:00,325 If you have a function that produces a Boolean value, you should always have at 41 00:03:00,325 --> 00:03:03,995 least two examples, one that produces true and one that produces false. 42 00:03:03,995 --> 00:03:11,010 So let's see, if I say check expect best? And now I need to give some city name, 43 00:03:11,010 --> 00:03:15,008 like let's say Boston and is that the best city in the world? 44 00:03:15,008 --> 00:03:20,636 Well, remember the problem description said we were free to choose any best city 45 00:03:20,636 --> 00:03:23,658 we want. So I loved Boston when I lived there but 46 00:03:23,658 --> 00:03:25,802 let me say that it's not the best city in the world. 47 00:03:25,802 --> 00:03:31,178 Let's use a imaginary city for the best city in the world so no one can possibly 48 00:03:31,178 --> 00:03:36,282 be upset. We'll use Hogsmeade as our best city in 49 00:03:36,282 --> 00:03:41,489 the world. Now that is the best city in the world. 50 00:03:41,489 --> 00:03:47,560 And probably this is a good time to make the purpose more specific. 51 00:03:47,560 --> 00:03:56,830 So we'll say it produced true if the given city is Hogsmeade. 52 00:03:56,830 --> 00:03:59,720 Okay, so now I've got two tests and the stub. 53 00:03:59,720 --> 00:04:05,630 I should run and I get that one of the two tests fail. 54 00:04:05,630 --> 00:04:09,668 But both tests actually manage to run. So that tells me that my tests are well 55 00:04:09,668 --> 00:04:11,690 formed. Both tests ran. 56 00:04:11,690 --> 00:04:15,710 So now we can keep going. Let me start by making the screen bigger 57 00:04:15,710 --> 00:04:17,970 again. So, let's see. 58 00:04:17,970 --> 00:04:22,770 Signature, purpose, stub, check-expects, now the template. 59 00:04:22,770 --> 00:04:31,140 Well the way we get the template now is we're going to put a note here that says, 60 00:04:31,140 --> 00:04:39,180 took template from city name. Because this function consumes a city 61 00:04:39,180 --> 00:04:43,360 name, city name is a data definition that we designed. 62 00:04:43,360 --> 00:04:44,950 So we're going to take the template right here. 63 00:04:44,950 --> 00:04:52,730 And what I'm going to do is copy it. I'll have this note about where I got it. 64 00:04:52,730 --> 00:04:55,589 The first thing I gotta do is remember to rename it. 65 00:04:55,589 --> 00:05:01,420 I'll rename it to best. And now here is the template. 66 00:05:01,420 --> 00:05:04,140 Now let's see what I do next, this is a function that has two cases. 67 00:05:04,140 --> 00:05:09,380 A false case and a true case. So a good first thing to do when you have 68 00:05:09,380 --> 00:05:13,070 a function that has two cases is to add an if to the template. 69 00:05:13,070 --> 00:05:16,590 So what I'm going to do is I'm going to copy the existing template. 70 00:05:16,590 --> 00:05:22,418 I'll put an if in here and for both the question and the true and false answer 71 00:05:22,418 --> 00:05:29,769 I'll put the copy of the template. This is a way now of telling me, listen 72 00:05:29,769 --> 00:05:33,309 there's an if and in order to fill out all these parts dot dot dot cn is what 73 00:05:33,309 --> 00:05:42,681 you have to work with. Let's see cn is the city name. 74 00:05:42,681 --> 00:05:53,910 So let's see in one case the city name is Hogsmeade. 75 00:05:53,910 --> 00:05:56,284 Let's see. If the city name is Hogsmeade then this 76 00:05:56,284 --> 00:05:59,630 example right here tells me I should produce true. 77 00:06:02,600 --> 00:06:08,800 So that's true and if the city name is not Hogsmeade then this example on the 78 00:06:08,800 --> 00:06:16,834 purpose tell me I should produce false. So that's going to be false. 79 00:06:16,834 --> 00:06:21,020 Let me run it. Oh, I forgot when I was up here to 80 00:06:21,020 --> 00:06:27,322 comment out this stub, there we go. Both tests pass. 81 00:06:27,322 --> 00:06:33,420 Now, let me make couple points here. One is, did you notice that there was a 82 00:06:33,420 --> 00:06:37,830 point in the template where this was dot dot dot cn and then I deleted that and 83 00:06:37,830 --> 00:06:42,886 replaced it with true. This is an important thing about 84 00:06:42,886 --> 00:06:45,961 templates. Templates tell you everything you have 85 00:06:45,961 --> 00:06:49,483 available to work with. They don't tell you you have to use all 86 00:06:49,483 --> 00:06:52,778 of it. So often times you'll be deleting things 87 00:06:52,778 --> 00:06:55,890 out of templates. Another point to make about this function 88 00:06:55,890 --> 00:06:59,398 is, let's think about this. This says if string equals c and 89 00:06:59,398 --> 00:07:03,279 Hogsmeade is true. Produce true. 90 00:07:04,450 --> 00:07:08,210 Otherwise if string equals C and the Hogsmeade is false, it produce false. 91 00:07:10,260 --> 00:07:12,948 When you follow a very structured approach to designing code, sometimes you 92 00:07:12,948 --> 00:07:16,460 produce code that's a little bit more cumbersome than it needs to be. 93 00:07:16,460 --> 00:07:21,825 This function could instead just be this. Let me comment out this version of it and 94 00:07:21,825 --> 00:07:25,439 run it again. And now that's working too. 95 00:07:25,439 --> 00:07:30,702 But, you know, don't worry if you produce this version of the function. 96 00:07:30,702 --> 00:07:40,807 This is clear and it's well tested. It's just if you ever find yourself 97 00:07:40,807 --> 00:07:50,324 writing if xxx true, false. That's always the same as just xxx.