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