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