Effective PyCharm Transcripts
Chapter: Refactoring
Lecture: Concept: Refactorings

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's do a quick flyover of all the main concepts around re factoring and PyCharm we've
0:05 covered and actually introduce one or two more that we didn't take the time to do as a demo. So remember we have the re factoring menu,
0:13 we can get to all sorts of good things. I would say extract and introduces probably where you're gonna be most as well as rename
0:19 But there is the more specialized ones like pull members up, push members down and so on.
0:24 The way that I typically get to refactoring is using the refactor this the first thing in this menu which on macOS is ctrl+t.
0:31 So highlight the code or put the cursor on the thing you want to rename. Refactor or whatever. Press 'ctrl+t' or your OS is hotkey.
0:40 And then after the races, the first thing that we looked at was renaming.
0:45 Remember having a good name is super important and it can help avoid using comments as
0:50 deodorant for code smells. So good name often tells you what's happening in the code without the need for comments here.
0:56 Maybe this was just for CN creatures, we want to make it more obvious.
1:00 So for creature in creatures, you notice we're renaming in the for loop location and
1:06 it's changing throughout this whole file where the creature or see what's being used before, if we wanted to change the way the game played.
1:14 So not just wizard could attack, but like a toad could emerge from the forest and surprise attack us.
1:19 We'd want to use the attack functionality built into wizard, but make it available to all creatures.
1:24 So we could use pull members up over here to attack, say pull members up and say we'd like to push this up to the creatures so
1:31 all creatures have the ability to attack, not just wizards, we wanted to reverse that, we had some functionality that was in creature,
1:39 but we want a specialised version in each class, we can push members down and we can check it off like that and then every
1:46 drive class will get its own copy, which we can then specialize. I love the extract variable feature,
1:53 it helps with debugging, it helps with understanding and so on. We talked about this, get defensive role, we said, you know, this is not great,
2:00 it's not entirely clear what's happening here. So we could extract this out as a variable and then we'll let us see it
2:07 in the debugger what we did is broken into multiple variables, so we could actually see each little piece but still this will help us in our
2:13 debugging if we decided, you know, that's not the way we wanted to go. Actually, there's no real reason for this variable here,
2:20 let's just return that expression itself. We can do the reverse of extract very but we can in line a variable and now we have just this like,
2:28 clean one line feature. Another thing that's really cool is we can extract a constant constant in Python, it's not a real language way to enforce it,
2:38 but the convention is that they're all caps. So if we go over here and we try to extract this 12,
2:45 it's a constant. It's going to put it at the module level and then we can give it a name like the dye sides.
2:50 Like how many sides does my dye have this one, it's a 12 sided one. This will let us easily change this one.
2:56 Constant that will apply to the rest of the program, highlight the 12. We? Refactor and say create extract the constant and go
3:03 over here and then it's going to replace the usage of the 12 wherever we were using it with the constant. We also saw that we might want to have some
3:12 kind of constant become configurable. That could be passed into a method. Something that's not a variable at the moment,
3:18 but on an argument, what could be? So in the for example, we have the seven. That was our modifier. We can go over here and do an extract parameter.
3:26 It will create a modifier parameter, give it the default value. So by default it behaves the same.
3:31 But now it's configurable. This is really handy way to add more extensive bility and
3:37 more flexibility to your code. Here's one that we did not cover if we're going
3:42 to create a Python package, there's a certain structure that it has to have.
3:47 There's a directory name. That's the name of the package And in there there's a
3:51 '__init.py' file and potentially others what we've been working with so far
3:56 is just a couple of files that run together and they're technically known as modules. So notice we have this actors.py,
4:03 we can re factor, convert to a Python package, watch what's going to happen here,
4:08 This actors right there and all the code in it is going to get changed. Actors are going to become a directory.
4:15 There's going to be a file called __init.py. And the implementation that was in that module is now moved to the __init.py
4:23 By default, we've got this new structure, which it's not usually required, but if you're working with packages and you need to
4:31 install things or especially if you want to ship this over two PyPI or
4:34 share it with other programs, having it as a package might be exactly what you need. It's the quick way to do inPyCharm.

Talk Python's Mastodon Michael Kennedy's Mastodon