1 00:00:00,000 --> 00:00:00,280 2 00:00:00,280 --> 00:00:02,970 >> SPEAKER 1: Suppose now that I wanted to give Scratch the ability 3 00:00:02,970 --> 00:00:05,080 to sneeze as well. 4 00:00:05,080 --> 00:00:07,710 Well, my first instincts would be, as before, to make a 5 00:00:07,710 --> 00:00:10,070 block, call this sneeze. 6 00:00:10,070 --> 00:00:14,110 Specify that I'd like to specify with a number how many times 7 00:00:14,110 --> 00:00:15,800 Scratch should sneeze. 8 00:00:15,800 --> 00:00:17,540 Thereafter clicking OK. 9 00:00:17,540 --> 00:00:20,370 Let me move this over to the right to give myself some more room. 10 00:00:20,370 --> 00:00:26,070 >> And then let me again go to Repeat, overriding the default 10 with n. 11 00:00:26,070 --> 00:00:29,610 Let me go to the say statement and say not "hello", but 12 00:00:29,610 --> 00:00:32,350 "achoo" for one second. 13 00:00:32,350 --> 00:00:35,180 Then take a moment's respite. 14 00:00:35,180 --> 00:00:36,040 And that's it. 15 00:00:36,040 --> 00:00:39,220 >> Now I have a block called sneeze, as well as a block called cough. 16 00:00:39,220 --> 00:00:46,550 So that now if I want Scratch to cough and sneeze, I can simply specify a 17 00:00:46,550 --> 00:00:48,580 sneeze below his cough. 18 00:00:48,580 --> 00:00:49,930 Three times, say. 19 00:00:49,930 --> 00:00:55,560 Click the green flag, and I should see indeed, cough, cough, cough. 20 00:00:55,560 --> 00:01:01,880 Followed by achoo, achoo, achoo. 21 00:01:01,880 --> 00:01:04,180 My most realistic sneezes to date. 22 00:01:04,180 --> 00:01:09,240 >> Now notice something that's bad about this design. 23 00:01:09,240 --> 00:01:11,930 Even though I didn't outright duplicate any puzzle pieces, I 24 00:01:11,930 --> 00:01:15,020 definitely dragged and dropped pretty much the same puzzle pieces, giving 25 00:01:15,020 --> 00:01:18,660 the one function an ever so slightly different name than the other, sneeze 26 00:01:18,660 --> 00:01:19,640 versus cough. 27 00:01:19,640 --> 00:01:23,410 So this is an opportunity for factoring out some common code. 28 00:01:23,410 --> 00:01:27,020 Indeed, notice the only difference between cough and sneeze is that one 29 00:01:27,020 --> 00:01:30,710 has Scratch saying "cough," the other of which saying "achoo." 30 00:01:30,710 --> 00:01:35,010 >> So couldn't we somehow generalize this code in such a way that we simply 31 00:01:35,010 --> 00:01:37,990 specify what word we want to Scratch to stay? 32 00:01:37,990 --> 00:01:41,170 And depending on that word, it will effectively be a cough or 33 00:01:41,170 --> 00:01:42,370 it will be a sneeze. 34 00:01:42,370 --> 00:01:43,580 So let's go ahead and do this. 35 00:01:43,580 --> 00:01:49,500 Let's make a third function and call this one, "say." and I want it to 36 00:01:49,500 --> 00:01:53,360 accept, not just a number, but this time a string, otherwise known as a 37 00:01:53,360 --> 00:01:54,980 word or phrase. 38 00:01:54,980 --> 00:01:57,500 >> And we'll call this "word." 39 00:01:57,500 --> 00:01:59,500 And how many times do I want to say this word? 40 00:01:59,500 --> 00:02:02,680 Well let's specify a number, calling it again n. 41 00:02:02,680 --> 00:02:05,540 And just to make this block more readable, let me go ahead and add a 42 00:02:05,540 --> 00:02:11,000 label called "times." So that when you read this block from left to right, it 43 00:02:11,000 --> 00:02:14,380 literally says "say word n times." 44 00:02:14,380 --> 00:02:15,760 >> Now let me go ahead and click OK. 45 00:02:15,760 --> 00:02:18,320 46 00:02:18,320 --> 00:02:22,750 And now let me implement this block, again using a repeat block. 47 00:02:22,750 --> 00:02:25,680 Specifying that I want it to repeat n times. 48 00:02:25,680 --> 00:02:29,480 Specifying that I want to say something for one second. 49 00:02:29,480 --> 00:02:33,750 But rather than something hard coded into this function, let's instead drag 50 00:02:33,750 --> 00:02:36,450 and drop the word that's been passed in. 51 00:02:36,450 --> 00:02:38,790 Let's then wait for one second. 59 00:02:39,297 --> 00:02:43,387 So now let me go back and refine my implementations of cough and sneeze, 60 00:02:43,387 --> 00:02:47,367 in such a way that those two functions call this third function, thereby 61 00:02:47,367 --> 00:02:49,627 minimizing the amount of duplicate code. 62 00:02:49,627 --> 00:02:52,237 So let's literally throw this code away. 63 00:02:52,237 --> 00:02:54,037 Let's throw this code away. 64 00:02:54,037 --> 00:02:57,707 And this time, let's simply specify that cough should be implemented by 65 00:02:57,707 --> 00:03:02,607 saying "cough" some number n of times. 66 00:03:02,607 --> 00:03:06,987 >> And similarly, can we go back and re-implement sneeze so that it uses 67 00:03:06,987 --> 00:03:13,577 say with an argument of "achoo" n times. 68 00:03:13,577 --> 00:03:14,687 So what have we done? 69 00:03:14,687 --> 00:03:18,417 We've recognized the code that was common to both our cough and our 70 00:03:18,417 --> 00:03:22,127 sneeze functions, factored it out into a new third function, and then 71 00:03:22,127 --> 00:03:25,377 rewritten cough and sneeze in such a way that they call that 72 00:03:25,377 --> 00:03:27,177 third and new function. 73 00:03:27,177 --> 00:03:30,297 >> Now we have absolutely made this whole program more complicated than it 74 00:03:30,297 --> 00:03:31,377 really needs to be. 75 00:03:31,377 --> 00:03:34,307 After all, at the end of the day, we just need Scratch to cough three times 76 00:03:34,307 --> 00:03:35,587 and sneeze three times. 77 00:03:35,587 --> 00:03:38,007 And we could have done that with just a few puzzle pieces. 78 00:03:38,007 --> 00:03:41,197 But as our programs get much more complicated, and our programs more 79 00:03:41,197 --> 00:03:45,257 sophisticated, this technique of factoring out common code, or 80 00:03:45,257 --> 00:03:49,007 hierarchical decomposing your program into smaller function, each of which 81 00:03:49,007 --> 00:03:51,846 call other functions, is a very compelling technique. 82 00:03:51,846 --> 00:03:55,627 Because it will ensure that your code, even as it gets more complex, remains 83 00:03:55,627 --> 00:03:58,087 easy to update and also very readable. 84 00:03:58,087 --> 00:03:59,627 >> Indeed, consider the end result. 85 00:03:59,627 --> 00:04:03,917 When the green flag is clicked, cough three times, sneeze three times. 86 00:04:03,917 --> 00:04:05,007 And that's it. 87 00:04:05,007 --> 00:04:07,007 In fact, let's do exactly that. 88 00:04:07,007 --> 00:04:12,187 Click the green flag and we get one cough, two coughs, three coughs. 89 00:04:12,187 --> 00:04:18,607 Followed by, we hope, once sneeze, two sneezes, and a third sneeze. 90 00:04:18,607 --> 00:04:19,857 Hope he gets better soon. 91 00:04:19,857 --> 00:04:20,892