Multiple common Code Design defects 0 Preface
In software design and development, code design is embodied in the relationship between subsystems and subsystems, modules and modules, functions and functions. The worse the design of software, the higher the maintenance cost, quality is often difficult to reach the standard and praise.
A good design must be: simple, clear, easy to maintain and expand the hierarchical relationship.
I will not study too advanced designs, but will summarize some common code design defects. These design defects can be well solved and avoided, and I believe in the Code capabilities (coding, design, review, and refactoring) it can be upgraded to a higher level.
This section describes the following 15 common code design defects:
1 Complex Functions (BLOB Operation)
Defect features:It refers to a function with many code lines, deep nested branches, many variables, many parameters, many annotations, and high complexity features.
Impact of defects:Functions are hard to understand and maintain, and code is repetitive and redundant.
Solution:When new code is developed, the more complex the function is to be written, the more conscious and active the function should be decomposed into small functions or independent functions, even when you feel that you need to annotate something, you should actually form a function independently. Recommended function value: the code line 24, the nested depth of the IF language 6, the lap complexity 10, and the function should be single.
2 Data clumps)
Defect features:The function has many parameters and the parameter list is similar. The same parameter list is called repeatedly.
Impact of defects:A large number of duplicates affect compilation efficiency. Many parameters are difficult to understand and call.
Solution:The parameter list should be encapsulated into a structure. Recommended value: the average function parameter value is 2 to avoid more than 5.
Sample pseudo code:Getdate (INT year, int month, int day, int time)-> getdate (struct daterange ).
3 Unnecessary Coupling)
Defect features:Contains a header file, but does not use any content in the header file.
Impact of defects:The compilation link is slow, coupled with a high degree of coupling, and the header file contains errors. For example, if a header file is not used, a header file depends on a DLL, this will cause unnecessary DLL dependencies and errors.
Solution:The header file cannot be contained in disorder. 100% check the usage of each contained header file and delete unnecessary header files.
4 Intensiue Coupling)
Defect features:A function calls a large number of functions in other modules, but calls a small number of functions in this module.
Impact of defects:A function is too closely related to multiple functions (these functions belong to one or two classes). A class provides many functions for external function calls. The coupling is high and the class is not abstract enough.
Solution:Identifies internal and external module functions. External modules must be abstract enough to call functions.
5 Cyclic Dependencies)
Defect features:Multiple subsystems are in a ring-shaped dependency. Function calling is chaotic and cyclical. Files are directly or indirectly referenced.
Impact of defects:Difficult to understand and maintain, slow compilation, chaotic relationships, and difficult to reuse.
Solution:Multiple files or systems should be clearly divided into structures and hierarchies, so as to achieve no ring dependency.
Sample pseudo code:Loop contains the header file. File a contains file B, and file B contains file.
6 Feature envy)
Defect features:Functions seldom access the data of their own modules, and always access the data of external modules. There are few access modules and many access modules. Data and operations are not in the same module. You are more interested in other types of data.
Impact of defects:High coupling.
Solution:Data and operations of the same module should be put together.
7 Repeat code)
Defect features:Different modules or files have similar or repeated functions. Different classes have similar or repeated functions. The child classes of the same parent class have similar or repeated functions.
Impact of defects:Code expansion is messy and difficult to maintain. Originally, a code must be maintained in multiple places due to repeated code.
Solution:Duplicate code extraction. For example, tool functions are encapsulated into tool classes, and general functions are encapsulated into public libraries.
8 Unstable dependency)
Defect features:A sub-system or module depends on another sub-system or module that is less stable than it. For example, if the upper-layer module depends on the lower-layer module, the upper-layer module will definitely suffer from problems.
Impact of defects:Not independent and unstable.
Solution:When dependencies exist, ensure the stability of the dependent subsystems or modules. At least ensure that unstable subsystems depend on stable subsystems.
9 Underutiliaed Interface)
Defect features:Many interfaces are designed and implemented, most of which are not used or only used internally. Many global variables are defined, and most other modules are not used.
Impact of defects:Redundancy, over-design, and visualization exposure.
Solution:Design interfaces as needed. Variables and functions that do not need to be made public should be private.
10 Schizophrenic class)
Defect features:A class implements multiple different functions. For example, the interface class processes business-related functions.
Impact of defects:Difficult to understand, high coupling, too many public methods.
Solution:Split multiple features.
11 Complex class)
Defect features:Classes with a very large scale and high complexity often contain multiple complex functions and have multiple functions.
Impact of defects:High circle complexity, poor cohesion, high coupling, difficult to understand and maintain.
Solution:To solve complex functions, the structure should be clear and the class functions should be single. Recommended value: the number of classes should be less than 2000.
12 God class)
Defect features:A class combines the functions of multiple irrelevant classes. A Class operates too much data on other modules, which is large and complex.
Impact of defects:This damages the encapsulation of classes, high coupling, poor cohesion, and difficulty in maintenance.
Solution:Classes unrelated to multiple functions should be encapsulated into different classes, and the function should be removed as appropriate to solve the problem of complex functions.
13 Distorted hierarchy)
Defect features:Class has a deep inheritance relationship.
Impact of defects:High complexity and difficulty in maintenance.
Solution:The hierarchy of classes should not exceed 6.
14 Data class)
Defect features:Many public attributes and functions are provided for many other classes, but they are rarely operated by themselves.
Impact of defects:It is not object-oriented and lacks encapsulation and is not easy to maintain.
15 Tradition Breaker)
Defect features:The derived class does not use any function to inherit the parent class, but adds a new function.
Impact of defects:It is difficult to understand and maintain non-inherited relationships.
Solution:Clarify the inheritance relationship between classes. classes that are not suitable for the inheritance relationship should be separated separately.
16 2 related books are recommended