Method and device for detecting code quality

By matching the parser to generate an abstract syntax tree and compute the circle complexity, and combining the number of code lines for quality analysis, the problem of single code quality detection dimensions and unoptimized parsing results in the existing technology is solved, and a more comprehensive and accurate code quality evaluation is achieved.

CN119782174AInactive Publication Date: 2025-04-08JINGDONG ALLIANZ GENERAL INSURANCE CO LTD

Patent Information

Application Number
CN202411919587.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-24
Publication Date
2025-04-08
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

In the prior art, the code quality detection dimension is single, and the code characteristics are not considered when analyzing the code to generate an abstract syntax tree, resulting in limitations in evaluation standards and unoptimized analysis results.

Method used

By obtaining the attribute information of the code file, matching the parser, generating an abstract syntax tree, determining the circle complexity through statistics and determining the number of nodes, and performing quality analysis in combination with the number of code lines, triggering an alarm in response to the quality analysis result exceeding the preset threshold.

Benefits of technology

It realizes the evaluation of code quality from multiple dimensions, improves circle complexity and accuracy, enhances code maintainability and readability, and promptly issues alarms to promote code optimization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119782174A_ABST
    Figure CN119782174A_ABST
Patent Text Reader

Abstract

The invention discloses a method and device for detecting code quality, and relates to the technical field of computers. A specific embodiment of the method comprises the following steps: acquiring a code file of which the quality is to be detected, and matching an analyzer according to attribute information of the code file; processing the code file by using a parser to obtain an abstract syntax tree; traversing the abstract syntax tree, and determining the circle complexity of the code file through a strategy of counting and judging the number of nodes; and counting the code line number of the code file, determining a quality analysis result of the code file according to one or more of the circle complexity and the code line number, and triggering an alarm in response to the condition that the quality analysis result is greater than or equal to a preset threshold value. According to the implementation mode, the abstract syntax tree aiming at the code characteristics can be generated, the code quality is systematically evaluated from multiple dimensions, and developers are helped to quickly identify complexity and potential problems in the codes, so that subsequent continuous improvement and optimization of the codes are facilitated.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method and device for detecting code quality. Background Art

[0002] Code quality detection is carried out through automated tools or manual review, aiming to discover errors, irregular habits and security vulnerabilities in the code. However, current tools can usually only evaluate from a single dimension, resulting in limitations in the evaluation criteria and an inability to fully reflect the various problems existing in the code. In addition, the existing abstract syntax tree parsing method is relatively conventional and lacks targeted analysis of specific code features, which affects the optimization and support of the parsing results. In order to improve the effectiveness of code quality detection, it is necessary to develop more comprehensive and intelligent evaluation tools to overcome the limitations of existing methods and provide more targeted code feature analysis. Summary of the invention

[0003] In view of this, an embodiment of the present invention provides a method and device for detecting code quality, which can at least solve the problem that the dimension of detecting code quality in the prior art is single and the method of parsing code to generate an abstract syntax tree does not consider the code characteristics.

[0004] To achieve the above object, according to one aspect of an embodiment of the present invention, a method for detecting code quality is provided, comprising:

[0005] Obtain a code file whose quality is to be tested, and match a parser according to the attribute information of the code file; wherein the code file is a full code file or an incremental code file;

[0006] Process the code file using the parser to obtain an abstract syntax tree;

[0007] Traversing the abstract syntax tree, and determining the cyclomatic complexity of the code file by counting and determining the number of nodes;

[0008] The number of code lines of the code file is counted, and a quality analysis result of the code file is determined according to one or more of the cyclomatic complexity and the number of code lines. In response to the quality analysis result being greater than or equal to a preset threshold, an alarm is triggered.

[0009] Optionally, matching a parser according to the attribute information of the code file includes:

[0010] In response to the file type being a target file type, determining a preconfigured first parser according to a programming language type of the code file and the target file type;

[0011] In response to the file type not being the target file type, the number of code lines of the code file is counted to determine a code line range in which the number of code lines falls; and based on the programming language type, the code line range and one or more of the file type, a second parser with the shortest time consumption is determined from a preset parser set.

[0012] Optionally, the method further comprises:

[0013] Determine a target test code file; wherein different target test code files are set for different programming language types, different code line ranges, and different file types;

[0014] Use each parser in the preset parser set to process the target test code file to obtain the time consumption distribution at different time points, and use a preset method to process the time consumption distribution to obtain the time consumption of each parser.

[0015] Optionally, the method further comprises:

[0016] Determine a configuration item of a code quality detection tool, and receive a file type set in the configuration item; wherein the file type includes a target file type and other file types;

[0017] Receive the quality analysis result level information set in the configuration item, as well as the alarm information set for each level;

[0018] Receives the preset thresholds set in the configuration item for each preset dimension.

[0019] Optionally, traversing the abstract syntax tree and determining the cyclomatic complexity of the code file by counting and determining the number of nodes includes:

[0020] Traversing each node in the abstract syntax tree, and in response to each node being a decision node, increasing the value of the counter by 1;

[0021] In response to the end of the traversal, the sum of the value of the counter and the preset value is used as the cyclomatic complexity of the code file.

[0022] Optionally, determining the quality analysis result of the code file according to one or more of the cyclomatic complexity and the number of code lines includes:

[0023] Calculating a ratio of the cyclomatic complexity to the number of lines of code;

[0024] Performing weighted processing on the cyclomatic complexity and the number of lines of code to obtain a weighted sum value;

[0025] Determine the number of nodes determined by statistics of the quality analysis result of the code file according to one or more of the cyclomatic complexity, the number of code lines, the ratio, or the weighted sum value.

[0026] Optionally, the code file includes one or more functions, and the method is used to detect the quality of each function, and the method further includes:

[0027] Determine the reference relationship between functions, and mark the function call chain in the abstract syntax tree of the code file;

[0028] In the abstract syntax tree of the code file, determine a sub-abstract syntax tree of each function, and mark the quality analysis result of each function and the decision node in each sub-abstract syntax tree;

[0029] For each code line in each function, the number of marked decision nodes is counted, and the code line with the largest number of decision nodes is highlighted.

[0030] Optionally, the method further comprises:

[0031] In response to the quality analysis result being less than the preset threshold, generating a page based on the code file;

[0032] In response to the access operation to the page, the execution time of each decision node in each function is obtained, and the execution time interval is determined;

[0033] A highlighting mode pre-set for each time-consuming interval is determined, and each decision node in the abstract syntax tree of the code file is highlighted.

[0034] To achieve the above object, according to another aspect of an embodiment of the present invention, a device for detecting code quality is provided, comprising:

[0035] A matching module, used to obtain a code file whose quality is to be tested, and match a parser according to the attribute information of the code file; wherein the code file is a full code file or an incremental code file;

[0036] A cyclomatic complexity module, configured to process the code file using the parser to obtain an abstract syntax tree; traverse the abstract syntax tree and determine the cyclomatic complexity of the code file by using a strategy for counting and determining the number of nodes;

[0037] A quality analysis module is used to count the number of code lines of the code file, determine the quality analysis result of the code file according to one or more of the cyclomatic complexity and the number of code lines, and trigger an alarm in response to the quality analysis result being greater than or equal to a preset threshold.

[0038] Optionally, the matching module is used to:

[0039] In response to the file type being a target file type, determining a preconfigured first parser according to a programming language type of the code file and the target file type;

[0040] In response to the file type not being the target file type, the number of code lines of the code file is counted to determine a code line range in which the number of code lines falls; and based on the programming language type, the code line range and one or more of the file type, a second parser with the shortest time consumption is determined from a preset parser set.

[0041] Optionally, the device further comprises a preprocessing module, configured to:

[0042] Determine a target test code file; wherein different target test code files are set for different programming language types, different code line ranges, and different file types;

[0043] Use each parser in the preset parser set to process the target test code file to obtain the time consumption distribution at different time points, and use a preset method to process the time consumption distribution to obtain the time consumption of each parser.

[0044] Optionally, the device further comprises a configuration module, configured to:

[0045] Determine a configuration item of a code quality detection tool, and receive a file type set in the configuration item; wherein the file type includes a target file type and other file types;

[0046] Receive the quality analysis result level information set in the configuration item, as well as the alarm information set for each level;

[0047] Receives the preset thresholds set in the configuration item for each preset dimension.

[0048] Optionally, the cyclomatic complexity module is used to:

[0049] Traversing each node in the abstract syntax tree, and in response to each node being a decision node, increasing the value of the counter by 1;

[0050] In response to the end of the traversal, the sum of the value of the counter and the preset value is used as the cyclomatic complexity of the code file.

[0051] Optionally, the quality analysis module is used to:

[0052] Calculating a ratio of the cyclomatic complexity to the number of lines of code;

[0053] Performing weighted processing on the cyclomatic complexity and the number of lines of code to obtain a weighted sum value;

[0054] Determine the number of nodes determined by statistics of the quality analysis result of the code file according to one or more of the cyclomatic complexity, the number of code lines, the ratio, or the weighted sum value.

[0055] Optionally, the code file includes one or more functions, the device is used to detect the quality of each function, and the device further includes a code flow chart module, which is used to:

[0056] Determine the reference relationship between functions, and mark the function call chain in the abstract syntax tree of the code file;

[0057] In the abstract syntax tree of the code file, determine a sub-abstract syntax tree of each function, and mark the quality analysis result of each function and the decision node in each sub-abstract syntax tree;

[0058] For each code line in each function, the number of marked decision nodes is counted, and the code line with the largest number of decision nodes is highlighted.

[0059] Optionally, the code flow chart module is further used to:

[0060] In response to the quality analysis result being less than the preset threshold, generating a page based on the code file;

[0061] In response to the access operation to the page, the execution time of each decision node in each function is obtained, and the execution time interval is determined;

[0062] A highlighting mode pre-set for each time-consuming interval is determined, and each decision node in the abstract syntax tree of the code file is highlighted.

[0063] To achieve the above objective, according to another aspect of an embodiment of the present invention, an electronic device for detecting code quality is provided.

[0064] The electronic device of an embodiment of the present invention includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement any of the above-mentioned methods for detecting code quality.

[0065] To achieve the above objective, according to another aspect of an embodiment of the present invention, a computer-readable medium is provided, on which a computer program is stored, and when the program is executed by a processor, any of the above methods for detecting code quality is implemented.

[0066] To achieve the above object, according to another aspect of an embodiment of the present invention, a computer program product is provided. A computer program product according to an embodiment of the present invention includes a computer program, and when the program is executed by a processor, the method for detecting code quality provided by an embodiment of the present invention is implemented.

[0067] According to the solution provided by the present invention, one embodiment of the above invention has the following advantages or beneficial effects: a unified set of detection code quality standards is designed to be able to systematically evaluate code quality, solving the problem that existing tools consider a single dimension and do not consider code characteristics when generating an abstract syntax tree. By considering code attributes, generating an abstract syntax tree, and calculating cyclomatic complexity, the accuracy of cyclomatic complexity can be improved, and combined with the number of lines of code, it can help developers quickly identify the complexity and potential problems in the code from multiple dimensions, which not only improves the maintainability and readability of the code, but also can issue an alarm in time when the quality analysis results exceed the preset threshold, thereby promoting continuous improvement and optimization of the code.

[0068] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS

[0069] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.

[0070] Figure 1 This is a schematic diagram of the main process of a method for detecting code quality according to an embodiment of the present invention;

[0071] Figure 2 is a flowchart of an optional method for detecting code quality according to an embodiment of the present invention;

[0072] Figure 3 is a flowchart of another optional method for detecting code quality according to an embodiment of the present invention;

[0073] Figure 4 is a flowchart of a specific method for detecting code quality according to an embodiment of the present invention;

[0074] Figure 5 is a schematic diagram of main modules of a device for detecting code quality according to an embodiment of the present invention;

[0075] Figure 6 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;

[0076] Figure 7 It is a schematic diagram of the structure of a computer system of a mobile device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION

[0077] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0078] It should be noted that the collection, collection, updating, analysis, processing, use, transmission, storage and other aspects of user personal information involved in the technical solution of this disclosure are in compliance with the provisions of relevant laws and regulations, are used for legitimate purposes, and do not violate public order and good morals. Necessary measures are taken for user personal information to prevent illegal access to user personal information data and maintain the security of user personal information, network security and national security.

[0079] In order to improve the flexibility and practicality of the code quality detection tool, you can customize the supported file types through the configuration items of the code quality detection tool. In the current technology, due to the lack of support for Vue type files, it is impossible to perform effective quality analysis on its code. By adding new configuration items, users can specify target file types, such as Vue / React, as well as TypeScript (the extension of TypeScript files), JavaScript (the extension of JavaScript files), etc., thereby solving the problem that existing tools do not support cyclomatic complexity (CC) detection for Vue type files. In addition to the Vue file type, this solution also allows other file types that need to be detected to be flexibly set according to actual needs, thereby comprehensively improving the functionality and applicability of the code quality detection tool.

[0080] Vue: This is the extension of the Vue.js single-file component. Vue.js is a popular JavaScript framework for building user interfaces. Vue files usually contain three parts: template, script, and style, which define the structure, logic, and style of the component respectively. In a project, these file types can work together to build a dynamic and interactive web application. For example, a project may use the Vue.js framework, which contains multiple Vue files to define various components; at the same time, the project may also contain some JS files to handle non-component logic; if the project uses TypeScript, there will also be TSX files to take advantage of TypeScript to write React components.

[0081] In addition, multiple quality levels and their corresponding error messages can be set in the configuration items, for example: below 10 does not require optimization, 10-20 requires optimization (warning level, warn), above 20 requires adjustment of code logic (error level, error), and if it exceeds 30, it needs to be rewritten. Each level can be set with different marking methods, such as green, yellow, red and black corresponding to different levels.

[0082] At the same time, you can also set preset thresholds in the configuration items, such as the maximum number of lines of code for a single function (such as 100 lines), the maximum number of lines of code for a code file (such as 500 lines), the cyclomatic complexity threshold of a single function (such as 20), the threshold of the quotient of the cyclomatic complexity and the number of lines of code for a single function (such as 5), and the threshold of the weighted sum of the cyclomatic complexity and the number of lines of code for a single function (such as 40). These lines of code, cyclomatic complexity, ratio, and weighted sum can be regarded as different dimensions. By setting the above configuration items, not only the problem of insufficient configuration capabilities of existing tools is solved, but also the limitation of a single threshold limit (that is, only one fixed threshold can be set, and an error will be reported after exceeding it) is broken. This configuration can provide more flexible and diverse functions, avoid the singleness of functions, significantly improve the flexibility and practicality of code quality detection tools, and better meet the diverse needs in daily development.

[0083] Different projects may have different code quality standards and preferences. According to the above configuration files, the personalized needs of different projects can be flexibly met. It should be noted that this solution can perform quality testing on front-end code, such as Vue / React and TypeScript, which are mainly used in the front-end, and can also perform quality testing on back-end code, such as JavaScript, which is mainly used in the back-end.

[0084] See also Figure 1 , which shows a main flow chart of a method for detecting code quality provided by an embodiment of the present invention, comprising the following steps:

[0085] S101: Obtain a code file whose quality is to be tested, and match a parser according to attribute information of the code file; wherein the code file is a full code file or an incremental code file;

[0086] S102: Processing the code file using the parser to obtain an abstract syntax tree;

[0087] S103: traversing the abstract syntax tree, and determining the cyclomatic complexity of the code file by counting and determining the number of nodes;

[0088] S104: Count the number of code lines of the code file, determine a quality analysis result of the code file according to one or more of the cyclomatic complexity and the number of code lines, and trigger an alarm in response to the quality analysis result being greater than or equal to a preset threshold.

[0089] In the above implementation, for steps S101 and S102, a class usually contains multiple functions and a large amount of code, which causes the prior art to often have a high cyclomatic complexity when detecting it, because classes and functions are repeatedly counted. In addition, some existing tools can only detect the full amount of code and cannot handle incremental code. In order to solve these problems and improve the flexibility and accuracy of code quality detection, this solution is specially designed to support quality detection of functions in full or incremental code files, and does not involve class detection. This can avoid repeated calculations, ensure that the detection results are more accurate, and improve development efficiency.

[0090] First, obtain the code file to be tested for quality. The code file can be a full code file or an incremental code file, such as searching from a code library, or a code file sent from an upstream, or a code file to be tested that is submitted or uploaded by the user after the code is written and before the code goes online. The code file can include only one function or multiple functions. This solution only performs quality testing on functions, without considering classes. This effectively solves the problem that existing tools can only test full codes and that class detection results have large deviations. For the determination of incremental code, methods currently known to those skilled in the art can be used, such as using the difference comparison function of a version control system (such as Git). By comparing the differences between two different versions in the code library, the newly added or modified code parts can be accurately identified, thereby determining the incremental code.

[0091] Currently, the parsing of syntax trees is done in a conventional way, which is unable to achieve targeted parsing based on the attribute information of the code file, resulting in a lack of optimization and support for specific code features in the parsing results. This general parsing method cannot fully consider the unique needs of different programming languages, frameworks, or project structures, thus affecting the accuracy and efficiency of code analysis. In order to improve the parsing effect, it is necessary to introduce a more flexible parsing strategy that can perform customized parsing based on the specific attributes of the code file to provide more accurate and efficient code analysis results.

[0092] After obtaining the code file to be tested, this solution can match the parser according to the attribute information of the code file, so as to parse the code file into an abstract syntax tree (AST) through the parser. The abstract syntax tree is an abstract representation of the grammatical structure of the source code. It represents the grammatical structure of the programming language in the form of a tree, and each node on the tree represents a structure in the source code. Common statements in the code, such as literals, identifiers, expressions, statements, module syntax, and class syntax, have their own corresponding AST node types.

[0093] The process of matching the parser can be done as follows:

[0094] Step 1: Determine the file type of the code file, such as Vue / React (i.e. the target file type), TypeScript, JavaScript. If it is Vue / React, proceed to step 2, otherwise proceed to step 3;

[0095] Step 2: For the parsing of Vue files, we use languages ​​such as C / Rust and combine them with the syntax characteristics of Vue to develop our own tools. That is, we develop a set of parsers for Vue file types and programming language types in advance. For example, when parsing Vue single-file components, the templates, scripts, and styles in the Vue files can be parsed into corresponding abstract syntax trees for subsequent quality testing and analysis. For example, when parsing the JavaScript or TypeScript script part in the Vue file, these script codes can be converted into abstract syntax trees, thereby extracting functions and other code structures for detailed code quality testing. Similarly, for react, a parser needs to be specially customized according to language requirements.

[0096] Step 3: For typescript and JavaScript files, perform dynamic scheduling based on the existing parsers on the market (Babel, Typescript, Tree-sitter, ast-grep, swc, oxc, etc.). The principle of dynamic selection: using code file type, number of code lines, programming language type and / or parser type as factors, a probability distribution graph is formed based on big data statistics to obtain the optimal time-consuming parser. In subsequent dynamic scheduling, this data is fully utilized and updated.

[0097] Different target test code files are set in advance for different programming language types, different code line ranges, and different code file types. Taking the above 6 parsers as an example, for the same target test code file, these 6 parsers are used to parse the target test code file into AST respectively, and the time consumption at different time points is collected to obtain the time-time consumption distribution curve. Assuming that in the process of Babel processing a certain target test code file, 90% of the time consumption is 4s and 10% of the time consumption is 5s, the majority strategy is adopted to determine that its processing time consumption is 4s. In this way, the time consumption of different parsers processing the same target test code file is obtained, and then the parser with the shortest time consumption is used as the optimal parser corresponding to one or more of the programming language type, code line range, and file type. This scheme preferably considers the combination of programming language type and code line number.

[0098] For step S103, cyclomatic complexity is also called conditional complexity, which is a measure of code complexity and is used to indicate the complexity of a program. It can be used to measure the complexity of a module's judgment structure, which is quantitatively expressed as the number of independent current paths, and can also be understood as the number of test cases used at least to cover all possible situations. A high cyclomatic complexity indicates that the judgment logic of the program code is complex, and the quality may be low and difficult to test and maintain. Possible errors in the program are closely related to high cyclomatic complexity. Therefore, in code quality detection, cyclomatic complexity is a key evaluation indicator that helps developers identify and optimize complex code segments and improve the readability and maintainability of the code.

[0099] After generating an AST for a single function, the cyclomatic complexity can be determined by traversing the AST and counting the number of decision nodes. Depth-first traversal of the AST: Use depth-first traversal to visit each node in the AST. Traversal can be implemented using recursion or a stack. Count decision nodes: During the traversal process, for each decision node encountered (such as IfStatement, ForStatement, etc.), increase the value of the counter. Calculate cyclomatic complexity: Calculate the cyclomatic complexity based on the total number of decision nodes plus 1. For example, if 10 decision nodes are counted during the traversal, the cyclomatic complexity is 11.

[0100] Among them, the decision node is an important concept in decision trees and similar algorithms. In a decision tree, a decision node is used to branch according to the value of a certain feature, thereby determining the flow of data. Each decision node divides the data into two or more child nodes based on a condition. Conditional judgment: Each decision node contains a conditional judgment, usually a threshold or category of a feature. Branching: Based on the result of the conditional judgment, the data will be distributed to different child nodes. Recursive structure: Each child node can further contain more decision nodes, forming a recursive tree structure.

[0101] In addition to the above methods, other methods can also be used to calculate the cyclomatic complexity of a function. For example, the calculation formula for cyclomatic complexity is: CC = E - N + 2P, where: E is the number of edges in the program (i.e., the edges in the control flow graph), N is the number of nodes in the program (i.e., the nodes in the control flow graph), and P is the number of connected graphs in the program (usually 1). The higher the cyclomatic complexity, the greater the complexity of the code, and the difficulty of maintenance and debugging increases accordingly. Code with high cyclomatic complexity is prone to hide more defects and errors, so cyclomatic complexity is an important evaluation indicator in code quality detection. Through the application of these tools, problems can be discovered and fixed in the early stages of the software development life cycle, thereby reducing later maintenance costs and improving the overall quality of software products. This solution prefers the method of determining nodes.

[0102] For step S104, for the quality analysis results, this solution can directly use the cyclomatic complexity as the quality analysis result, or use the quotient of the cyclomatic complexity and the number of lines of code as the quality analysis result. It can also determine the weights set in advance for the cyclomatic complexity and the number of lines of code, and use the weighted summation method to process the cyclomatic complexity and the number of lines of code to obtain the quality analysis result of a single function. Subsequent quality analysis can use a combination of one or more of the above methods, and this solution does not limit this.

[0103] For the quotient of cyclomatic complexity and number of lines of code, if the cyclomatic complexity of function A exceeds 50 and the number of lines is 10, the calculation result is: 50 / 10=5. If the cyclomatic complexity of function B exceeds 60 and the number of lines is 15, the calculation result is: 60 / 15=4. For the weighted sum of cyclomatic complexity and number of lines of code, for example, the weight of cyclomatic complexity is 0.7 and the weight of number of lines of code is 0.3, for function A, the weighted sum result is 50*0.7+10*0.3=38, and for function B, the weighted sum result is 60*0.7+15*0.3=46.5. Therefore, the higher the cyclomatic complexity, the more code problems there are. The number of lines of code should also be considered. In addition, the maximum number of lines can be specified for a single function, and small and pure functions should be kept as much as possible. If the number of lines of code for a single function exceeds the preset threshold, an alarm is triggered, such as prompting that the number of lines of code is too large.

[0104] The above description states that "preset thresholds can also be set in the configuration items, such as the maximum number of code lines for a single function (such as 100 lines), the maximum number of code lines for a code file (such as 500 lines), the cyclomatic complexity threshold for a single function (such as 20), the threshold for the quotient of the cyclomatic complexity and the number of code lines for a single function (such as 5), and the threshold for the weighted sum of the cyclomatic complexity and the number of code lines for a single function (such as 40). ". Therefore, here, the quality analysis results can be compared with the corresponding preset thresholds. If they are greater than or equal to the preset thresholds, an alarm is triggered and a prompt is given. For example, if the cyclomatic complexity of function A is greater than 20, an alarm is triggered for the cyclomatic complexity. In addition, if the ratio is equal to 5, the threshold is triggered for the ratio; if the weighted sum result and cyclomatic complexity of function B are both greater than the corresponding thresholds, alarms are triggered for these two dimensions.

[0105] It should be noted that this solution is designed for code quality detection and provides detection results based on preset thresholds. The specific threshold to use for comparison, the code to be detected, and the code to be used based on the detection results are all decided by the developer based on the analysis results.

[0106] In addition to informing developers of which dimensions of function A and function B exceed the threshold in the above-mentioned prompting method, it is also possible to determine which of function A and function B is more complex through certain technical means to further simplify the developer's work. Specifically, one or more of the following methods may be used:

[0107] 1. Priority sorting method, compare the complexity indicators of the functions one by one through the set priority order. Assume that the priority order is set: cyclomatic complexity > number of lines of code > ratio of cyclomatic complexity / number of lines of code > weighted sum of cyclomatic complexity and number of lines of code. If the cyclomatic complexities of function A and function B are different, the function with higher cyclomatic complexity is more complex. For example, if the cyclomatic complexity of function A is 50 and the cyclomatic complexity of function B is 60, function B is more complex. If the cyclomatic complexities are the same, compare the number of lines of code, and the function with more lines of code is more complex. If the number of lines of code is also the same, compare the ratio of cyclomatic complexity to the number of lines of code, and the function with a higher ratio is more complex. If the above three items are the same, calculate the weighted sum of cyclomatic complexity and number of lines of code for final comparison.

[0108] 2. Overall weighted summation method. Developers can set the weights of cyclomatic complexity, number of lines of code, ratio of cyclomatic complexity / number of lines of code, and weighted sum of cyclomatic complexity and number of lines of code according to actual needs, so as to determine the overall weighted sum of the function through the overall weighted summation method. Assume that the weights are set as follows: the weight of cyclomatic complexity is 0.4, the weight of number of lines of code is 0.3, the weight of ratio of cyclomatic complexity / number of lines of code is 0.2, and the weight of weighted sum of cyclomatic complexity and number of lines of code is 0.1. The calculation shows that the overall weighted sum value of function A is WS_A = 0.4*50 + 0.3*10 + 0.2*5 +0.1*(50*0.7+10*0.3)=20+3+1+3.8=27.8, and the overall weighted sum value of function B is WS_B=0.4*60+0.3*15+0.2*4+ 0.1*(60*0.7+15*0.3)= 24+4.5+0.8+4.65=34.95. Function B is more complex than function A.

[0109] 3. Multi-dimensional comprehensive scoring method. By combining multiple complexity indicators and setting a scoring range for each indicator (such as 1 to 10 points), the function is scored based on its actual performance. Finally, all scores are added or weighted to obtain a comprehensive score.

[0110] Assuming that the cyclomatic complexity scoring criteria are: [0,20)-1 point, [20,40)-3 points, [40,60)-5 points, [60,80)-7 points, [80,100)-10 points, and the code line scoring criteria are: [0,10)-1 point, [10,20)-3 points, [20,30)-5 points, [30,40)-7 points, [40,50)-10 points, the cyclomatic complexity / code line ratio score The standards are: [0,1)-1 point, [1,2)-3 points, [2,3)-5 points, [3,4)-7 points, [4,5)-8 points, [5,infinity)-10 points. The weighted sum of cyclomatic complexity and number of lines of code is: [0,10)-1 point, [10,20)-3 points, [20,30)-5 points, [30,40)-7 points, [40,50)-9 points, [50,infinity)-10 points.

[0111] For function A, the cyclomatic complexity is 50 (5 points), the number of lines of code is 10 (3 points), the cyclomatic complexity / number of lines of code ratio is 5 (10 points), the weighted sum is 38 (7 points), and the comprehensive score is 25 points. For function B, the cyclomatic complexity is 60 (7 points), the number of lines of code is 15 (3 points), the cyclomatic complexity / number of lines of code ratio is 4 (8 points), the weighted sum is 46.5 (9 points), and the comprehensive score is 27 points. Therefore, according to the multi-dimensional comprehensive scoring method, function B is more complex than function A.

[0112] It should be noted that the above example is based on four dimensions: "cyclomatic complexity, number of lines of code, ratio of cyclomatic complexity / number of lines of code, and weighted sum of cyclomatic complexity and number of lines of code". In actual applications, multiple complexity indicators can be selected for evaluation according to needs, for example, only "cyclomatic complexity, number of lines of code, and ratio of cyclomatic complexity / number of lines of code" can be considered. In this case, parameters such as priority and weight need to be adjusted accordingly to ensure the accuracy and rationality of the evaluation results.

[0113] The method provided in the above embodiment designs a unified set of detection code quality standards to systematically evaluate code quality: by analyzing code attributes and matching parsers, an abstract syntax tree for code characteristics is generated. Based on this abstract syntax tree and cyclomatic complexity calculation method, cyclomatic complexity can be accurately calculated. Finally, the code quality is comprehensively analyzed by combining multi-dimensional indicators such as the number of lines of code, thereby providing more comprehensive and in-depth detection results. This set of standards not only helps developers quickly identify complexity and potential problems in the code, but also provides more detailed detection results by integrating data from multiple dimensions, thereby significantly improving the maintainability and readability of the code. In addition, an alarm is issued in a timely manner when the quality analysis results exceed the preset threshold, thereby promoting continuous improvement and optimization of the code.

[0114] See also Figure 2 , shows a schematic flow chart of an optional method for detecting code quality according to an embodiment of the present invention, comprising the following steps:

[0115] S201: Determine the reference relationship between functions, and mark the function call chain in the abstract syntax tree of the code file;

[0116] S202: determining a sub-abstract syntax tree of each function in the abstract syntax tree of the code file, marking a quality analysis result of each function and marking a decision node in each sub-abstract syntax tree;

[0117] S203: For each code line in each function, count the number of marked decision nodes, and then highlight the code line with the largest number of decision nodes.

[0118] In the above implementation, the existing tools have the problem of insufficient visualization. Although they provide cyclomatic complexity values, they lack intuitive visualization and are difficult to understand and optimize. After determining the quality analysis results of each function and comparing them with the threshold, this solution generates a code flow chart to clearly display the logical judgment process inside the function.

[0119] Match the corresponding parser based on the attribute information of the code file, and parse the code file to obtain the abstract syntax tree (AST). For example, the code file contains four functions ABCD, and the generated AST of the code file will include the sub-ASTs of these four functions. In the large AST, one or more of the cyclomatic complexity and quality analysis results are annotated for each function, and the decision node is annotated. In addition, there may be a calling relationship between functions, such as A calls B, B calls C, and C calls D. These function call chains will be annotated in the AST of the code file.

[0120] In addition, for each line of code in the function, the number of annotated decision nodes can be counted to highlight the line of code with the largest number of decision nodes. Through the flow chart, you can intuitively understand the code structure, even for functions with high cyclomatic complexity, you can intuitively display its structure. The plug-in also considers the ratio of cyclomatic complexity to the number of lines of code, weighted summation, etc. to evaluate code efficiency and encourage writing small, pure functions with fewer lines and clearer logic.

[0121] It should be noted that the above-mentioned marking methods can be different and can be adjusted as needed. For example, different colors or icons can be used to represent different quality analysis results and decision nodes. For example, green can be used to represent low cyclomatic complexity functions, yellow to represent medium cyclomatic complexity functions, and red to represent high cyclomatic complexity functions; different shapes or colors of marks can be used to represent different types of decision nodes. In addition, more detailed information can be provided through pop-up windows or tooltips to help developers better understand and optimize the code.

[0122] The method provided in the above embodiment can clearly display the structure and quality status of the code by analyzing the reference relationship and quality analysis results between functions in the code file. By marking the decision nodes in the abstract syntax tree and highlighting the key code lines, developers can quickly identify potential problems and parts with high complexity, so as to conduct code review and optimization more effectively. This visual approach helps to improve the maintainability and readability of the code and promote team collaboration.

[0123] See also Figure 3 , shows a schematic flow chart of another optional method for detecting code quality according to an embodiment of the present invention, comprising the following steps:

[0124] S301: In response to a quality analysis result of a code file being less than a preset threshold, generating a page based on the code file;

[0125] S302: In response to the access operation to the page, obtaining the execution time of each decision node in each function, and determining the time interval in which the execution time falls;

[0126] S303: Determine a highlighting mode pre-set for each time-consuming interval, and highlight each decision node in the abstract syntax tree of the code file.

[0127] In the above implementation, the existing tools have the problem of insufficient customization. Although the cyclomatic complexity can be calculated, the problem of a specific line of code cannot be located. In response to this problem, this solution sets up a subsequent use mechanism to determine the execution logic time of each execution node through page access, and then determine the line of code with the highest probability of problems. Specifically, a page is generated based on the code file, and the user accesses the page. The page performs user operations, such as clicking, browsing, jumping to the next page, displaying options, etc. The judgment node here needs to execute the corresponding logic according to the user operation. A buried point is set in the page, and the execution time of each judgment node is obtained through the buried point. In order to facilitate subsequent processing, the time-consuming interval in which the execution time is located can be determined; the highlighting mode set in advance for each time-consuming interval is determined, and each judgment node is highlighted in the abstract syntax tree of the code file.

[0128] The method provided in the above embodiment can provide intuitive visual feedback for the quality analysis results of the code file. By generating a page and recording the execution time of the decision node in each function, it can help developers quickly identify performance bottlenecks and potential optimization points. Highlighting the decision nodes in different time-consuming intervals makes the problem more obvious, thereby improving the performance and maintainability of the code, and helping the development team to perform performance tuning and code improvement more effectively.

[0129] See also Figure 4 , shows a schematic flow chart of a method for detecting code quality according to an embodiment of the present invention, comprising the following steps:

[0130] 1. The plug-in supports customization and opens up customization capabilities, such as setting supported code file types, such as Vue / React (i.e. target file types), TypeScript, JavaScript, setting quality analysis result level information, and setting warning information for each level; such as setting preset thresholds for each preset dimension.

[0131] 2. Traverse the project files according to the rules and find the code files that need to be detected, such as full code files or incremental code files. Existing tools only support full code, not incremental code. Among them, project: In software development, a project usually refers to a complete application or software system, which contains multiple files and components. JS, TSX, Vue, these file types are different files that may be included in the project, and they are usually used to build the front-end part of a web application.

[0132] 3. Identify code files of types such as JS, TSX, Vue, etc. Existing tools do not support processing Vue code files.

[0133] 4. Extract the function part in the code file, such as the SwitchStatement node: deep traversal to count the number of cases, ConditionalExpression node: represents the ternary operator, which needs special processing, and the expression containing &&: the number of left and right sub-expressions in the if statement needs to be counted. Existing tools need to detect classes and functions, resulting in repeated calculations. This solution only performs cyclomatic complexity detection on the function part to unify the standards.

[0134] 5. For each decision node encountered, increase the value of the counter to obtain the cyclomatic complexity.

[0135] 6. Calculate the quality analysis results based on one or more of cyclomatic complexity and number of lines of code. Existing tools do not consider the impact of the number of lines of code in a single function. In addition, you can also set the level of quality analysis results, such as using different annotation methods to mark the level of code quality, such as green - passed the test, yellow - can be modified or not, red - warning, black - serious warning.

[0136] 7. Output JSON statistics files, and output function code flow charts based on JSON files. In the flow chart, the logical judgment process is clear at a glance, and logical judgment is usually represented by branches. Each branch corresponds to a conditional statement (i.e., judgment node) in the code, such as different conditions of if statements. Developers can quickly locate the corresponding position in the code by viewing the branches in the flow chart, so as to understand the execution logic of the code. Existing tools do not have this function.

[0137] 8. Convert the JSON file into an HTML report template to determine the call time when the code is running. This solution can combine the code runtime data to determine the time-consuming decision nodes, which is not available in existing tools.

[0138] The embodiment of the present invention provides a tool (or plug-in) for detecting code quality, which can not only accurately analyze the quality of the code, but also provide targeted optimization suggestions to help developers improve the code quality. Compared with the existing technology, the tool has the following advantages:

[0139] 1. Support multiple file types: Support TypeScript, JavaScript, Vue / React file types, which solves the problem that existing tools do not support Vue files. At the same time, it realizes the detection of incremental code, allowing developers to customize the detection scope, thereby realizing the quality analysis of incremental code.

[0140] 2. Comprehensive cyclomatic complexity detection: Cyclomatic complexity calculation depends on the number of decision nodes, which are located in the abstract syntax tree. This solution considers the code characteristics, matches the parser based on the code attributes, and generates an abstract syntax tree for the code characteristics through the parser to improve the accuracy of cyclomatic complexity. In addition to cyclomatic complexity detection of functions in code files, the number of function code lines is also considered, so as to build a set of unified code quality detection standards from a multi-dimensional perspective to help developers identify and optimize complex parts in the code.

[0141] 3. Enhanced visualization tools: Provide more intuitive visualization functions, such as interactive flowcharts, support the output of single function internal logic flowcharts, to clearly display the internal logic judgment process of the function, making it easier for developers to understand and optimize complex code. At the same time, evaluate code efficiency and encourage the writing of small / pure functions with fewer lines and clearer logic.

[0142] 4. Runtime performance feedback: When the code quality test passes, a runtime code running time feedback mechanism is introduced to display the execution time of each decision node in the code flow chart, further helping developers optimize performance.

[0143] 5. Personalized Configuration Wizard: An easy-to-use configuration wizard is designed to help developers customize the content of configuration items according to project characteristics, and provide specific refactoring suggestions or automatic refactoring functions to improve code quality.

[0144] At present, the code quality detection tool provided by the embodiment of the present invention can be applied to multiple scenarios, such as the Commit Gate embedded in coding to realize the quality detection of incremental / full code. Among them, the Commit Gate refers to the process that must pass a series of automated checks and verifications before the code is submitted to the version control system (such as Git). These checks usually include code quality detection, unit testing, code style checks, static code analysis, etc. Only when all checks are passed can the code be successfully submitted and merged. The main purpose of the Commit Gate is to ensure code quality and stability and prevent problematic code from entering the main branch. For example, by installing a plug-in, it is embedded in front-end general tools such as vscode to implement quality detection support for functions in special file types such as Vue / React.

[0145] See also Figure 5 , which shows a schematic diagram of main modules of a device 500 for detecting code quality provided by an embodiment of the present invention, including:

[0146] A matching module 501 is used to obtain a code file to be tested for quality, and match a parser according to attribute information of the code file; wherein the code file is a full code file or an incremental code file;

[0147] The cyclomatic complexity module 502 is used to process the code file using the parser to obtain an abstract syntax tree; traverse the abstract syntax tree and determine the cyclomatic complexity of the code file by counting and determining the number of nodes;

[0148] The quality analysis module 503 is used to count the number of code lines of the code file, determine the quality analysis result of the code file according to one or more of the cyclomatic complexity and the number of code lines, and trigger an alarm in response to the quality analysis result being greater than or equal to a preset threshold.

[0149] In the implementation device of the present invention, the matching module 501 is used to:

[0150] In response to the file type being a target file type, determining a preconfigured first parser according to a programming language type of the code file and the target file type;

[0151] In response to the file type not being the target file type, the number of code lines of the code file is counted to determine a code line range in which the number of code lines falls; and based on the programming language type, the code line range and one or more of the file type, a second parser with the shortest time consumption is determined from a preset parser set.

[0152] The implementation device of the present invention also includes a pre-processing module, which is used to:

[0153] Determine a target test code file; wherein different target test code files are set for different programming language types, different code line ranges, and different file types;

[0154] Use each parser in the preset parser set to process the target test code file to obtain the time consumption distribution at different time points, and use a preset method to process the time consumption distribution to obtain the time consumption of each parser.

[0155] The implementation device of the present invention also includes a configuration module, which is used to:

[0156] Determine a configuration item of a code quality detection tool, and receive a file type set in the configuration item; wherein the file type includes a target file type and other file types;

[0157] Receive the quality analysis result level information set in the configuration item, as well as the alarm information set for each level;

[0158] Receives the preset thresholds set in the configuration item for each preset dimension.

[0159] In the implementation device of the present invention, the cyclomatic complexity module 502 is used to:

[0160] Traversing each node in the abstract syntax tree, and in response to each node being a decision node, increasing the value of the counter by 1;

[0161] In response to the end of the traversal, the sum of the value of the counter and the preset value is used as the cyclomatic complexity of the code file.

[0162] In the implementation device of the present invention, the quality analysis module 503 is used to:

[0163] Calculating a ratio of the cyclomatic complexity to the number of lines of code;

[0164] Performing weighted processing on the cyclomatic complexity and the number of lines of code to obtain a weighted sum value;

[0165] Determine the number of nodes determined by statistics of the quality analysis result of the code file according to one or more of the cyclomatic complexity, the number of code lines, the ratio, or the weighted sum value.

[0166] In the implementation device of the present invention, the code file includes one or more functions, the device is used to detect the quality of each function, and the device also includes a code flow chart module for:

[0167] Determine the reference relationship between functions, and mark the function call chain in the abstract syntax tree of the code file;

[0168] In the abstract syntax tree of the code file, determine a sub-abstract syntax tree of each function, and mark the quality analysis result of each function and the decision node in each sub-abstract syntax tree;

[0169] For each code line in each function, the number of marked decision nodes is counted, and the code line with the largest number of decision nodes is highlighted.

[0170] In the implementation device of the present invention, the code flow chart module is also used for:

[0171] In response to the quality analysis result being less than the preset threshold, generating a page based on the code file;

[0172] In response to the access operation to the page, the execution time of each decision node in each function is obtained, and the execution time interval is determined;

[0173] A highlighting mode pre-set for each time-consuming interval is determined, and each decision node in the abstract syntax tree of the code file is highlighted.

[0174] In addition, the specific implementation content of the device described in the embodiment of the present invention has been described in detail in the method described above, so the repeated content will not be described again here.

[0175] Figure 6 An exemplary system architecture 600 to which embodiments of the present invention may be applied is shown, including terminal devices 601 , 602 , 603 , a network 604 and a server 605 (only an example).

[0176] Terminal devices 601, 602, 603 can be various electronic devices with display screens and supporting web browsing, and various communication client applications are installed. Users can use terminal devices 601, 602, 603 to interact with server 605 through network 604 to receive or send messages, etc.

[0177] The network 604 is used to provide a medium for communication links between the terminal devices 601, 602, 603 and the server 605. The network 604 may include various connection types, such as wired, wireless communication links or optical fiber cables.

[0178] The server 605 may be a server that provides various services, such as a backend management server that provides support for shopping websites browsed by users using the terminal devices 601, 602, and 603 (for example only). The backend management server may analyze and process the received data such as product information query requests, and feed back the processing results (such as target push information, product information - for example only) to the terminal device. It should be noted that the method provided in the embodiment of the present invention is generally executed by the server 605, and accordingly, the device is generally set in the server 605.

[0179] It should be understood that Figure 6 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.

[0180] Reference below Figure 7 , which shows a schematic diagram of the structure of a computer system 700 of a terminal device suitable for implementing an embodiment of the present invention. Figure 7 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0181] like Figure 7 As shown, the computer system 700 includes a central processing unit (CPU) 701, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 702 or a program loaded from a storage part 708 into a random access memory (RAM) 703. In the RAM 703, various programs and data required for the operation of the system 700 are also stored. The CPU 701, the ROM 702, and the RAM 703 are connected to each other through a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.

[0182] The following components are connected to the I / O interface 705: an input section 706 including a keyboard, a mouse, etc.; an output section 707 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 708 including a hard disk, etc.; and a communication section 709 including a network interface card such as a LAN card, a modem, etc. The communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the I / O interface 705 as needed. A removable medium 711, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 710 as needed, so that a computer program read therefrom is installed into the storage section 708 as needed.

[0183] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program contains program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 709, and / or installed from the removable medium 711. When the computer program is executed by the central processing unit (CPU) 701, the above-mentioned functions defined in the system of the present invention are executed.

[0184] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0185] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0186] The modules involved in the embodiments of the present invention may be implemented by software or hardware. The modules described may also be set in a processor, for example, they may be described as: a processor including a matching module, a cyclomatic complexity module, and a quality analysis module. The names of these modules do not, in some cases, constitute limitations on the modules themselves, for example, the quality analysis module may also be described as an "alarm module".

[0187] As another aspect, the present invention further provides a computer-readable medium, which may be included in the device described in the above embodiment; or may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device executes any of the above methods for detecting code quality.

[0188] The computer program product of the present invention comprises a computer program, and when the computer program is executed by a processor, the method for detecting code quality in the embodiment of the present invention is implemented.

[0189] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A method for detecting code quality, characterized in that: include: Obtain a code file whose quality is to be tested, and match a parser according to the attribute information of the code file; wherein the code file is a full code file or an incremental code file; Process the code file using the parser to obtain an abstract syntax tree; Traversing the abstract syntax tree, and determining the cyclomatic complexity of the code file by counting and determining the number of nodes; The number of code lines of the code file is counted, and a quality analysis result of the code file is determined according to one or more of the cyclomatic complexity and the number of code lines. In response to the quality analysis result being greater than or equal to a preset threshold, an alarm is triggered.

2. The method according to claim 1, characterized in that The matching parser according to the attribute information of the code file includes: In response to the file type being a target file type, determining a preconfigured first parser according to a programming language type of the code file and the target file type; In response to the file type not being the target file type, the number of code lines of the code file is counted to determine a code line range in which the number of code lines falls; and based on one or more of the programming language type, the code line range and the file type, a second parser with the shortest time consumption is determined from a preset parser set.

3. The method according to claim 2, characterized in that The method further comprises: Determine a target test code file; wherein different target test code files are set for different programming language types, different code line ranges, and different file types; Use each parser in the preset parser set to process the target test code file to obtain the time consumption distribution at different time points, and use a preset method to process the time consumption distribution to obtain the time consumption of each parser.

4. The method according to claim 2 or 3, characterized in that: The method further comprises: Determine a configuration item of a code quality detection tool, and receive a file type set in the configuration item; wherein the file type includes a target file type and other file types; Receive the quality analysis result level information set in the configuration item, as well as the alarm information set for each level; Receives the preset thresholds set in the configuration item for each preset dimension.

5. The method according to claim 1, characterized in that The traversing the abstract syntax tree and determining the cyclomatic complexity of the code file by counting and determining the number of nodes includes: Traversing each node in the abstract syntax tree, and in response to each node being a decision node, increasing the value of the counter by 1; In response to the end of the traversal, the sum of the value of the counter and the preset value is used as the cyclomatic complexity of the code file.

6. The method according to claim 1 or 5, characterized in that: Determining the quality analysis result of the code file according to one or more of the cyclomatic complexity and the number of code lines includes: Calculating a ratio of the cyclomatic complexity to the number of lines of code; Performing weighted processing on the cyclomatic complexity and the number of lines of code to obtain a weighted sum value; A quality analysis result of the code file is determined according to one or more of the cyclomatic complexity, the number of code lines, the ratio, or the weighted sum value.

7. The method according to claim 1 or 5, characterized in that: The code file includes one or more functions, and the method is used to detect the quality of each function, and the method further includes: Determine the reference relationship between functions, and mark the function call chain in the abstract syntax tree of the code file; In the abstract syntax tree of the code file, determine a sub-abstract syntax tree of each function, and mark the quality analysis result of each function and the decision node in each sub-abstract syntax tree; For each code line in each function, the number of marked decision nodes is counted, and the code line with the largest number of decision nodes is highlighted.

8. The method according to claim 7, characterized in that The method further comprises: In response to the quality analysis result being less than the preset threshold, generating a page based on the code file; In response to the access operation to the page, the execution time of each decision node in each function is obtained, and the execution time interval is determined; A highlighting mode pre-set for each time-consuming interval is determined, and each decision node in the abstract syntax tree of the code file is highlighted.

9. A device for detecting code quality, characterized in that: include: A matching module, used to obtain a code file whose quality is to be tested, and match a parser according to the attribute information of the code file; wherein the code file is a full code file or an incremental code file; A cyclomatic complexity module, configured to process the code file using the parser to obtain an abstract syntax tree; traverse the abstract syntax tree and determine the cyclomatic complexity of the code file by using a strategy for counting and determining the number of nodes; A quality analysis module is used to count the number of code lines of the code file, determine the quality analysis result of the code file according to one or more of the cyclomatic complexity and the number of code lines, and trigger an alarm in response to the quality analysis result being greater than or equal to a preset threshold.

10. An electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 8.

11. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.

12. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.

Citation Information

Patent Citations

  • Detection method and detection system for JavaScript script file

    CN104866412A

  • Multilevel change analysis-based software evolution assessment system and method

    CN108509338A

  • Software test defect evaluation method and device, computer equipment and storage medium

    CN110083514A

  • Branch coverage rate test method, coverage rate test method, electronic equipment and storage medium

    CN116257448A

Cited By

  • Programming assistance method based on generative AI, computer equipment and storage medium

    CN120010825A

  • Programming Assistance Method, Computer Device, and Storage Medium Based on Generative AI

    CN120010825B

  • Real-time code review processing method and system and related equipment

    CN120743725A