- Hands-On Enterprise Automation with Python.
- Bassem Aly
- 284字
- 2021-06-18 19:22:25
Code refactoring
Refactoring the code is the process of changing the structure of a specific variable name inside your code. For example, you may choose a name for your variable and use it for a project that consists of multiple source files, then later decide to rename the variable to something more descriptive. PyCharm provides many refactoring techniques, to make sure that the code can be updated without breaking the operation.
PyCharm does the following:
- The refactoring itself
- Scans every file inside the project and makes sure that the references to the variables are updated
- If something can't be updated automatically, it will give you a warning and open a menu, so you can decide what to do
- Saves the code before refactoring it, so you can revert it later
Let's look at an example. Assume that we have three Python files in our project, called refactor_1.py, refactor_2.py, and refactor_3.py. The first file contains important_funtion(x), which is also used in both refactor_2.py and refactor_3.py.

Copy the following code in a refactor_1.py file:
def important_function(x):
print(x)
Copy the following code in a refactor_2.py file:
from refactor_1 import important_function
important_function(2)
Copy the following code in a refactor_3.py file:
from refactor_1 import important_function
important_function(10)
To perform the refactoring, you need to right-click on the method itself, select Refactor | Rename, and enter the new name for the method:

Notice that a window opens at the bottom of the IDE, listing all references of this function, the current value for each one, and which file will be affected after the refactoring:

If you choose Do Refactor, all of the references will be updated with the new name, and your code will not be broken.
- 數據庫系統原理及MySQL應用教程(第2版)
- Go Web編程
- AngularJS Testing Cookbook
- Unity 5 for Android Essentials
- 小學生C++創意編程(視頻教學版)
- Java系統化項目開發教程
- Scala Reactive Programming
- Natural Language Processing with Java and LingPipe Cookbook
- 新一代SDN:VMware NSX 網絡原理與實踐
- Qlik Sense? Cookbook
- 零基礎學HTML+CSS
- Distributed Computing in Java 9
- Java EE Web應用開發基礎
- 愛上C語言:C KISS
- 深入解析Java編譯器:源碼剖析與實例詳解