Effective PyCharm Transcripts
Chapter: Refactoring
Lecture: Refactoring methods and functions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's look at some code we haven't touched yet. So, here is a little wizard project and it has a couple of classes.
0:08 Let's go ahead and compress this down a little bit you can see. So we've got a wizard a little higher. We have a creature object a class.
0:18 Some behaviors you can create one of them and it can do this. Get defensive role is going to be a little role playing dungeons and dragon type game
0:24 And then there are other more specialized things in here. Like there's a wizard that derives from creature,
0:30 there's a small animal, there's a dragon and so on. Now let's just run it real quick so you can see what it does.
0:38 So it has this cool little header here that looks like a dragon. The wizard battle app. And you can see it's in the smoky breath or the
0:45 fire of the dragon. And turner says a level a tiger of level 12 has appeared from a dark and foggy forests. And you have some options here.
0:53 We could look around, we could attack it. We're pretty strong so we can attack it and look the Wizard gained attacks the tiger
0:59 We rolled 900. The tiger rolls 108. The wizard has handily triumphed over the tiger. All right, So this is what the program does.
1:07 We can also look around and we can run away and we can quit. So what we want to do.
1:14 Not so much play the game is we want to look around and see where are some code smells. There are some things that we might be able to make better
1:21 And let's get started at a real simple thing over here, this is the start up of the whole program. We do this big print statement here,
1:29 notice here's the dragon text, A huge welcome to the dragon game on the wizard game with the big fiery breath writing set up here.
1:39 It's a bunch of text and we're just printing that out and then we'd run the game. Well this this formatting just right in line in this main method.
1:48 It's not beautiful, is it? It would be nice if we could just real quickly read what's happening main like,
1:53 oh, we're gonna show the header that we're going to run the game loop, but that's not what's happening here.
1:57 We've got to figure out what does this print mean, Why is it all here? And then we run the game loop.
2:02 So the first thing we want to do maybe is to make this its own function with its own name. So instead of saying, we'll show the header as a comment.
2:14 Remember this is a bit of deodorant for this. Not so great code for this code smell.
2:20 What would be better is to just make it more clear with some kind of refactoring and that brings us to the refactoring features.
2:27 So there's a couple of ways to get to them. I almost always use this first one 'Ctrl+t' on mac Os. And it's something else on the others.
2:34 You will see in a moment what that is and what I would like to do is just go highlight the code and say, well what can I do to make this better?
2:42 But you could be more specific. Like if we were on a variable, we could rename it or a function,
2:47 we could change the signature, we could introduce a variable, a field, a parameter object.
2:53 Remember I talked about the function that takes too many arguments. This is one of the proposed fixes. We create a method and so on.
3:01 We can also not with what I have selected, but in general we could reverse that.
3:05 We get in line a variable in line of function and just make it happen right
3:09 there. I said this is this parallel refactoring and the anti re factoring and both times they're useful and they're both useful but at different times.
3:18 Right? So you want to keep in mind what we're doing here? All right. So what we want to do is we want to instead of just
3:24 do this, we want to have a function that has a name that kind of tells us what's happening. So I come down here and get ctrl+t or
3:30 You can see I can get control shift t on windows or Linux. I'm going to go down here and have a bunch of options. I know I want to extract a method,
3:38 you can see there's a hotkey for it. Option command M for me, but what I can actually do is I could just
3:44 type start typing method until it gets to be a nice short list. You can actually search or narrow down this list. We have that, we get this thing here,
3:52 it says show header or something like that. And if we choose it had arguments or if it were using variables that need to
4:00 be passed through as an argument, it would allow us to put them here and change the order and stuff.
4:05 But this doesn't have any. So we'll just say go and look at that. It means the same thing. If we run, it's going to do the same thing.
4:13 See down here is the wizard just like before the wizard text being shown. But when you read it now def main we'll show the header,
4:22 show header. Do you think we need this deodorant here anymore. Is it smart to leave this comment?
4:27 No, because it doesn't make sense to have it when the code is so simple and it just so clean. It just says show header.
4:34 Alright, this is silly. So we'll take it away.
4:36 That's one of those cases where the code was acting as deodorant. That's how we can invoke different refactoring tools.

Talk Python's Mastodon Michael Kennedy's Mastodon