Visual Studio Code for Python Developers Transcripts
Chapter: Editing Python Code with VS Code
Lecture: Refactoring Code with VS Code

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's take a look at how VS Code and the Python extension helps us to refactor our projects and our Python code.
0:08 First up, we're going to take a look at renaming variables and modules and what that does for us.
0:13 So in this case, we have this variable in this function for this API route, show_id, and we want to rename that. Maybe it's not really a show_id,
0:22 it's like show_time_id or you want to capitalize the ""i"" let's say. So what you can do is a couple of ways you can go about renaming this.
0:29 You can right-click on it and click on the rename symbol or as you can see from here in the menu, there's the F2 keyboard shortcut.
0:37 I click on that and you'll see this new little pop-up come in here and let's change it to where it's capital ""i"" and then ""d"".
0:43 One thing I want to call out before I hit enter to rename it, as you can see here,
0:47 there's different keyboard commands. If I do shift enter, it shows me a preview of what's going to change, which is really nice.
0:53 And then you can choose to apply or discard. Right now I'm going to discard it. We'll do this again.
0:58 Show_id, capital ""i"" ""d"" and take note of everywhere else inside of this function where show_id is being used.
1:06 VS Code will update this for us. So if I hit enter, all those have now updated with the capital ""i"" ""d"".
1:13 So that's one of the ways that Visual Studio Code through the Python extension and PyLens are able to create that quick experience,
1:21 quick way of editing and renaming a variable.
1:23 You can do the same thing for modules too. So let's take a look at this logging module that we're importing.
1:28 I'm going to put this over to the side because I want you to be able to see the changes from this.
1:35 All right. In the same way that we're able to rename variables in our file and it affects that variable that's being used throughout the whole file,
1:44 we can do the same with modules. So if I want to rename the logger module here, I press F2 on that and I type it to call it log.
1:52 And you'll notice on the right hand side, I have the logging py file that defines that module for me. And let's see what changes.
1:58 You'll see now that while I changed the name in this file, it also updated the module itself to use that log file instead of logger naming convention.
2:10 That's one of the cool things about VS Code is if even though I'm working in one file, it will update all the other places that that name change would affect.
2:18 Moving on from here, let's talk about extracting methods and variables in a function.
2:23 I'm going to close out that and let's fix this really quick because that's bothering me. So let's go back to the show ID.
2:30 So let's rename that back to show ID. So let's try and extract a variable.
2:34 In this case, we're logging information that the show ID that we're retrieving is the one that's being executed right now for when a request comes into this API.
2:44 But instead of having maybe I reuse this multiple times in here and I wouldn't like to have to retype this every time I want to extract this out to a variable.
2:52 So I can select that and I can go back to the show ID. And I can see that I've got a variable that I want to extract. So I can go back to the show ID.
2:58 You'll notice that the little bubble like a bulb icon rather shows up here.
3:03 I can click on that and I can say, hey, I want to extract the variable from this. And it'll prompt you to give a name to that variable that you'd want.
3:11 So let's say message. And now I can use that variable again anywhere else. In this case, I'm only using it once, but you get the idea.
3:19 And you can even use that string interpolation capabilities built into that as well. So that's extracting a variable.
3:26 But what about extracting bubbles? I'm going to use a little bit of code here. I'm going to use a little bit of code here.
3:32 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
3:38 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
3:44 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
3:50 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
3:56 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
4:02 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
4:08 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
4:14 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
4:20 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
4:26 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
4:32 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
4:38 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
4:44 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
4:50 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
4:56 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
5:02 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
5:08 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
5:14 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
5:20 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
5:26 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
5:32 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
5:38 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
5:44 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
5:50 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
5:56 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
6:02 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
6:08 I'm going to use a little bit of code here. I'm going to use a little bit of code here. I'm going to use a little bit of code here.
6:14 I'm going to use a little bit of code here. I'm going to use a little bit of code here.


Talk Python's Mastodon Michael Kennedy's Mastodon