Understand how to refactor code to make it more readable and easier to maintain using the built-in refactoring tools in VS Code.
- [Instructor] Alright, now let's take a look at how to refactor Python code in Visual Studio. Refactoring is the process of restructuring code to make it easier to read and maintain without actually changing the runtime behavior of the program. So VS Code with the Python extension installed provides support for common code refactoring operations, such as extracting methods and variables and renaming symbols. So let's look at an example. Here in VS Code I'll open up my Geometry Python code and suppose I wanted to rename my geometry solver class to something else. So rather than do a search and replace on the name I can select it and then right click and then choose Rename Symbol. Now when I do this, I get a little editor and I can name this, how 'about we call it geometry calculator. Now in this particular case you can see I'm getting a little bit of an error. It says, "Rename Failed to Execute" and it says, "Refactoring Library Rope is not installed." Should I install it? So I'm going to install the Rope Library, which is what handles refactoring. So I click on Yes and then VS Code will go out and get the library for me. You can see that it was successful. So Rope is now installed. So I'll go ahead and close that and I'll kill this editor. So now that I've got the Rope library installed, I'll try this again. So I'll choose Rename Symbol and I'll call it geometry calculator. And you can see that now it's been renamed and if we scroll down a little bit, you can see that wherever that symbol was used, it's now been renamed to geometry calculator. I can also extract individual lines of code to create new variables and methods. So for example, let's go back up to my geometry calculator class. So this line of code right here returns the area of a circle calculation right in line with the return statement. So to make that a little bit more readable and maybe help with debugging, I can create a new temporary variable by selecting the code and then right clicking and choosing Extract Variable. I got to save it, okay, so we'll do that. And I'll make a new variable called ans. That's fine, it going to be called the answer and you can see that when I hit Okay, the expression is extracted and made into a new variable. Now you might see this little pop-up here that says, "Cannot perform refactoring using selected elements." There's a known bug in VS Code right now that when you do refactorings with the Rope library that this little error might appear, but as you can see it works. So you can go ahead and safely ignore that for now. And that is a known issue, so it is being worked on. So, let's go ahead and Save. Alright, let's do one other example. I'm going to extract some code into a new method and to do that you use the Extract Method command. So let's scroll down in our code and we'll come to the main loop here. I don't really need to have all of these print statements here in the main function printing out the menu each time. I can improve the readability of my code by separating all of these statements into a new separate method. So let's go ahead and highlight these statements and then I'll right click and I'll choose Extract Method and then I'll get a pop-up like I did earlier and I'll just call this print underscore menu. And you can see now that all of those lines of code have been replaced by this function call to print menu and, well, there's that little useless error again. So I'll get rid of that. And if I scroll down, you'll see that now those lines of code have been extracted into a new function. Refactoring is a fairly common task in software development as programs get larger and more complex. With VS Code, you can perform this operation more efficiently and with fewer errors.
- Constructing a simple Python program in VS Code
- Editing, formatting, and refactoring Python code
- Debugging Python code
- Setting breakpoints
- Using Python docstring
- Creating better comments for your source code
- Using the REST Client extension