Mastering PyCharm Transcripts
Lecture: Concepts: Refactoring
0:01 You've seen refactoring in action in PyCharm,
0:03 and we didn't cover every single thing,
0:06 it was not an exhaustive presentation,
0:08 because it doesn't seem worthwhile to actually touch every single little detail;
0:12 for example, we didn't do super class,
0:14 I'm pretty sure you can take the ideas that we've already applied
0:17 and use them for the last few refactorings we didn't touch on.
0:21 So let's review the ones we did cover really quickly.
0:25 So my contention was, knowing when to refactor, it has to do with a few things
0:29 it's primarily to do with duplicate code, extra long code
0:33 or code that is really hard to read,
0:36 so at least for one of these, we have locate duplicates,
0:40 so we can run this and it'll actually show us
0:48 So we run it, we highlight some code,
0:51 we can run it and it will show us where stuff like that code exist,
0:53 in this case, we are doing this if not name required error exception thing
0:59 If you just run it without selecting code,
1:01 it will actually just look for duplicates anywhere in the entire file
1:05 not based on any origin, so that's really, really cool.
1:08 We saw that rename is super important,
1:11 so here we could highlight some loop variable
1:14 and say, call it this rename in action,
1:17 and it will apply across through here.
1:20 We also saw that if we do this on a method or a class,
1:23 we get a slightly different view,
1:26 but this is within a method, it will edit it right here.
1:28 We can move code, so if we want to take this part right here
1:32 and if we want to move the main method somewhere else,
1:34 we can check off the functions or the members to move at the bottom
1:38 and then do the refactor for that.
1:40 We can pull members up, so for example,
1:43 if we wanted to say we're going to change the game,
1:46 so that all creatures can attack, not just Wizards,
1:49 but one creature could fight another
1:52 and since the Wizards are creatures they can still do what they do,
1:54 but we could pull this attack method up into the Creature class
1:58 so that'd be a really nice refactoring,
2:00 but maybe we'll change the word Wizard to the name of the Creature.
2:04 Similarly, kind of in reverse, we can push members down,
2:10 so here in our base Creature class we have this string representation,
2:13 if we want that to be super different
2:15 for every type of thing that derives from Creature,
2:17 we probably need a separate method to overwrite it.
2:20 So we can put in different direction
2:23 and copy the representation method to all the things that derive from it.
2:28 We can extract a variable, we've played with this get defensive role,
2:32 we saw random.randint(1,12) maybe wasn't so obvious,
2:36 so we could call this roll value,
2:39 and that was just to highlight that bit of code
2:42 and choose extract variable, that was great.
2:44 Sometimes you want to in the other direction
2:47 you've got all these variables and it would be better
2:49 to just make a little expression,
2:51 so we can come from here and say inline
2:53 and it'll go right back into the single expression form,
2:55 so these are sort of refactorial inverses of each other, if that's a word.
2:59 We can extract not just variables, but a constant as well,
3:04 if we want to extract the roll that we have in the bottom,
3:07 maybe we want to come up with a roll once and just keep it constant
3:12 so we could do that and we could get THE_ROLL at the top,
3:14 now, granted that makes no real sense,
3:17 but I am just showing you we could highlight this bit and extract it as a constant.
3:21 We can also do extract parameters, like we have the 7 here,
3:25 remember that was our modifier we're like
3:27 it doesn't make a lot of sense that this 7 is here,
3:29 we actually want to make that configurable as well,
3:32 so we can highlight it and say extract parameter
3:35 and it'll go up, it will put it up there you can name it whatever you want,
3:38 we call it extra factor, in the example we call it a modifier,
3:41 and it uses the default value that was there before,
3:44 it uses that right here, which is pretty sweet.
3:47 Finally, we saw that we can take Python modules and convert them to packages
3:52 and we can convert Python packages into individual modules.
3:57 Here we have our actors and notice, in the editor it's actors.py
4:01 we can extract that, we can take this code,
4:06 and we can turn it into an actor's folder with a __init__.py
4:10 in the implementation of what was in actors.py
4:13 in our __init__ for our package.
4:16 In this case, it maybe doesn't make a ton of sense,
4:18 but if you're already working with packages,
4:20 it could be really nice to create a subpackage out of what is a module.