Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyCharm Code Refactoring

PyCharm Code Refactoring

Code refactoring is the process of optimizing a program's internal structure without changing its external behavior.

The purpose of refactoring is to improve readability, maintainability, and extensibility, not to add features or fix bugs.

PyCharm provides Python developers with a powerful and safe set of automated refactoring tools, making refactoring efficient and less error-prone.


Quick overview of common refactoring operations

Below is a reference table of the most common refactoring operations in PyCharm and their shortcuts.

Refactoring operation Keyboard shortcut Description
Rename Shift + F6 Rename symbols such as variables, functions, classes, and files, automatically updating all references
Extract Variable Ctrl + Alt + V Extract the selected expression into a variable
Extract Method Ctrl + Alt + M Extract the selected code block into a standalone method or function
Extract Constant Ctrl + Alt + C Extract the selected literal into a constant
Inline Ctrl + Alt + N Inline-expand a variable, method, or constant to remove unnecessary intermediate layers
Change Signature Ctrl + F6 Change the signature of a function or method, including parameter names, types, default values, and order

The shortcuts above are based on the default Windows/Linux keymap. macOS users should replace Ctrl with Command and Alt with Option.


Hands-on: renaming a variable

Renaming is the most frequent refactoring operation; the following example demonstrates the complete Rename workflow.

Suppose we have code that computes the speed of a moving object, where the variable name speed is not precise enough and needs to be changed to velocity.

Original code before refactoring

Example

Original code: the variable name speed is not semantically precise

def

calculate_motion

(

distance

,

time

)

:

    speed

=

distance /

time

 

speed leans toward rate, while velocity emphasizes vector speed

   

return

speed

result

=

calculate_motion

(

100

,

10

)

print

(

result

)

Steps

  1. Move the cursor onto the variable name speed.
  2. Press the shortcut Shift + F6, and PyCharm highlights all references to that variable.
  3. Type the new name velocity directly, and PyCharm previews in real time all the locations that will be changed.
  4. Press Enter to confirm, and all references are updated synchronously.

You can also double-click to select the variable, then right-click, choose Refactor from the menu, and then Rename:

Rename in the pop-up window:

Code after refactoring

Example

Code after refactoring: the variable name velocity expresses vector speed more precisely

def

calculate_motion

(

distance

,

time

)

:

    velocity

=

distance /

time

 

The variable name has been renamed from speed to velocity

   

return

velocity

result

=

calculate_motion

(

100

,

10

)

print

(

result

)

[/mycode4]

PyCharm's Rename refactoring searches for references across the entire project, including in comments and strings, ensuring nothing is missed.


Open the refactoring menu with one shortcut

If you cannot remember the shortcuts for various refactorings, use the universal shortcut Ctrl + Alt + Shift + T (Command + Option + Shift + T on macOS) to bring up the refactoring menu.

The menu lists all refactoring operations available at the current cursor position; you simply select from the menu.

This approach suits less frequently used refactorings or serves as a transition for beginners.


Preview feature before refactoring

Before performing a refactoring, PyCharm provides a Preview feature so you can fully understand its impact before confirming.

After you press the refactoring shortcut, PyCharm shows a dialog that contains a Preview button.

Click the Preview button, and PyCharm opens a comparison window at the bottom: the original code on the left and the modified code on the right.

You can review each change one by one, decide whether to keep or skip it, and then click Do Refactor to execute the refactoring after confirming everything is correct.

It is strongly recommended to use the Preview feature when refactoring large projects or core modules, to avoid bugs caused by accidental changes.


Refactoring notes

Although PyCharm's refactoring tools are very reliable, you should still pay attention to the following points when refactoring.

Master these refactoring techniques and your code will stay clean, clear, and easy to maintain.