Original English:Top 6 refactoring patterns to help you score 80% in code Quality
In the past, I did a lot of code reading and found some common problems in code quality. The following areTop five:
Bloated class:
The reason why the class is bloated is that developers lack the most basic coding principle, namely the "single Responsibility Principle" (SRP. These classes tend to become very bloated because different methods that lack Association in functions are placed in the same class.
Long Method:
The main reasons for the method to become long are as follows:
- Code of many unrelated and functional modules is put in the same method. This is mainly because developers lack the SRP concept.
- Multiple conditions are placed in the same method, which usually occurs in long methods. This is due to the lack of McCabe Code complexity and the comparison of SRP concepts.
Massive parameter passing:
I often encounter these situations. Some methods interact with other methods, or a large number of parameters are input when other methods are called. In this case, if you change one of the parameters, you have to change them in multiple methods.
Constant Value is everywhere:
It is often found that developers (especially new users) use some constants with clear meanings (mainly devil numbers), but do not assign them appropriate constant variables. This reduces code readability and comprehension.
Fuzzy Method Name:
In many cases, the following method names affect the readability and comprehension of the Code:
- Fuzzy Method Name that does not have any meaning
- Technical, but not the names of relevant fields
Six reconstruction methods (methods) to handle the odor of the code above)
The following are six to help youSolve 80% (80-20 principles) of code Quality ProblemsAnd helps you become a better developer.
- Extract class/extract method: As mentioned above, it is like a "bloated class" (a class provides functions that should have been provided by several classes) this code odor should move the methods and attributes in the original class to the new classes with appropriate numbers. The methods and attributes of the new class in the old class should be removed. In addition, sometimes some classes are too bloated because they contain Member methods that are used by other classes and should be member methods of other classes. These methods should also be migrated to appropriate classes.
- Extraction Method: Code odor like the "too long method" mentioned above can be eliminated by extracting code from the old method to one or more new methods.
- Separation condition: in many cases, a method is long because it contains several branch statements (if-else ). These branch conditions can be extracted and moved to several separate methods. This can indeed greatly improve code readability and comprehension.
- Introduce parameter objects/retain Global Objects: I found another common situation during code review-several parameters are passed in methods. The problem is mainly caused by adding or removing a method parameter from an existing method. In this scenario, it is recommended that the relevant method parameters form an object (introduce the parameter object), so that the method can pass these objects instead of each individual parameter.
- Replace magic numbers with symbolic constants: assign them a naming constant for literal constants that make sense and are used everywhere. This greatly enhances code readability and comprehension.
- Rename method: As mentioned above, ambiguous method names will affect the Code's usability. These ambiguous names should be renamed to meaningful names that may be related to business terms to help developers better understand the code through the business context. This requires skill and requires developers and business experts to work together to clarify the business needs that the code needs to meet. Interestingly, This refactoring method seems very easy to understand, but it is often overlooked by many developers, although it is often seen in the refactor menu items of the IDE such as Eclipse.
Six refactoring methods can help you improve code quality by 80%.