Effective PyCharm (2021 edition) Transcripts
Chapter: Refactoring
Lecture: Refactoring class methods

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well now we've got our app startup looking a little bit nicer.
0:03 Let's go look at the various classes that are involved.
0:05 Remember we've got our creature wizard,
0:08 the small animal, the dragon and so on.
0:12 Now one of the things that we do when we run this program is we might
0:17 Show Something Like This. A Tiger of the Level 12.
0:20 And if we look around we'll see there's a total level one,
0:23 a tiger, level 12, a bat of level three and so on.
0:27 Where is that coming from? It's coming from this repr method.
0:31 So here you can see is a dragon,
0:35 such and such of level such and such.
0:38 But what we'd like to do is actually solve a problem that is very common in
0:43 code. And that's duplicate code.
0:45 So notice the small animal says it's a small animal,
0:49 such and such. Down here we have a dragon and it says basically the same
0:54 thing. This bit right here is different.
0:56 But other than that it's basically the same.
0:59 Could we fix this? Sure,
1:01 we could, we could come down here and we could just go to the type
1:04 of self, this is going to be the dragon and we can sit come down
1:10 here and go down __name like that and that's going to show exactly the same
1:14 thing, say dragon in this case.
1:17 But if that code will run inside of a small animal that would say small animal
1:20 If it were run inside a wizard,
1:22 it would say wizard. So maybe this duplicate code here is not really necessary.
1:27 Let's go highlight this and we can re factor,
1:32 what we want to do is we're gonna pull members up,
1:35 pull up means go to the base class.
1:37 We want to take this and just put it in the base class.
1:39 So everything that derives from creature wizard,
1:42 small animal, dragon and so on.
1:44 They can get that and use it.
1:47 So here I can choose what things to pull up,
1:49 right? We could do multiple methods and so on.
1:51 But this is the one we want and we could figure out where to put it
1:54 There's only one object or one type in our type hierarchy here.
1:58 So it's just going to go there to notice the reprr method is gone from
2:04 the dragon and on the creature we now have this nice generic one that we wrote
2:10 PyCharm is not going to make a big enough guess or assumption that we can just
2:14 drop it from all of them.
2:15 Right, But we know that this is going to actually be fine to take away
2:19 this repr from all of these and now we'll be able to use them just from
2:23 the base type here. Let's make sure we're using it exactly.
2:29 We go over to the program when it starts up,
2:33 let's go to a little game loop and here we'll just print rubber permanent.
2:40 I'll say hero like this. He's a rapper.
2:47 Wizard. Gandalf of Level 75.
2:49 So it works perfectly for a wizard and you can imagine it's going to work just
2:53 fine for the dragon and so on.
2:56 All right. So if we were saying like creatures of -2,
3:01 that should be the dragon. We go dragon just has the name dragon.
3:06 So I guess it's duplicate here,
3:07 But dragon dragon of level 50 were able to use this pull members up to take
3:12 this function, this method from one of the derived classes and put it into the
3:17 base class in a nice generic way that all the other objects in our type arching
3:22 you can pick it up and run with and we were able to remove that
3:25 code duplication throughout these different classes and that's a good thing.