Effective PyCharm Transcripts
Chapter: Refactoring
Lecture: Concepts: Refactoring

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


Talk Python's Mastodon Michael Kennedy's Mastodon