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