Software code complexity measurement method and device, equipment and medium

By decomposing software code complexity into business logic and technical implementation components, the method improves the accuracy and reference value of code complexity assessment, addressing the limitations of existing simplistic measurement methods and aiding in maintaining code quality.

CN119645812APending Publication Date: 2025-03-18INDUSTRIAL AND COMMERCIAL BANK OF CHINA
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411832291.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-12
Publication Date
2025-03-18

AI Technical Summary

Technical Problem

Existing software code complexity measurement methods are overly simplistic, relying solely on code lines, leading to low reference value and inability to effectively manage and control code complexity, resulting in decreased code quality.

Method used

A method that decomposes software code complexity into business logic complexity and technical implementation complexity, using code scanning, syntax analysis, and weight-based calculations to provide a comprehensive complexity assessment.

Benefits of technology

Enhances the accuracy and reference value of code complexity measurement, enabling developers and engineers to maintain code complexity within a healthy range and guiding microservice architecture.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119645812A_ABST
    Figure CN119645812A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a software code complexity measurement method and device, equipment and a medium, and relates to the field of distributed technologies. The method comprises the following steps: performing code scanning processing and grammar analysis processing on a source code to obtain business logic data; determining a business logic branch tree; determining code logic branch density of each method corresponding to one class; determining code logic branch density of each class; determining the business logic complexity of the source code; determining hierarchical design data, class design data and method design data according to the source code; determining the technical implementation complexity of the source code; and determining the overall complexity of the source code according to the business logic complexity and the technology implementation complexity. According to the method provided by the invention, the overall complexity of the source code is quantified better, a reference basis is provided for controlling and managing the complexity of the engineering code, and the complexity of the software code is kept in a healthy and controllable range. The technical problem of low reference of the measurement result is solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of distributed technology, and in particular to a method, device, equipment and medium for measuring software code complexity. Background Art

[0002] Excessive code complexity in a software system will lead to decreased code readability, increased maintenance costs, and increased defect rates, and the overall code quality of the software system will decline. Therefore, detecting the complexity of the software system and controlling the complexity of the software system within a certain controllable range is one of the important means to ensure the quality of the software system code.

[0003] In the prior art, the simplest method for measuring software code complexity is to determine the complexity based on the number of software code lines. The more code lines there are, the higher the complexity of the software code is, and the more likely the software is to have vulnerabilities.

[0004] The inventors have discovered that there are at least the following technical problems in the prior art: the measurement results of software code complexity are too single, resulting in low reference value of the measurement results, and unable to help developers and engineering maintenance personnel control and manage the complexity of engineering codes, so as to keep the engineering complexity within a healthy and controllable range. Summary of the invention

[0005] The present application provides a method, apparatus, device and medium for measuring software code complexity, which are used to solve the technical problem that the measurement results of software code complexity are too single, resulting in low reference value of the measurement results.

[0006] In a first aspect, the present application provides a method for measuring software code complexity, comprising:

[0007] Get the source code;

[0008] Perform code scanning and syntax analysis on the source code to obtain business logic data;

[0009] Determine a business logic branch tree according to the business logic data;

[0010] Determine the code logic branch density of each method corresponding to a class according to the business logic branch tree;

[0011] Determine the code logic branch density of each class based on the code logic branch density of all methods corresponding to each class;

[0012] Determine the business logic complexity of the source code based on the code logic branch density of all classes;

[0013] Determining hierarchical design data, class design data, and method design data according to the source code;

[0014] Determining the technical implementation complexity of the source code according to the hierarchical design data, the class design data, the method design data and a preset weight ratio;

[0015] Determine the overall complexity of the source code based on the business logic complexity and technical implementation complexity

[0016] In a second aspect, the present application provides a software code complexity measurement device, comprising:

[0017] Get module, used to get source code;

[0018] A data processing module is used to perform code scanning and syntax analysis on the source code to obtain business logic data;

[0019] The measurement module is further used to determine business logic branch tree data according to the business logic data;

[0020] The measurement module is further used to determine the code logic branch density of each method corresponding to a class according to the business logic branch tree data;

[0021] The measurement module is further used to determine the code logic branch density of each class according to the code logic branch density of all methods corresponding to each class;

[0022] The measurement module is further used to determine the business logic complexity of the source code according to the code logic branch density of all classes;

[0023] The data processing module is further used to determine hierarchical design data, class design data and method design data according to the source code;

[0024] The metric module is further used to determine the technical implementation complexity of the source code according to the hierarchical design data, the class design data, the method design data and the preset weight ratio;

[0025] The measurement module is also used in the overall complexity determination module to determine the overall complexity of the source code according to the business logic complexity and the technical implementation complexity.

[0026] In a third aspect, the present application provides an electronic device, comprising: a processor, and a memory communicatively connected to the processor;

[0027] The memory stores computer-executable instructions;

[0028] The processor executes the computer-executable instructions stored in the memory to implement the method described in the first aspect.

[0029] In a fourth aspect, the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are executed by a processor, they are used to implement the method described in the first aspect.

[0030] In a fifth aspect, the present application provides a computer program product, comprising a computer program, which implements the method described in the first aspect when executed by a processor.

[0031] The present application provides a method, device, equipment and medium for measuring software code complexity, wherein the method better quantifies the overall complexity of the source code by splitting the overall complexity of the source code into business logic complexity and technical implementation complexity, providing a reference basis for developers and engineering maintenance personnel to control and manage the complexity of engineering codes, so that the complexity of software codes can be kept within a healthy and controllable range. The method solves the technical problem that the measurement results of software code complexity are too single, resulting in low reference value of the measurement results. BRIEF DESCRIPTION OF THE DRAWINGS

[0032] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0033] Figure 1 A schematic diagram of an application scenario of the method for measuring software code complexity provided in an embodiment of the present application.

[0034] Figure 2 A flowchart of a method for measuring software code complexity provided in an embodiment of the present application.

[0035] Figure 3 A schematic diagram of a business logic branch tree provided for an embodiment of the present application.

[0036] Figure 4 A schematic diagram of the overall structure of a device for measuring software code complexity provided in an embodiment of the present application.

[0037] Figure 5 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application.

[0038] The above drawings have shown clear embodiments of the present application, which will be described in more detail later. These drawings and text descriptions are not intended to limit the scope of the present application in any way, but to illustrate the concept of the present application to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION

[0039] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.

[0040] It should be noted that the software code complexity measurement method, device, equipment and medium provided in the present application can be used in the field of distributed technology, and can also be used in any field other than the field of distributed technology. The application field of the software code complexity measurement method, device, equipment and medium in the present application is not limited.

[0041] It should be noted that the method, device, equipment and medium for measuring the complexity of software code of the present application can be used in the field of financial technology, and can also be used in any field other than the field of financial technology. The application field of the method, device, equipment and medium for measuring the complexity of software code of the present application is not limited.

[0042] In some possible implementations, in addition to evaluating software code complexity based on the number of lines of code, the software code complexity of a program can also be calculated based on the number of operators and operands in the executable lines of code in the program. The more operators and operands there are, the more complex the program structure is. However, the inventors have found that the above two methods of determining software code complexity are both process-oriented, and only analyze from the perspective of program execution logic, without measuring complexity from the perspective of code engineering. The measurement results of software code complexity are too single, resulting in low reference value, and cannot help developers and engineering maintenance personnel control and manage the complexity of engineering codes.

[0043] In response to the above technical problems, the inventors have proposed the following technical ideas: measure the complexity of the engineering source code, and then combine the existing known code characteristics to calculate the corresponding software code complexity from the two perspectives of business logic and technical implementation to obtain the business logic complexity and technical implementation complexity. Finally, the overall software code complexity is calculated according to the business logic complexity and technical implementation complexity according to the weight. In this way, by quantifying the complexity of the software code from two perspectives, it helps developers and engineering maintenance personnel control the complexity of the management code, and also provides certain guidance for the splitting of microservices in the microservice architecture, so as to solve the technical problem that the measurement results of the software code complexity are too single, resulting in low reference value of the measurement results.

[0044] The technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.

[0045] Figure 1 A schematic diagram of an application scenario of the method for measuring software code complexity provided in an embodiment of the present application.

[0046] like Figure 1 As shown, the application scenario includes: a terminal 101 and a server 102 .

[0047] The terminal 101 is used for the staff to view the measurement results of the software code complexity and select the software code to be measured according to the business requirements. The server 102 is used to receive the software code transmitted from the cloud or other servers, and measure the software code complexity of the received software code to obtain the measurement results.

[0048] Figure 2 A flowchart of a method for measuring software code complexity provided in an embodiment of the present application.

[0049] The execution subject of this embodiment can be Figure 1 The server 102 in the illustrated embodiment may also be other computer-related devices having the same functions, and this embodiment does not impose any particular limitation on this.

[0050] like Figure 2 As shown, the method for measuring software code complexity provided in the embodiment of the present application includes:

[0051] S201: Obtain source code.

[0052] In this embodiment, the source code may be the code of a Java project based on the Spring framework (an open source Java / Java EE full-stack application framework), or program code of other object-oriented programming languages.

[0053] S202: Perform code scanning and syntax analysis on the source code to obtain business logic data.

[0054] In this embodiment, the code scanning process and the syntax analysis process may be a process of aggregating data related to the business logic in the source code by using a static code processing method.

[0055] Specifically, in an optional embodiment of the present application, code scanning and syntax analysis are performed on the source code to obtain business logic data, including:

[0056] S202a: Analyze the operators and control flow statements in the source code by static code scanning and syntax analysis to perform code scanning and syntax analysis to obtain business logic data.

[0057] In this embodiment, the method of static code scanning and syntax analysis is to statically scan and analyze operators such as ternary operators and control flow statements such as if-else statements, switch statements and loop statements in the code to obtain business logic data such as the branch logic density of the program.

[0058] In this embodiment, by obtaining business logic data related to business logic from the perspective of operators and control flow statements, the business logic data required to obtain the business logic complexity is further refined to improve the accuracy of the final measurement result of the business logic complexity.

[0059] S203: Determine business logic branch tree data according to the business logic data.

[0060] In this embodiment, determining the business logic branch tree data may be a process of applying a drawing tool to draw the business logic data into a business logic branch tree.

[0061] Specifically, in an optional embodiment of the present application, the business logic data includes business step data and business step correspondence data; accordingly, step S203 specifically includes:

[0062] S203a: Determine the node information of each business step and the upper and lower hierarchical relationship of each business step according to the business step data and the business step corresponding relationship data.

[0063] S203b: Draw a logical tree with a tree structure according to the node information of each business step and the upper and lower hierarchical relationship of each business step, and determine the logical tree as a business logic branch tree.

[0064] In this embodiment, the refinement process of the business logic branch tree in the above steps utilizes the upper and lower layer relationship of each business step to obtain a business logic branch tree that is more conducive to measuring the complexity of business logic.

[0065] Figure 3 A schematic diagram of a business logic branch tree provided for an embodiment of the present application.

[0066] like Figure 3As shown, in this embodiment, each step corresponds to a node, and the relationship between the steps is the upper and lower layer relationship of the business steps. The business step data can be each step displayed in the source code, and the business step correspondence data can be the correspondence between the steps. For example: Step 2 is a sub-step of Step 1. Step 4 is a sub-step of Step 3.

[0067] S204: Determine the code logic branch density of each method corresponding to a class according to the business logic branch tree data.

[0068] In this embodiment, taking the Java program as an example, the class in the source code is the basic building block of the Java program, which is used to create objects. The class encapsulates the properties and behaviors of the object. The method is a function in the class, which is used to perform specific operations or calculations. The code logic branch density of each method in a class can be calculated according to the tree structure and constituent elements of the business logic branch tree.

[0069] Specifically, in an optional embodiment of the present application, the business logic branch tree structure includes the number of nodes and the number of branches of each method corresponding to each class; accordingly, step S204 includes:

[0070] S204a: Determine the method code logic branch density of each method according to the number of nodes and the number of branches of each method.

[0071] Based on the above embodiment, in an optional embodiment of the present application, the calculation method of step S204a is:

[0072] The method code logic branch density is the sum of the number of nodes and the number of branches.

[0073] like Figure 3 As shown, in this embodiment, the number of nodes can be the total number of steps in the method, such as Figure 3 The number of nodes in is 12, and the number of branches refers to the number of edges in the business logic branch tree, such as Figure 3 The number of branches in is 17. Therefore, Figure 3 The method code logic branch density shown = 17 + 12 = 29.

[0074] S205: Determine the code logic branch density of each class according to the code logic branch density of all methods corresponding to each class.

[0075] In this embodiment, the code logic branch density of all methods corresponding to each class can be calculated by a calculation formula or an operation rule to obtain the code logic branch density of each class.

[0076] Specifically, in an optional embodiment of the present application, step S205 includes:

[0077] S205a: Perform a summation operation on the code logic branch densities of all methods corresponding to each class to obtain the code logic branch density corresponding to each class.

[0078] Based on the above embodiments, in an optional embodiment of the present application, the summation operation refers to accumulating and summing the code logic branch densities of all methods corresponding to each class one by one, and obtaining the final result as the code logic branch density corresponding to each class.

[0079] S206: Determine the business logic complexity of the source code according to the code logic branch density of all classes.

[0080] In this embodiment, the software code of a Java program based on the Spring framework is taken as an example. Due to the characteristics of the Spring framework, the main business logic in the Spring Java project is encapsulated as a Spring Bean class. Therefore, the code logic branch density of all classes in the source code is summed to obtain the business logic complexity.

[0081] Specifically, in an optional embodiment of the present application, step S206 includes:

[0082] S206a: Perform a sum operation based on the branch density of all class codes to determine the business logic complexity of the source code.

[0083] Based on the above embodiment, in an optional embodiment of the present application, the calculation formula of step S206a is:

[0084]

[0085] Where S1 is the business logic complexity of the source code, s i is the code logic branch density of the i-th class, N is the number of classes in the source code, where i is an integer between 1 and N, and N is an integer greater than 1 or equal to 1.

[0086] S207: Determine hierarchical design data, class design data, and method design data according to the source code.

[0087] In the embodiment, the hierarchical design data, class design data and method design data correspond to the analysis angles of the entire code project respectively, so as to complete the measurement of the complexity of the technical implementation through a "top-down" and layer-by-layer measurement method. Among them, the "top-down" method is based on the source code, and analyzes layer by layer from the top hierarchical design angle to the next layer class design angle and finally to the bottom layer method design to determine the hierarchical design data, class design data and method design data in the source code.

[0088] Based on the above embodiment, in an optional embodiment of the present application, step S207 includes:

[0089] S207a: Determine the number of layers, the number of dependencies between layers, and the total number of code lines from the perspective of layered design of the entire source code according to the source code, and determine the number of layers, the number of dependencies between layers, and the total number of code lines as layered design data.

[0090] S207b: Determine the number of classes, the inheritance depth between classes, the number of dependencies between classes and the average number of lines of code for a class from the perspective of class design of the entire source code according to the source code, and determine the number of classes, the inheritance depth between classes, the number of dependencies between classes and the average number of lines of code for a class as hierarchical design data.

[0091] S207c: Determine the number of methods, the number of calls between methods, and the average number of lines of code for methods from the perspective of method design of the entire source code according to the source code, and determine the number of methods, the number of calls between methods, and the average number of lines of code for methods as method design data.

[0092] In this embodiment, it is assumed that the source code is the software code of a Java project based on the Spring framework. This is the same as the starting point of the business logic complexity. Since the main business logic in the project is included in the Spring Bean class, only the Spring Bean class in the project can be considered in the process of measuring the complexity of technical implementation.

[0093] S208: Determine the technical implementation complexity of the source code according to the hierarchical design data, the class design data, the method design data and the preset weight ratio.

[0094] In this embodiment, the technical implementation complexity can be calculated by the "weighted average" method for the technical implementation complexity of the entire source code. However, due to the different design difficulties and importance, the weights of hierarchical design, class design and method design are divided into different values.

[0095] In an optional embodiment of the present application, the preset weight ratio is a ratio between the preset hierarchical weight, the preset class weight and the preset method weight; accordingly, step S208 includes:

[0096] S208a: Determine the layered design complexity according to the number of layers, the number of dependencies between layers, the total number of code lines, and the preset layer weights.

[0097] In this embodiment, the number of layers can be the number of layers decomposed according to program functions in the layered design. The dependency relationship between layers can be the frequency and degree of mutual dependence between different layers. The value of the complexity of the layered design can be determined by calculation.

[0098] S208b: Determine the class design complexity according to the number of classes, the inheritance depth between classes, the number of dependencies between classes, the average number of code lines of a class, and the preset class weight.

[0099] In this embodiment, the number of classes can be the total number of classes defined in the source code, such as the number N of Spring Bean classes described in the above embodiment. The inheritance depth between classes can be the maximum length from a class (node) to a base class (root node) from the node perspective in the inheritance depth tree. In object-oriented programming, inheritance is an important means of code reuse. Through inheritance, a subclass can inherit the properties and methods of the parent class and expand on this basis. The inheritance depth reflects the hierarchy and complexity between classes. For example: Taking a distributed management system as an example, suppose we have a base class "Headquarters", and then there are two derived classes "Division 1" and "Division 2", both of which inherit the "Headquarters" class. If we add a derived class "Division 22" to the "Division 2" class, then the inheritance depth at this time is 3 (from "Division 22" to "Headquarters").

[0100] In this embodiment, the number of dependencies between classes can be the degree of mutual dependence between classes. In object-oriented programming, the dependency relationship between classes is established by using instances or methods of other classes. The more the number of dependencies, the higher the degree of coupling between classes, and the greater the mutual influence between them.

[0101] S208c: Determine the method design complexity according to the number of methods, the number of calls between methods, the average number of lines of code of the method, and the preset method weight.

[0102] In this embodiment, the number of methods may be the number of functions used in the source code, and the number of calls between methods may be the frequency and degree of mutual calls between different functions, which is used to reflect the logical structure and process complexity of the source code.

[0103] S208d: Determine the technical implementation complexity of the source code based on the layered design complexity, class design complexity, and method design complexity.

[0104] Based on the above embodiment, in an optional embodiment of the present application, the preset layer weight is a, the preset class weight is b, and the preset method weight is c, where a+b+c=1; accordingly, the calculation method of step S208a is:

[0105] The complexity of layered design is a times the sum of the number of layers, the number of dependencies between layers, and the total number of lines of code.

[0106] The calculation method of step S208b is:

[0107] The class design complexity is b times the sum of the number of classes, the inheritance depth between classes, the number of dependencies between classes, and the average number of lines of code in a class.

[0108] The calculation method of step S208c is: the method design complexity is c times the sum of the number of class methods, the number of calls between class methods, and the average number of lines of code of the method.

[0109] The calculation method of step S208d is: the technical implementation complexity of the source code is the sum of the layered design complexity, the class design complexity and the method design complexity.

[0110] For example, in this embodiment, a is 0.5, b is 0.4, and c is 0.1. The final technical implementation complexity of the source code is 0.5×(number of layers + number of dependencies between layers + total number of lines of code) + 0.4×(number of classes + inheritance depth between classes, number of dependencies between classes + average number of lines of code for classes) + 0.3×(number of class methods + number of calls between class methods + average number of lines of code for methods).

[0111] S209: Determine the overall complexity of the source code based on the business logic complexity and the technical implementation complexity.

[0112] In this embodiment, after obtaining the business logic complexity and the technical implementation complexity, the overall complexity of the source code can be determined according to a preset overall complexity calculation rule.

[0113] Based on the above embodiment, in an optional embodiment of the present application, the calculation method of step S209 is:

[0114] The overall complexity of the source code is the sum of the business logic complexity and the technical implementation complexity.

[0115] In an optional embodiment of the present application, taking the overall complexity of the source code of a Java project based on the Spring framework as an example, where a=0.5, b=0.4, c=0.1, the overall complexity of the source code of the Java project = S1 + the technical implementation complexity of the source code = +0.5×(number of layers + number of dependencies between layers + total number of lines of code)+ 0.4×(number of classes + inheritance depth between classes, number of dependencies between classes + average number of lines of code for a class)+ 0.3×(number of class methods + number of calls between class methods + average number of lines of code for methods). Finally, the overall complexity of the source code of the Java project is output, and the measurement of software code complexity can be completed.

[0116] In summary, a method for measuring the complexity of software code provided in an embodiment of the present application first obtains business logic data by performing code scanning and syntax analysis on the acquired source code, and then determines the business logic complexity of the source code based on the business logic data, and obtains the technical implementation complexity based on the hierarchical design data, class design data, and method design data, and finally determines the overall complexity of the source code based on the business logic complexity and the technical implementation complexity. Therefore, the present application better quantifies the overall complexity of the source code by splitting the overall complexity of the source code into business logic complexity and technical implementation complexity, and provides a reference basis for developers and engineering maintenance personnel to control and manage the complexity of engineering codes, so that the complexity of software code is kept within a healthy and controllable range. It solves the technical problem that the measurement results of software code complexity are too single, resulting in low reference value of the measurement results.

[0117] At the same time, by quantitatively measuring the complexity of software code, it provides certain guidance for the splitting of microservices in the microservice architecture.

[0118] At the same time, by adjusting the preset hierarchical weights, preset class weights and preset method weights according to the design difficulty and importance of hierarchical design, class design and method design in different source codes, the measurement results of the complexity of technical implementation are made more accurate, thereby improving the reference value of the measurement results of the overall complexity of the source code.

[0119] Figure 4 A schematic diagram of the overall structure of a device for measuring software code complexity provided in an embodiment of the present application.

[0120] like Figure 4 As shown, the device for measuring software code complexity includes: an acquisition module 41, a data processing module 42 and a measurement module 43.

[0121] The acquisition module 41 is used to acquire source code.

[0122] The data processing module 42 is used to perform code scanning and syntax analysis on the source code to obtain business logic data.

[0123] The measurement module 43 is also used to determine the business logic branch tree data according to the business logic data.

[0124] The measurement module 43 is also used to determine the code logic branch density of each method corresponding to a class based on the business logic branch tree data.

[0125] The measurement module 434 is further used to determine the code logic branch density of each class according to the code logic branch density of all methods corresponding to each class.

[0126] The measurement module 43 is also used to determine the business logic complexity of the source code according to the code logic branch density of all classes.

[0127] The data processing module 42 is also used to determine the hierarchical design data, class design data and method design data according to the source code.

[0128] The measurement module 43 is also used to determine the technical implementation complexity of the source code according to the hierarchical design data, the class design data, the method design data and the preset weight ratio.

[0129] The measurement module 43 is also used in the overall complexity determination module to determine the overall complexity of the source code according to the business logic complexity and the technical implementation complexity.

[0130] In an optional embodiment of the present application, the data processing module 42 is specifically used to analyze the operators and control flow statements in the source code through static code scanning and syntax analysis to perform code scanning and syntax analysis to obtain business logic data.

[0131] In an optional embodiment of the present application, the business logic data includes business step data and business step correspondence data; accordingly, the measurement module 43 is specifically used to: determine the node information of each business step and the upper and lower hierarchical relationship of each business step according to the business step data and the business step correspondence data; draw a tree-structured logic tree according to the node information of each business step and the upper and lower hierarchical relationship of each business step, and determine the logic tree as a business logic branch tree.

[0132] In an optional embodiment of the present application, the business logic branch tree structure includes the number of nodes and the number of branches of each method corresponding to each class; accordingly, the measurement module 43 is specifically used to: determine the method code logic branch density of each method according to the number of nodes and the number of branches of each method; sum up the code logic branch density of all methods corresponding to each class to obtain the code logic branch density corresponding to each class; sum up the code branch density of all classes to determine the business logic complexity of the source code.

[0133] In an optional embodiment of the present application, the metric module 43 is used to determine the method code logic branch density of each method according to the number of nodes and the number of branches of each method, and the calculation method is:

[0134] The method code logic branch density is the sum of the number of nodes and the number of branches;

[0135] The measurement module 43 is used to perform a summation operation based on the branch density of all classes of code to determine the business logic complexity of the source code. The calculation formula is:

[0136]

[0137] Where S1 is the business logic complexity of the source code, s i is the code logic branch density of the i-th class, N is the number of classes in the source code, where i is an integer between 1 and N, and N is an integer greater than 1 or equal to 1.

[0138] In an optional embodiment of the present application, the data processing module 42 is specifically used to: determine the number of layers, the number of dependencies between layers, and the total number of code lines from the perspective of hierarchical design of the entire source code according to the source code, and determine the number of layers, the number of dependencies between layers, and the total number of code lines as hierarchical design data; determine the number of classes, the inheritance depth between classes, the number of dependencies between classes, and the average number of code lines for classes from the perspective of class design of the entire source code according to the source code, and determine the number of classes, the inheritance depth between classes, the number of dependencies between classes, and the average number of code lines for classes as hierarchical design data; determine the number of methods, the number of calls between methods, and the average number of code lines for methods from the perspective of method design of the entire source code according to the source code, and determine the number of methods, the number of calls between methods, and the average number of code lines for methods as method design data.

[0139] In an optional embodiment of the present application, the preset weight ratio is the ratio between the preset hierarchical weight, the preset class weight and the preset method weight; accordingly, the measurement module 43 is also specifically used to: determine the hierarchical design complexity according to the number of layers, the number of dependencies between layers, the total number of code lines and the preset hierarchical weight; determine the class design complexity according to the number of classes, the inheritance depth between classes, the number of dependencies between classes, the average number of code lines of classes and the preset class weight; determine the method design complexity according to the number of methods, the number of calls between methods, the average number of code lines of methods and the preset method weight; determine the technical implementation complexity of the source code according to the hierarchical design complexity, the class design complexity and the method design complexity.

[0140] In an optional embodiment of the present application, the preset layer weight is a, the preset class weight is b, and the preset method weight is c, where a+b+c=1; accordingly, the metric module 43 is used to determine the layered design complexity according to the number of layers, the number of dependencies between layers, the total number of code lines, and the preset layer weights, and the calculation method is:

[0141] The complexity of layered design is a times the sum of the number of layers, the number of dependencies between layers, and the total number of lines of code;

[0142] The class design complexity is determined based on the number of classes, the inheritance depth between classes, the number of dependencies between classes, the average number of lines of code in a class, and the preset class weight. The calculation method is:

[0143] The class design complexity is b times the sum of the number of classes, the inheritance depth between classes, the number of dependencies between classes, and the average number of lines of code in a class;

[0144] The method design complexity is determined based on the number of methods, the number of calls between methods, the average number of lines of code in the method, and the preset method weight. The calculation method is:

[0145] The method design complexity is c times the sum of the number of class methods, the number of calls between class methods, and the average number of lines of code in the method;

[0146] According to the layered design complexity, class design complexity and method design complexity, the technical implementation complexity of the source code is determined by the calculation method:

[0147] The technical implementation complexity of the source code is the sum of the layered design complexity, class design complexity, and method design complexity.

[0148] In an optional embodiment of the present application, the measurement module 43 is specifically used to determine the overall complexity of the source code according to the business logic complexity and the technical implementation complexity, and the calculation method is:

[0149] The overall complexity of the source code is the sum of the business logic complexity and the technical implementation complexity.

[0150] The software code complexity measurement device provided in this embodiment can be used to perform the above Figure 2 The technical solution of the method embodiment shown has similar implementation principles and technical effects, and will not be described in detail in this embodiment.

[0151] Figure 5 The hardware structure diagram of the electronic device provided in the embodiment of the present application is shown in FIG. Figure 5 As shown, the electronic device includes: a processor 501 and a memory 502 communicatively connected to the processor 501 .

[0152] The memory 502 stores computer-executable instructions.

[0153] The processor 501 executes the computer-executable instructions stored in the memory 502 to implement the various steps involved in the method embodiment of the software code complexity measurement method as described above. For details, please refer to the relevant description in the above method embodiment.

[0154] Optionally, the memory 502 may be independent or integrated with the processor 501 .

[0155] When the memory 502 is independently provided, the electronic device further includes a bus 503 for connecting the memory 502 and the processor 501 .

[0156] The embodiment of the present application further provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are executed by a processor, the above-mentioned method for measuring software code complexity is implemented.

[0157] An embodiment of the present application also provides a computer program product, including a computer program, which implements the above method for measuring software code complexity when executed by a processor.

[0158] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described order of actions, because according to the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all optional embodiments, and the actions and modules involved are not necessarily required by the present application.

[0159] It should be further noted that, although the various steps in the flowchart are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless there is a clear description in this article, the execution of these steps is not strictly limited in order, and these steps can be executed in other orders. Moreover, at least a portion of the steps in the flowchart may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these sub-steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a portion of the sub-steps or stages of other steps.

[0160] It should be understood that the above-mentioned device embodiments are only illustrative, and the device of the present application can also be implemented in other ways. For example, the division of units / modules in the above-mentioned embodiments is only a logical function division, and there may be other division methods in actual implementation. For example, multiple units, modules or components can be combined, or can be integrated into another system, or some features can be ignored or not executed.

[0161] In addition, unless otherwise specified, each functional unit / module in each embodiment of the present application may be integrated into one unit / module, each unit / module may exist physically separately, or two or more units / modules may be integrated together. The above-mentioned integrated unit / module may be implemented in the form of hardware or in the form of a software program module.

[0162] If the integrated unit / module is implemented in the form of hardware, the hardware may be a digital circuit, an analog circuit, etc. The physical implementation of the hardware structure includes but is not limited to transistors, memristors, etc. Unless otherwise specified, the processor may be any appropriate hardware processor, such as a CPU, a GPU, an FPGA, a DSP, an ASIC, etc. Unless otherwise specified, the storage unit may be any appropriate magnetic storage medium or magneto-optical storage medium, such as a resistive random access memory (RRAM), a dynamic random access memory (DRAM), a static random access memory (SRAM), an enhanced dynamic random access memory (EDRAM), a high-bandwidth memory (HBM), a hybrid memory cube (HMC), etc.

[0163] If the integrated unit / module is implemented in the form of a software program module and sold or used as an independent product, it can be stored in a computer-readable memory. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a memory, including a number of instructions to enable a computer device (which can be a personal computer, server or network device, etc.) to execute all or part of the steps of the various embodiments of the present application. The aforementioned memory includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, disk or optical disk and other media that can store program codes.

[0164] In the above embodiments, the description of each embodiment has its own emphasis. For parts not described in detail in a certain embodiment, please refer to the relevant description of other embodiments. The technical features of the above embodiments can be combined arbitrarily. In order to make the description concise, all possible combinations of the technical features in the above embodiments are not described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0165] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the invention disclosed herein. The present application is intended to cover any modification, use or adaptation of the present application, which follows the general principles of the present application and includes common knowledge or customary techniques in the art that are not disclosed in the present application. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present application are indicated by the following claims.

[0166] It should be understood that the present application is not limited to the precise structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.

Claims

1. A method for measuring software code complexity, characterized in that: include: Get the source code; Perform code scanning and syntax analysis on the source code to obtain business logic data; Determine a business logic branch tree according to the business logic data; Determine the code logic branch density of each method corresponding to a class according to the business logic branch tree; Determine the code logic branch density of each class based on the code logic branch density of all methods corresponding to each class; Determine the business logic complexity of the source code based on the code logic branch density of all classes; Determining hierarchical design data, class design data, and method design data according to the source code; Determining the technical implementation complexity of the source code according to the hierarchical design data, the class design data, the method design data and a preset weight ratio; The overall complexity of the source code is determined according to the business logic complexity and the technical implementation complexity.

2. The method according to claim 1, characterized in that The code scanning and syntax analysis of the source code to obtain business logic data includes: The source code is analyzed by static code scanning and syntax analysis to analyze operators and control flow statements in the code to perform code scanning and syntax analysis to obtain business logic data.

3. The method according to claim 1, characterized in that The business logic data includes business step data and business step correspondence data; Accordingly, determining the business logic branch tree according to the business logic data includes: Determine the node information of each business step and the upper and lower hierarchical relationship of each business step according to the business step data and the business step corresponding relationship data; According to the node information of each business step and the upper and lower hierarchical relationship of each business step, a logical tree with a tree structure is drawn, and the logical tree is determined as a business logic branch tree.

4. The method according to claim 1, characterized in that: The business logic branch tree structure includes the number of nodes and branches of each method corresponding to each class; Accordingly, determining the code logic branch density of each method corresponding to a class according to the business logic branch tree includes: Determine the method code logic branch density of each method according to the number of nodes and the number of branches of each method; Determining the code logic branch density of each class according to the code logic branch density of all methods corresponding to each class includes: The code logic branch density of all methods corresponding to each class is summed up to obtain the code logic branch density corresponding to each class; Determining the business logic complexity of the source code according to the code logic branch density of all classes includes: The business logic complexity of the source code is determined by performing a summation operation based on the branch density of the codes of all classes.

5. The method according to claim 4, characterized in that The method code logic branch density of each method is determined according to the number of nodes and the number of branches of each method, and the calculation method is: The method code logic branch density is the sum of the number of nodes and the number of branches; The business logic complexity of the source code is determined by summing the branch densities of all classes of code, and the calculation formula is: Where S1 is the business logic complexity of the source code, s i is the code logic branch density of the i-th class, N is the number of classes in the source code, where i is an integer between 1 and N, and N is an integer greater than 1 or equal to 1.

6. The method according to claim 1, characterized in that According to the source code, hierarchical design data, class design data and method design data are determined, including: Determine the number of layers, the number of dependencies between layers, and the total number of code lines from the perspective of layered design of the entire source code according to the source code, and determine the number of layers, the number of dependencies between layers, and the total number of code lines as layered design data; According to the source code, the number of classes, the inheritance depth between classes, the number of dependencies between classes and the average number of lines of code for a class are determined from the perspective of class design of the entire source code, and the number of classes, the inheritance depth between classes, the number of dependencies between classes and the average number of lines of code for a class are determined as hierarchical design data; According to the source code, the number of methods, the number of calls between methods and the average number of code lines of methods are determined from the perspective of method design of the entire source code, and the number of methods, the number of calls between methods and the average number of code lines of methods are determined as method design data.

7. The method according to claim 6, characterized in that The preset weight ratio is the ratio between the preset hierarchical weight, the preset class weight and the preset method weight; Accordingly, determining the technical implementation complexity of the source code according to the hierarchical design data, the class design data, the method design data and the preset weight ratio includes: Determine the complexity of layered design according to the number of layers, the number of dependencies between layers, the total number of code lines and the preset layer weights; Determine the class design complexity according to the number of classes, the inheritance depth between classes, the number of dependencies between classes, the average number of lines of code of the class, and the preset class weight; Determine the method design complexity according to the number of methods, the number of calls between methods, the average number of lines of code of the method and the preset method weight; The technical implementation complexity of the source code is determined according to the layered design complexity, the class design complexity and the method design complexity.

8. The method according to claim 7, characterized in that The preset layer weight is a, the preset class weight is b, and the preset method weight is c, where a+b+c=1; Accordingly, the calculation method for determining the layered design complexity according to the number of layers, the number of dependencies between layers, the total number of code lines and the preset layer weight is: The complexity of layered design is a times the sum of the number of layers, the number of dependencies between layers, and the total number of lines of code; The class design complexity is determined according to the number of classes, the inheritance depth between classes, the number of dependencies between classes, the average number of lines of code of the class and the preset class weight, and the calculation method is: The class design complexity is b times the sum of the number of classes, the inheritance depth between classes, the number of dependencies between classes, and the average number of lines of code in a class; The method design complexity is determined according to the number of methods, the number of calls between methods, the average number of lines of code of the method and the preset method weight, and the calculation method is: The method design complexity is c times the sum of the number of class methods, the number of calls between class methods, and the average number of lines of code in the method; The calculation method for determining the technical implementation complexity of the source code according to the hierarchical design complexity, the class design complexity and the method design complexity is: The technical implementation complexity of the source code is the sum of the layered design complexity, class design complexity, and method design complexity.

9. The method according to any one of claims 1 to 8, characterized in that The overall complexity of the source code is determined based on the business logic complexity and the technical implementation complexity, and the calculation method is: The overall complexity of the source code is the sum of the business logic complexity and the technical implementation complexity.

10. A software code complexity measurement device, characterized in that: include: Get module, used to get source code; A data processing module is used to perform code scanning and syntax analysis on the source code to obtain business logic data; The measurement module is further used to determine business logic branch tree data according to the business logic data; The measurement module is further used to determine the code logic branch density of each method corresponding to a class according to the business logic branch tree data; The measurement module is further used to determine the code logic branch density of each class according to the code logic branch density of all methods corresponding to each class; The measurement module is further used to determine the business logic complexity of the source code according to the code logic branch density of all classes; The data processing module is further used to determine hierarchical design data, class design data and method design data according to the source code; The metric module is further used to determine the technical implementation complexity of the source code according to the hierarchical design data, the class design data, the method design data and the preset weight ratio; The measurement module is also used in the overall complexity determination module to determine the overall complexity of the source code according to the business logic complexity and the technical implementation complexity.

11. An electronic device, characterized in that: include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory to implement the method according to any one of claims 1 to 9.

12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 9 when executed by a processor.

13. A computer program product, characterized in that The invention comprises a computer program, which implements the method according to any one of claims 1 to 9 when being executed by a processor.