Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Cleaner Code Through Refactoring6:40 with Craig Dennis
Create a reusable function to remove calculations
This is looking great and Monty Python thinks so, too. 0:00 I showed a demo to the group and 0:03 they realized that they forgot to include a requirement. 0:05 They forgot that there's a service charge involved with each transaction. 0:08 You can't sell tickets without a service charge, right? 0:11 Now this works a little differently. 0:15 Each purchase, not each ticket, has a service charge of $2. 0:17 Every time you demo your software, users will request additional features. 0:22 It happens all the time. 0:25 This is why it's important to get working software in front of your stakeholders. 0:27 This is actually a good chance to take a look at our code and 0:31 see if we can't refactor it a bit to make it more easy to read. 0:34 Well, there's a term we haven't touched on yet, refactor. 0:38 Refactoring is when you take a look at your code and you improve it for 0:41 readability or extensibility without changing how the program actually works. 0:44 Let's see if we can't refactor that price calculation into a function and 0:48 then add this new service charge. 0:52 Okay, so I'm gonna go ahead. 0:54 And I'm gonna add a new card. 0:55 And this one is As an owner, I should receive, 0:57 A service charge so that I can pay 1:05 others to maintain the software. 1:10 I suppose that makes sense, right? 1:15 Maintaining software can be super difficult for clients. 1:17 Now, if there's an error, they'll need to pay developers somehow. 1:19 So, I hope some of that service charge makes its way to fellow developers, 1:23 cuz it might not be us that fixes it. 1:28 You might pick up an application that's already working and you need to fix it. 1:29 So, let's move this into In Progress. 1:33 Okay, so, I'm gonna get rid of these comments here, get rid of that one, and 1:36 that one, and that one. 1:41 Okay, looking good, all right. 1:44 So let's first refactor our calculation into a function, right? 1:48 Cuz currently, we are calculating. 1:54 Where are we doing that calculation? 1:56 Right here, num_tickets equals times TICKET_PRICE. 1:58 So let's go ahead, I'm gonna cut this out. 2:01 This is Cmd+X, or Ctrl+X. 2:04 So now it's in my clipboard, it's gone. 2:06 And I'm gonna add a function that we'll create here in a bit. 2:08 And it should calculate the price of how many tickets there are. 2:12 So sounds like a good name, calculate_price. 2:15 And we're gonna pass in the number of tickets, 2:20 which we know is a valid number at this point. 2:24 Go ahead and save that. 2:28 And I'm gonna up here to the top, and here we go, let's do this. 2:29 Create the calculate price function. 2:33 Let's use the proper name there, calculate_price function. 2:37 It takes Number of tickets and 2:44 returns, what do we have here? 2:49 num_tickets + TICKET_PRICE, let's make a new. 2:52 Cool, so create that function, and return that value. 2:57 All right, you got this. 3:02 Pause me and create that function. 3:03 Remember, it needs to take the number of tickets. 3:05 All right, so here's what I did. 3:09 So I defined calculate_price. 3:10 And I required a parameter of number of tickets. 3:13 I need a colon, open that body up, and there is no need to create a new variable. 3:17 We can actually just return the result, right? 3:22 So we're gonna return and let's get lazy. 3:24 This, paste this here, but note that this is number of tickets. 3:28 So I'm gonna say number_of_tickets. 3:33 Now, note how this refactoring puts this calculating price into a separate area 3:38 than where this loop is at there. 3:43 So this loop will never really need to change. 3:45 We can figure out what this calculation of the price is. 3:48 And other people could use it too, should they need to. 3:51 We didn't need to do this but we refactored and 3:54 things should still work exactly the same. 3:57 Let's go ahead and let's run it and make sure. 3:59 Hey, Bob, let's get 2 tickets. 4:03 We got 20. 4:05 Yes, I wanna proceed. 4:05 98 tickets left, awesome, perfect. 4:07 So now that we have it refactored, 4:09 what we should do is we need to add this service charge, right? 4:13 So I'm just gonna go ahead, I'll put it in here. 4:17 And we need to create a new constant for 4:22 the $2 service charge. 4:26 Remember, that's once per transaction. 4:31 And then we want to add the service charge to what's due. 4:35 Okay, you got this. 4:44 Pause me and give those a go. 4:45 You ready? 4:47 Okay, so here's how I did it. 4:49 So this service charge, I'm gonna go ahead, I'm gonna come up here. 4:51 I'm gonna make a new constant. 4:53 And I'm gonna put it at the top of the file. 4:57 If you ever look in here, the service charge, if we start charging too much for 4:59 our developers, we need to bump this price up. 5:02 We just bump it one place here. 5:05 And then, I used it in the calculate_price function. 5:07 So I'm gonna get rid of this comment here, bring this back up. 5:11 We can just say + SERVICE_CHARGE. 5:16 You know what, I'm gonna think about my dear Aunt Sally. 5:21 And I'm gonna use some parenthesis even though I don't need to. 5:25 Because I know that multiplication will happen first and not the addition. 5:29 But I'm gonna do that cuz I think that that makes things more clear. 5:33 Let's go ahead and see how we did. 5:37 I would like to have 2 tickets. 5:42 $22, because of that service charge. 5:44 And there we go, great job. 5:48 Now I do like how if they change the way that this works, 5:51 we know where to change things. 5:55 It's right up here at the calculate_price. 5:56 And when you look at it used in this loop here, it's pretty clean, right? 5:58 It's really clear that the price calculation is happening elsewhere, and 6:03 we don't need to worry about it here. 6:06 If you wanted to calculate this on a different page, 6:08 on like a shopping cart page, you could use that same function. 6:10 It's reusable, we change it in one place. 6:13 And you know what? 6:15 I think we're done. 6:17 Awesome job. 6:19 I want you to take a minute and breathe in this program. 6:21 Look at all the tools that you stitched together. 6:25 You really have learned a ton and you were able to build an entire application. 6:29 You did an excellent job at immersing yourself in the Python 6:34 programming language. 6:36 Excellent work. 6:38
You need to sign up for Treehouse in order to download course files.Sign up