Effective PyCharm Transcripts
Chapter: Refactoring
Lecture: Moving code

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now let's go through our code again here, we've got our little main, it's going to print the headers and the game loop
0:08 and now the real meat of this thing is the game loop. So let's suppose that for some reason we would like to take this method
0:16 and put it into a separate module called like game core or something like that. So we can highlight this over here, and we'll say refactor move,
0:30 this is great, we're going to move it from program.py and the Wizard
0:34 to another module— what do you want to call it, let's call it game core, like this.
0:40 And if we hit go, it's going to say we will create some dunder inits for you, it's treating this as if this was creating like a subpackage,
0:53 we're not going to do that, so we can get rid of these, because they're not part of this, so packages make sense.
1:00 But notice over here in game core, we now have game loop and we're creating our wizard, like so,
1:06 it's trying to do this, but I don't think that's going to work because over here we're importing game loop
1:14 and this is going to create an infinite loop, so it should have just really taken this with it. Okay, I think everything is good,
1:22 It's not quite pep 8— there we go, so if we look at our program down here, we should have this and game loop— where does game loop come from now,
1:33 it comes from game core, right, so now we've got just this one dedicated module for working with the game loop, and I think I kind of like that,
1:40 let's run it again make sure everything hangs together. Awesome, a Tiger of level 12 has appeared, let's attack it.
1:48 We've handily triumphed over it, how about the Frog, there's a Bat, let's fight that— ah yes, we've triumphed over it,
1:58 that Dragon, it's a little more tricky, oh we have been defeated, the Wizard must rest.
2:05 Okay, so it looks like our refactoring, all of this refractoring we've done, is truly a refactoring, in that we've changed our code,
2:13 it has quite a bit of different structure than when I started this chapter, and yet, from the outside, it's indistinguishable
2:19 in how it behaves and it runs and so on. That's awesome, that's refactoring.


Talk Python's Mastodon Michael Kennedy's Mastodon