1 00:00:00,000 --> 00:00:04,969 [MUSIC] 2 00:00:04,969 --> 00:00:06,949 Have you ever heard the old boy scout rule, 3 00:00:06,949 --> 00:00:10,100 always leave the campground cleaner than you found it? 4 00:00:10,100 --> 00:00:13,610 As software developers, we want to adopt a similar rule. 5 00:00:13,610 --> 00:00:17,820 Every time we make a change to our code or someone else's code, we want to try and 6 00:00:17,820 --> 00:00:20,320 clean it up or improve it in some way. 7 00:00:20,320 --> 00:00:23,590 We don't necessarily want to make any big changes, but 8 00:00:23,590 --> 00:00:28,580 if we see an opportunity to let say user valuable instead of a hard coded value, 9 00:00:28,580 --> 00:00:31,560 then we should make and test that change. 10 00:00:31,560 --> 00:00:33,670 The important thing to emphasize here is that, 11 00:00:33,670 --> 00:00:37,390 we don't want to change the behavior of the code, we're just looking for 12 00:00:37,390 --> 00:00:41,150 opportunities to make it a little cleaner and more efficient, or 13 00:00:41,150 --> 00:00:44,940 maybe more maintainable for someone else to work within the future. 14 00:00:44,940 --> 00:00:47,480 This process is known as refactoring. 15 00:00:47,480 --> 00:00:50,378 Let's see how we can do some refactoring with our FunFact code. 16 00:00:51,860 --> 00:00:56,035 Right now, we have all of our code in this FunFactsActivity class. 17 00:00:56,035 --> 00:00:59,420 And it works fine, but what if we wanted to add a new activity 18 00:00:59,420 --> 00:01:02,660 that also randomly selected one of our facts? 19 00:01:02,660 --> 00:01:06,670 We could copy all of these code and add it to the new activity. 20 00:01:07,740 --> 00:01:12,900 But then we would have the same code doing the same thing in two different places. 21 00:01:12,900 --> 00:01:15,340 Then if we change this code for some reason, 22 00:01:15,340 --> 00:01:19,930 we have to remember to make the same change to the code for the other activity. 23 00:01:19,930 --> 00:01:22,990 This violates a core principle of software development, 24 00:01:22,990 --> 00:01:24,430 the don't repeat yourself principle. 25 00:01:25,610 --> 00:01:30,080 A better solution is to move the code that is repeated to a reusable component. 26 00:01:30,080 --> 00:01:34,580 In this case, that could be a new object that could be used in multiple activities. 27 00:01:34,580 --> 00:01:38,960 We learned before that activities are used to control how things work on the screen. 28 00:01:38,960 --> 00:01:40,460 Our code does that, but 29 00:01:40,460 --> 00:01:44,630 it also controls how the effect is generated behind the scenes. 30 00:01:44,630 --> 00:01:48,630 Let's make a new object that separates out fact generation. 31 00:01:48,630 --> 00:01:51,582 Not only will it make our code a little cleaner, but 32 00:01:51,582 --> 00:01:55,060 it will also prevent us from repeating our ourselves in code. 33 00:01:55,060 --> 00:01:59,580 It's another good programming practice to give each class a single responsibility. 34 00:01:59,580 --> 00:02:00,330 So in that case, 35 00:02:00,330 --> 00:02:05,270 the FunFactsActivity class will handle only how the screen looks and works. 36 00:02:05,270 --> 00:02:08,560 And the new object, which we'll call factBook, will only 37 00:02:08,560 --> 00:02:13,810 handle things about the facts themselves, like how they're stored and selected. 38 00:02:13,810 --> 00:02:16,540 This is another programming principle known as the single 39 00:02:16,540 --> 00:02:17,810 responsibility principle. 40 00:02:18,870 --> 00:02:21,700 All right, let's create our new factBook object. 41 00:02:21,700 --> 00:02:24,070 There are a couple ways we can do this. 42 00:02:24,070 --> 00:02:29,710 We can go to File, New, and then pick Kotlin File or Class, 43 00:02:29,710 --> 00:02:34,620 or in the Project Pane we can right-click on our package and 44 00:02:34,620 --> 00:02:37,710 select New Kotlin File or Class. 45 00:02:37,710 --> 00:02:38,510 I'll choose this option. 46 00:02:40,460 --> 00:02:42,380 Then for the name let's type FactBook. 47 00:02:44,120 --> 00:02:49,930 And since we're making the class, let's change the kind to Class and then hit OK. 48 00:02:51,620 --> 00:02:53,229 And here's our new file. 49 00:02:53,229 --> 00:02:55,305 If we look over to the project pane, 50 00:02:55,305 --> 00:02:59,060 we can see that a new FactBook file has been added to our package. 51 00:02:59,060 --> 00:03:01,730 And our package now has two classes in it. 52 00:03:02,790 --> 00:03:05,170 Also, I already fixed it from a high version. 53 00:03:05,170 --> 00:03:09,800 But at the top of your class, Android Studio may have added a comment. 54 00:03:09,800 --> 00:03:12,340 I'm not a huge fan of the automatic comment, but 55 00:03:12,340 --> 00:03:16,210 luckily we can change our preference to make it not happen in the future. 56 00:03:16,210 --> 00:03:18,240 Follow along if you'd like. 57 00:03:18,240 --> 00:03:22,191 I'm going to click on Android Studio and then open the preferences window. 58 00:03:24,040 --> 00:03:28,640 Then I'll expand Editor and select File and Code Templates. 59 00:03:29,810 --> 00:03:34,030 And over on the Includes tab, I'll select File Header and 60 00:03:34,030 --> 00:03:37,230 then delete everything in the window on the right. 61 00:03:37,230 --> 00:03:40,990 Remember, mines empty cuz I've already done this, then click OK. 62 00:03:43,410 --> 00:03:46,880 You'll still need to delete any remaining comments by hand though. 63 00:03:46,880 --> 00:03:50,230 This class will be the blueprint for our FactBook object. 64 00:03:50,230 --> 00:03:55,123 Remember, that an object has two main components, Properties, 65 00:03:55,123 --> 00:04:00,590 which are characteristics of the object 66 00:04:02,680 --> 00:04:07,780 and Methods, which are actions the object can take. 67 00:04:08,910 --> 00:04:13,680 This object only needs to do one thing, get a random fact. 68 00:04:13,680 --> 00:04:14,720 Let's define a method for it. 69 00:04:16,040 --> 00:04:17,730 We'll call it getFact and 70 00:04:17,730 --> 00:04:20,650 it will return a string, which will be the randomly chosen fact. 71 00:04:22,110 --> 00:04:28,094 Inside the class, type fun getFact with open and closing parenthesis. 72 00:04:28,094 --> 00:04:34,890 Then let's make it return a string, colon string, and add the brackets. 73 00:04:36,140 --> 00:04:39,600 Now we just need to make this method return a random fact. 74 00:04:39,600 --> 00:04:44,190 Let's cut and paste some code from our activity class into our getFact method, 75 00:04:44,190 --> 00:04:46,290 this is the refactoring part. 76 00:04:46,290 --> 00:04:52,540 Over in FunFactsActivity, select everything in the OnClickListener 77 00:04:52,540 --> 00:04:55,820 except for the last two lines where we're actually setting the text. 78 00:04:57,210 --> 00:05:01,960 Then let's cut the text with command or Ctrl+X, and leave this error for 79 00:05:01,960 --> 00:05:06,380 now and go back to our FactBook class, and paste in the code. 80 00:05:07,790 --> 00:05:10,390 We didn't copy over any of our imports, so 81 00:05:10,390 --> 00:05:14,285 hit OK to let Android Studio automatically create the ones we need. 82 00:05:14,285 --> 00:05:18,760 GetFact now gets a random element from the facts array, but 83 00:05:18,760 --> 00:05:22,439 we still need to return which fact was selected. 84 00:05:22,439 --> 00:05:26,065 So instead of setting up a fact variable with a random fact, 85 00:05:26,065 --> 00:05:28,470 let's just return the fact variable. 86 00:05:33,100 --> 00:05:36,080 Nice, we've just made a new FactBook class and 87 00:05:36,080 --> 00:05:39,890 given it a method which will return a random fact. 88 00:05:39,890 --> 00:05:41,280 Let's take a short break and 89 00:05:41,280 --> 00:05:44,327 then we'll see how we can use your new method in FunFactsActivity.