Effective PyCharm Transcripts
Chapter: Refactoring
Lecture: Rename refactorings

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One of the most important things that you can focus on is giving the various symbols in your code good names. Why? Because if they have good names,
0:11 those can be the descriptor for what they actually do. Remember up here we said show header,
0:17 we have this silly comments said we want to show this this series of print statement
0:20 shows the header the name is so communicative that we don't actually have to have some kind of comment. It just says show header game.
0:29 Well done. I know this is just going to go around and around while the game plays. I don't have to go look at it right?
0:34 The name is good. But sometimes you write some code then you decide, you know, a better name has shown up or it's got more complicated.
0:43 It has to be more specific. For example, notice if we click on player at highlights all the use cases of player like there's player dot name,
0:50 there's player dot attack, there's a player, this is you. But I want to really emphasize that this is the hero of the game. So I could go over here,
0:59 right, click go and say refactor rename or go to the menu up here and re factor rename. But again,
1:06 what I really do is I just hit ctrl+t figure out what I want. You can see the really common ones right at the top under rename.
1:13 I'm gonna give this a new name. I'm gonna call it hero. Now watch as I type where it's used below all the places that it's being used,
1:23 it's getting updated. Right And again, remember this is not fancy, find and replace this is understand the abstract syntax tree
1:31 and actually make those changes where this is happening. And we also want to do that for this constant up here. Let's go and rename this notice.
1:38 I don't have to go to the definition. I can go to a use case here and say rename again. There's a little bit bigger model wide change.
1:46 So we're gonna get this dialogue here and then it's going to do it. All right, cool. So now our hero does the things that you know,
1:55 was the player right? A little more obvious like All right. This is not just the player, but the hero. Another thing I noticed this attack method.
2:02 We go over to the actors. You can see the wizard class has this attack and let's imagine that I wanted to not say attack but fight. Okay,
2:14 we can come over here again. We could do it at the definition or at a use case and say I want to rename this from attack to fight.
2:23 Now there's a few more options here. I can search in comments and strings. This is cool because if I have like a doc string or a comment that talks
2:31 about the attack method, it's also going to rename that So my comments don't get
2:36 broken. You also just full on search and straight text and you can also choose the different areas that you might want to apply those changes.
2:45 Okay, So I'm going to say re factor and it says, okay, we want to make sure that you know what's happening here. So you're going to re factor this,
2:54 but it's actually being used over here. All right. So, if you make a change, if you rename attack, we're gonna have to change the code here.
3:02 Now, this is a pretty simple example, But imagine this is being used in 50 different places across multiple files.
3:09 Notice that we did the renaming game core. It's changing that in the actors and then fixing up game core where it uses the
3:17 wizard dot whatever attacks going to be. All right, it looks good to me. So, we're gonna go ahead and click do we factor and notice hero now fights
3:26 It doesn't attack it fights and over here also. Right, let's just run it one more time to make sure it's still working.
3:34 Love our little character there. Let's have it well, attack. So, the wizard gained off attacks. The dragon, you the hero role 1 1350.
3:46 The dragon slightly has rolled a higher number there, 67,500. We've been defeated and we had to go hide but there's now a toad
3:54 we could kill. So we'll do that. Wonderful. So you can see that we can use the re factoring tools again,
4:01 not just on one file, across as many files and places as that codes used on real projects. This will be many,
4:08 many places. And you might forget it's a huge problem. You could make a change to say I'm gonna replace everywhere.
4:14 I see attack with fight. And if there's somewhere you forget, not that code is broken with PyCharm.
4:19 It just understands the whole project and it'll keep that stuff all consistent and give you a little preview even before you hit go.
4:25 So you know what you're in for.


Talk Python's Mastodon Michael Kennedy's Mastodon