Simple Refactoring: Creating a New Class5:45 with Ben Deitch
There is a famous Boy Scout rule that states: "Always leave the campground cleaner than you found it." As software developers we want to adopt a similar rule, except we're dealing with code, not campgrounds. In programming, this process is known as "refactoring," and we'll refactor our code using some handy tools built into the IDE.
Things to Consider
[MUSIC] 0:00 Have you ever heard the old boy scout rule, 0:04 always leave the campground cleaner than you found it? 0:06 As software developers, we want to adopt a similar rule. 0:10 Every time we make a change to our code or someone else's code, we want to try and 0:13 clean it up or improve it in some way. 0:17 We don't necessarily want to make any big changes, but 0:20 if we see an opportunity to let say user valuable instead of a hard coded value, 0:23 then we should make and test that change. 0:28 The important thing to emphasize here is that, 0:31 we don't want to change the behavior of the code, we're just looking for 0:33 opportunities to make it a little cleaner and more efficient, or 0:37 maybe more maintainable for someone else to work within the future. 0:41 This process is known as refactoring. 0:44 Let's see how we can do some refactoring with our FunFact code. 0:47 Right now, we have all of our code in this FunFactsActivity class. 0:51 And it works fine, but what if we wanted to add a new activity 0:56 that also randomly selected one of our facts? 0:59 We could copy all of these code and add it to the new activity. 1:02 But then we would have the same code doing the same thing in two different places. 1:07 Then if we change this code for some reason, 1:12 we have to remember to make the same change to the code for the other activity. 1:15 This violates a core principle of software development, 1:19 the don't repeat yourself principle. 1:22 A better solution is to move the code that is repeated to a reusable component. 1:25 In this case, that could be a new object that could be used in multiple activities. 1:30 We learned before that activities are used to control how things work on the screen. 1:34 Our code does that, but 1:38 it also controls how the effect is generated behind the scenes. 1:40 Let's make a new object that separates out fact generation. 1:44 Not only will it make our code a little cleaner, but 1:48 it will also prevent us from repeating our ourselves in code. 1:51 It's another good programming practice to give each class a single responsibility. 1:55 So in that case, 1:59 the FunFactsActivity class will handle only how the screen looks and works. 2:00 And the new object, which we'll call factBook, will only 2:05 handle things about the facts themselves, like how they're stored and selected. 2:08 This is another programming principle known as the single 2:13 responsibility principle. 2:16 All right, let's create our new factBook object. 2:18 There are a couple ways we can do this. 2:21 We can go to File, New, and then pick Kotlin File or Class, 2:24 or in the Project Pane we can right-click on our package and 2:29 select New Kotlin File or Class. 2:34 I'll choose this option. 2:37 Then for the name let's type FactBook. 2:40 And since we're making the class, let's change the kind to Class and then hit OK. 2:44 And here's our new file. 2:51 If we look over to the project pane, 2:53 we can see that a new FactBook file has been added to our package. 2:55 And our package now has two classes in it. 2:59 Also, I already fixed it from a high version. 3:02 But at the top of your class, Android Studio may have added a comment. 3:05 I'm not a huge fan of the automatic comment, but 3:09 luckily we can change our preference to make it not happen in the future. 3:12 Follow along if you'd like. 3:16 I'm going to click on Android Studio and then open the preferences window. 3:18 Then I'll expand Editor and select File and Code Templates. 3:24 And over on the Includes tab, I'll select File Header and 3:29 then delete everything in the window on the right. 3:34 Remember, mines empty cuz I've already done this, then click OK. 3:37 You'll still need to delete any remaining comments by hand though. 3:43 This class will be the blueprint for our FactBook object. 3:46 Remember, that an object has two main components, Properties, 3:50 which are characteristics of the object 3:55 and Methods, which are actions the object can take. 4:02 This object only needs to do one thing, get a random fact. 4:08 Let's define a method for it. 4:13 We'll call it getFact and 4:16 it will return a string, which will be the randomly chosen fact. 4:17 Inside the class, type fun getFact with open and closing parenthesis. 4:22 Then let's make it return a string, colon string, and add the brackets. 4:28 Now we just need to make this method return a random fact. 4:36 Let's cut and paste some code from our activity class into our getFact method, 4:39 this is the refactoring part. 4:44 Over in FunFactsActivity, select everything in the OnClickListener 4:46 except for the last two lines where we're actually setting the text. 4:52 Then let's cut the text with command or Ctrl+X, and leave this error for 4:57 now and go back to our FactBook class, and paste in the code. 5:01 We didn't copy over any of our imports, so 5:07 hit OK to let Android Studio automatically create the ones we need. 5:10 GetFact now gets a random element from the facts array, but 5:14 we still need to return which fact was selected. 5:18 So instead of setting up a fact variable with a random fact, 5:22 let's just return the fact variable. 5:26 Nice, we've just made a new FactBook class and 5:33 given it a method which will return a random fact. 5:36 Let's take a short break and 5:39 then we'll see how we can use your new method in FunFactsActivity. 5:41
You need to sign up for Treehouse in order to download course files.Sign up