Basic Code Refactoring

Get more productive and less error-prone with refactorings.

In the previous step, we saw how PyCharm can run your Python code.

In this step, we are going to talk about refactoring. Ever wish your IDE could do your work for you?

Change Signature

How many times does your work involve changing the signature of a given method? As easy as it sounds, changing a method signature can have a deep impact in your code and break your program in multiple places. The change signature refactoring helps you safely change a method signature, including changing its name; adding, removing, and reordering parameters; and assigning default values to the parameters.

Let’s start by changing its signature of a method name. We will add a new parameter here. The easiest approach is to do it inline. When you do this, PyCharm already gives you a yellow light bulb and a refactoring indication in the gutter. To access it use ⌥⇧⏎ (macOS) / Shift+Alt+Enter (Windows/Linux).

Update Usages

The first option is Update usages to reflect signature change.... When you click it, PyCharm will show you the previous and current method signature, and require a default value so it won’t break our program. At this point PyCharm is using its knowledge of your project acquired during indexing to add the default value to all your current method calls.

PyCharm will update all the usages in your project, so you don't have to go hunting for other places to update. Thankfully, your code will still compile.

Rename

Renaming is another common type of refactoring. You first write your code, get everything working, and then start making names more meaningful. This is when the rename refactoring comes in handy. As with signature changes, rename can also be done in place.

Let’s just go ahead and change this method name. We can also use ⌥⏎ (macOS) / Alt+Enter (Windows/Linux) to invoke the context menu.

Rename

We'll use the first option which is Update usages to reflect signature change…. The change is reflected in the code that calls this function. PyCharm will also find usages in other code in your project. For example, we have another file that imports this function and calls it.

Extract Method

Inplace refactorings are great, but PyCharm can also help you with much more robust refactoring techniques such as ‘Extract Method’. The Extract Method refactoring lets you take a code fragment, group it into a method, and replace the old code with a method call.

To extract a method you can either use ⌥⌘M (macOS) / Ctrl+Alt+M (Windows/Linux), or the Refactor This menu which is ^T (macOS) / Ctrl+Alt+Shift+T (Windows/Linux). Let’s take this code fragment as an example. Let's select it, and invoke the ‘Refactor this’ men and choose Extract Method.

Refactor This

We can now give the new method a name and click OK. PyCharm will now create a function in the class with the name we gave and called it from the previous place.

refactored

Of course, your code will still run exactly as it did before!

Conclusion

These are three common refactoring techniques that will automate your workflow and minimize your chances of bugs. PyCharm has many more robust refactoring capabilities, so, after experimenting with these basic ones make sure to try the others.

In the next step we will see how to perform basic testing in PyCharm.

Video

You can also check out the video for this step from our Getting Started series on YouTube: