1 00:00:06,570 --> 00:00:09,780 Here's another sample, how to design functions problem. 2 00:00:09,780 --> 00:00:14,360 In this problem we're going to see a few extra wrinkles including an example of 3 00:00:14,360 --> 00:00:20,070 how not to write a good purpose, some problems that can come up with tests and 4 00:00:20,070 --> 00:00:22,862 some issues having to do with copying part of what's in the template. 5 00:00:22,862 --> 00:00:28,120 Okay, so here's the example. I've got to design a function called area 6 00:00:28,120 --> 00:00:32,410 that consumes the length of one side of a square and produces the area of the 7 00:00:32,410 --> 00:00:38,890 square. Let's see, so the length is going to be a 8 00:00:38,890 --> 00:00:45,000 number, and the area is also going to be a number, so the signature is number to 9 00:00:45,000 --> 00:00:53,210 number. And the purpose is given length of one 10 00:00:53,210 --> 00:01:00,458 side of square comma, produce the area of the square. 11 00:01:00,458 --> 00:01:04,499 There, let me just make a note here about what's not a good purpose. 12 00:01:04,499 --> 00:01:10,420 This is not a good purpose. Given number produce number. 13 00:01:10,420 --> 00:01:17,441 That just repeats the signature. That's not telling me something we don't 14 00:01:17,441 --> 00:01:21,810 already know. The purpose needs to tell me something 15 00:01:21,810 --> 00:01:26,120 more specific than the signature about exactly what we do with the given number 16 00:01:26,120 --> 00:01:28,250 and exactly what the number we produce means. 17 00:01:28,250 --> 00:01:32,340 So, given the length of one side of the square, it says this is the length of one 18 00:01:32,340 --> 00:01:35,460 side of the square produced the area of the square. 19 00:01:35,460 --> 00:01:37,280 This number's going to be the area of the square. 20 00:01:37,280 --> 00:01:41,100 So, that's what we want a purpose statement to do is be quite specific 21 00:01:41,100 --> 00:01:45,551 about what's going on. So, now let's see the stub is define 22 00:01:45,551 --> 00:01:51,910 area, and this is a one argument function and maybe the length of a side will be s. 23 00:01:51,910 --> 00:01:54,760 We can use any name we want, but s seems good. 24 00:01:54,760 --> 00:01:58,860 And let's see, this is producing a number, so a good dummy value to produce 25 00:01:58,860 --> 00:02:03,108 is zero. Now let's do some check-expect. 26 00:02:03,108 --> 00:02:11,590 Let's see, check-expect area 3. One side of the square of three, then the 27 00:02:11,590 --> 00:02:19,850 area is 9. Then we'll do another one check expect 28 00:02:19,850 --> 00:02:22,330 area of, let's say, 3.2. And let's see, the area of that square is 29 00:02:22,330 --> 00:02:25,790 times 3.2. 3.2., I won't try to do that in my head. 30 00:02:30,430 --> 00:02:34,670 Now we run the check expects and both ran. 31 00:02:34,670 --> 00:02:38,840 They fail, but they both ran, so that means they're well formed, we can 32 00:02:38,840 --> 00:02:48,390 continue. Now let's see, we've got the stub. 33 00:02:48,390 --> 00:02:55,560 Let's comment out the stub and label it. And we'll write the template area of a 34 00:02:55,560 --> 00:02:58,850 function s, with a parameter s that's a number. 35 00:02:58,850 --> 00:03:05,800 The template is dot, dot, dot s. And it's going to be good to have a copy 36 00:03:05,800 --> 00:03:08,580 of that template, so I'll make a copy of it right there. 37 00:03:08,580 --> 00:03:16,440 We'll comment out the original template, we'll label it as the original. 38 00:03:16,440 --> 00:03:21,630 And now, let's see. This is a function that consumes the 39 00:03:21,630 --> 00:03:32,400 side, and it just multiplies the side by itself, so this becomes times ss. 40 00:03:32,400 --> 00:03:36,660 Let's run that. Oh, we've got a failing test here, let's 41 00:03:36,660 --> 00:03:42,860 see what's happening. This says that the actual value nine 42 00:03:42,860 --> 00:03:47,430 differs from three the expected value and I can click to see which test it is. 43 00:03:47,430 --> 00:03:53,765 Let's see it said it produced nine. And oh, here's a thing that happens, this 44 00:03:53,765 --> 00:03:58,830 test is actually wrong, that's not supposed to be three, that's supposed to 45 00:03:58,830 --> 00:04:07,240 be nine. Now if we run it, both tests pass. 46 00:04:07,240 --> 00:04:09,020 So, there's two interesting things in this example. 47 00:04:09,020 --> 00:04:13,120 One is to note that you need to be careful if you get a failing text to make 48 00:04:13,120 --> 00:04:16,560 sure the test is right. If your test is wrong and you make the 49 00:04:16,560 --> 00:04:18,890 function conform to it then the function will be wrong. 50 00:04:18,890 --> 00:04:21,320 And that happens. Sometimes you write a test and you're 51 00:04:21,320 --> 00:04:25,380 just going a bit too quickly. And you put the wrong expected value. 52 00:04:25,380 --> 00:04:28,835 So when you have a failing test, first look at the test to see if the test is 53 00:04:28,835 --> 00:04:31,640 right. The second interesting thing that's 54 00:04:31,640 --> 00:04:37,110 happening in this example is to see that sometimes you have to copy part of the 55 00:04:37,110 --> 00:04:38,920 template. Remember the template for this function 56 00:04:38,920 --> 00:04:42,670 was dot, dot, dot s. And in the final function we ended up 57 00:04:42,670 --> 00:04:47,650 with s times s and so we copy the piece of the template and that's okay. 58 00:04:47,650 --> 00:04:51,950 The template is really trying to tell you, hey, here's the stuff you have to 59 00:04:51,950 --> 00:04:55,055 work with. You can use it in any way you want. 60 00:04:55,055 --> 00:04:57,700 It's just telling you, this is the parts list. 61 00:04:57,700 --> 00:05:02,398 You can use s twice. So there you go, that's our second 62 00:05:02,398 --> 00:05:08,639 function. I'll save it and run it one more time.