The program can be written in a thousand different ways, and it will work. But the code will have to be cleaned.
Refactoring is the reworking of the source code of a program to make it simpler and easier to understand.
Refactoring does not change program behavior, fix bugs, or add new functionality. It makes the code more understandable and readable.
For example, here’s a Python snippet that creates a list from a string:
When refactoring, it can be simplified by using a list constructor:
The result of the program’s work has not changed, but the code has become simpler, more compact and more understandable.
The sequence of such small changes can greatly improve the quality of the project.
Why is refactoring needed?
Well-structured code is easy to read and fast to modify.
Developers are usually in a hurry, requirements may change in the process of a task, testers find bugs that need to be quickly fixed, or asap improvements appear, and they have to be done in a hurry.
To solve these problems, the program is refactored. In a project, it is needed for:
- preserve the architecture of the project, prevent the loss of structure;
- simplify the future life of developers, make the code understandable and transparent for all team members;
- speed up development and search for bugs.
How is refactoring different from optimization?
Refactoring is not optimization, although it may be related to it. Often it is carried out simultaneously with optimization, so the concepts seem to be synonymous. But these processes have different goals.
Optimization aims to improve program performance, while refactoring aims to improve code comprehensibility. Once optimized, the source code can become harder to understand.
When you asap need to improve your code?
Signs that refactoring is needed:
- The program works, but even small improvements are greatly delayed due to the fact that it takes a long time to understand the code each time.
- The developer is constantly unable to say exactly how much time he needs to complete the task.
- The same changes need to be made in different places in the text of the program.
You can clean everything, but first of all you need to find these problems:
1. Dead code
A variable, parameter, method, or class is no longer used: the requirements for the program have changed, but the code has not been cleaned up. Dead code can also be found in a complex conditional construction, where a branch is never executed due to an error or a change in requirements. Such elements or sections of text must be removed.
The same code performs the same action in several places in the program. Move this part into a separate function.
3. The names of variables, functions, or classes do not convey their purpose.
Names should communicate why a code item exists, what it does, and how it is used. If there is no comment, refactor.
4. Functions and methods are too long
The optimal size of these elements is 2-3 dozen lines. If you get more, divide the function into several smaller ones and add one common one. Let the little ones perform one operation at a time, and the general function calls them.
5. The classes are too long
The optimal class length is 20-30 lines. Break the long class into several small ones and include their objects into one general class.
6. The list of parameters for a function or method is too long
They only confuse, not help. If all these parameters are really needed, move them into a separate structure or class with a friendly name, and pass a reference to it to the function.
7. Many comments
Bad code is often covered up with copious comments. If you need to clarify a piece of code, try rewriting it first. Useless comments clutter up the program, and outdated and irrelevant comments are misleading.
It is dangerous to refactor not all the time, but from time to time.
Refactor constantly and little by little.