Simple Refactoring: Creating a New Class5:54 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 always leave the campground cleaner than 0:04 you found it? 0:08 As software developers we want to adopt this similar rule. 0:09 Every time we make a change to our code or someone else's code we want to try and 0:12 clean it up or improve it in some way. 0:16 We don't necessarily want to make any big changes. 0:19 But if we see an opportunity to, let's say use a variable, 0:21 instead of a hard-coded value, then we should make and test that change. 0:25 The important thing to emphasize here, 0:29 is that we don't want to change the behavior of the code. 0:31 We're just looking for opportunities to make it a little cleaner and 0:34 maybe more efficient. 0:37 Or maybe more maintainable for someone else to work with in the future. 0:38 This process is known as refactoring. 0:42 Let's see how we can do some refactoring with our fun fact code. 0:44 Right now, we have all of our code in this fun facts activity class. 0:48 And it works fine. 0:53 But what if we want to add a new activity that 0:55 also randomly selected one of our fun facts? 0:57 We could copy all of this code and add it to the new activity. 1:00 But then we would have the same code doing the same thing in two different places. 1:04 Then if we change this code for 1:09 some reason we'd have to remember to make this same change in the other activity. 1:11 This violates a core principle of software development. 1:16 The don't repeat yourself principle. 1:19 A better solution is to move the code that is repeated to a reusable component. 1:21 In this case, that can be a new object that can be used in multiple activities. 1:26 We learned before that activities are used to control how things work on the screen. 1:30 Our code does that, but 1:35 it also controls how the factors generated behind the scenes. 1:36 Let's make an object that separates out fact to generation. 1:40 Not only will it make our code a little cleaner, but 1:44 it will also prevent us from repeating ourselves in code. 1:47 It's another good programming practice to give each class a single responsibility. 1:51 So in our case, 1:55 the fun facts activity class will handle only how the screen looks and works. 1:56 And then new object, which we'll call fact book, will only 2:01 handle things about the facts themselves like how they are stored and selected. 2:04 This is another programming principle known as the single 2:09 responsibility principle. 2:11 All right, let me open up the project pane and then create our new fact book object. 2:13 There are a couple ways to do this. 2:18 We can go File, New and then pick Java Class, or 2:20 we can right click on our package and select new Java class. 2:29 I'll choose this option. 2:35 Then for the name, let's type FactBook. 2:36 And since we're making a normal class, we can leave this as is and click OK. 2:41 And here's our new file. 2:47 If we look over to the project pane, 2:49 we can see that a new FactBook file has been added to our package. 2:51 And our package now has two classes in it. 2:54 Also, at the top of our class, Android Studio has added a comment for us. 2:57 I'm not a big fan of this automatic comment, but luckily, 3:02 we can change a preference to make this not happen in the future. 3:05 Follow along if you'd like. 3:08 I'm going to click on Android Studio and then open the Preferences window. 3:10 Then expand Editor and select File and 3:17 Code Templates and over on the Includes tab select File Header. 3:22 And then delete everything in the window on the right, and hit OK. 3:28 We still need to delete this comment by hand though. 3:35 This class will be the blueprint for our FactBook object. 3:44 Remember that an object has two main components. 3:48 Fields or Member Variables which are properties about the object, 3:52 as well as Methods which are the actions the object can take. 4:00 This object only needs to do one thing, get a random fact. 4:10 Let's define a method for it. 4:15 We'll call it get fact, and 4:16 it will return a string, which will be the randomly chosen fact. 4:18 Right after our methods comment, let's type public String getFact, 4:21 with open and closing parenthesis, an open curly bracket, and hit Enter. 4:28 Now we just need to make this method return a random fact. 4:34 Let's cut and paste some code from our activity class into our getFact method. 4:37 This is the refactoring part. 4:42 Over in FunFactsActivity, and let me hide the project pane, 4:44 select everything here in the onClick method, except for 4:50 the last two lines where we're actually setting the text. 4:54 Since setting the text is part of how the screen looks, 4:59 it should stay as a responsibility of our activity class. 5:02 Then let's cut the text with Cmd or Ctrl+X. 5:06 Let's leave this error for now and go back to our FactBook class and 5:10 paste in the code. 5:14 We didn't copy over any of our imports. 5:16 So hit OK to let Android Studio automatically create the ones we need. 5:19 GetFact now gets a random element from the facts array. 5:25 But we still need to return which fact was selected. 5:29 So instead of setting up a fact variable with a random fact, 5:32 let's just return the random fact. 5:36 Nice, we've just made a new FactBook class and 5:42 given it a method which will return a random fact. 5:45 Let's take a short break, and then we'll see how to use our method and 5:49 fun facts activity. 5:53
You need to sign up for Treehouse in order to download course files.Sign up