Effective PyCharm Transcripts
Chapter: Refactoring
Lecture: Renaming items
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Here we are in our program, basically in our game loop and there's a few things I don't really like about it.
0:07
One, notice up here, we're creating our creatures and then we have our player and down through here, our player is doing in attack,
0:14
and then we have information, like output from the player. But, I'd like to call this hero, so we know exactly who this is,
0:22
this is the hero of the game, not just player. So what we can do is we could go to the refactor menu or I like the hotkey, just CTRL T,
0:31
say rename, and now I could just put hero and as you type it's just going to change everything. So it works like this, when you are renaming parameters
0:42
and local variables and stuff, but let's bring in something else. Over here we have an attack method on the wizard class,
0:48
let's suppose we want to rename that, and it could be used in a hundred locations across 20 files, it wouldn't matter, it would totally work.
0:57
So same thing CTRL T, rename and let's call this fight, so the idea is the hero is going to fight the active creature.
1:04
Now, this is pretty cool, you can search in comments and strings so this would fix like docstrings
1:11
and also you could search in like within strings that are just output. So hit refactor and it pops up this little preview thing,
1:19
it says hey we're going to refactor this thing and this is where it is used, it will say if hero fight is what is going to happen
1:27
you would see as many locations, up here also we have attack is coming from the creature to rename, and here's one of its usages, so we hit do it,
1:33
It also changed here, but also, now we have a wizard.fight, if we run it, it should work just the same,
1:41
the Wizard has handily triumphed over the Bat, how exciting. Okay so the Wizard is doing good,
1:48
our code is hanging together even across these multiple files, and I want to emphasize, this is not find and replace, that's not super cool,
1:55
tech search this is abstract syntax tree understanding the entire project, that's super cool.