Code quality evaluation method and device, equipment, medium and chip
By using an intelligent code quality assessment method based on the GPT language model, combined with static code analysis, a code quality report is generated, which solves the problems of low efficiency and strong subjectivity in traditional manual review. This achieves more efficient and accurate code quality assessment, and improves the maintainability and reliability of the code.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-28
- Publication Date
- 2026-04-10
AI Technical Summary
Traditional manual code review is inefficient and susceptible to subjective factors, and cannot effectively measure program complexity. Existing rule-based analysis methods cannot comprehensively evaluate code quality.
An intelligent code quality assessment method based on the GPT language model is adopted, combined with static code analysis, to generate a code quality report, including complexity information, testability information, and error information. The code is then analyzed and evaluated using the GPT model.
It enables smarter and more accurate code quality assessment, improves assessment efficiency, reduces the subjectivity of manual review, and enhances code maintainability, readability, and reliability.
Smart Images

Figure CN121833449A_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of software engineering technology, specifically relating to a code quality assessment method, apparatus, device, medium, and chip. Background Technology
[0002] In recent years, software code quality has become a crucial focus in the field of software engineering. High-quality code not only affects the reliability and maintainability of software systems but also directly impacts project development efficiency and business value. Therefore, enterprises and developers are increasingly emphasizing code quality management.
[0003] Traditional code review and quality checks often rely on manual evaluation, which is inefficient and susceptible to human error. Specifically, traditional manual code review is inefficient and easily influenced by subjective factors. Rule-based analysis methods, on the other hand, rely on a set of predefined rules or patterns to detect potential problems in the code. They can perform code style checks, syntax safety checks, and ensure that the code writing style conforms to industry best practices. However, rule-based analysis methods also have limitations, as they cannot effectively measure program complexity, which is the most important indicator of code quality. Summary of the Invention
[0004] The purpose of this application is to provide a code quality assessment method, apparatus, device, medium, and chip that can solve the above-mentioned technical problems.
[0005] Git is a distributed code version control software used in the field of software engineering to enable collaborative development by multiple users.
[0006] SSH (Secure Shell protocol) is an encrypted network transmission protocol. Using this protocol in insecure networks can ensure the security of network transmission services.
[0007] Cyclomatic complexity, also known as conditional complexity or cyclomatic complexity, is a metric for measuring program complexity, and is symbolized as VG or M.
[0008] A control flow graph (CFG) is a representation in computer science that uses mathematical graph representations to depict all paths traversed during the execution of a computer program. CFG is a core technology in many compiler optimization and static program analysis tools.
[0009] Cognitive complexity is a metric for evaluating code readability and maintainability. It reflects how easily the complexity of the code structure makes it easier for programmers to understand and modify the code.
[0010] The Maintenance Complexity Index (MCI) is a metric used to evaluate code maintainability. It takes into account factors such as code complexity, readability, and scalability, providing developers with a standard for measuring code quality.
[0011] Code coverage is a commonly used software testing metric used to measure the extent to which test cases cover the source code.
[0012] In recent years, high-level language models like GPT have made significant progress in natural language understanding. Applying these models to code analysis promises to achieve more intelligent and accurate code quality assessment. However, mature GPT-based code quality analysis solutions are currently lacking in the market.
[0013] Therefore, developing a GPT-based intelligent code quality assessment model will compensate for the shortcomings of related methods, improve the automation level of code quality management, and become an innovative direction worth exploring, enabling more efficient code quality analysis and optimization.
[0014] To solve the above-mentioned technical problems, this application is implemented as follows: In a first aspect, embodiments of this application provide a code quality assessment method, the method comprising: In response to a client's code evaluation request, retrieve the original code from the code repository; Without running the original code, perform static code analysis on the original code to obtain a static analysis report of the original code; the static analysis report includes: complexity information, testability information, error information, and function impact factor of the original code; The original code and the static analysis report are input into a pre-trained code quality assessment model to obtain the coding quality assessment result of the original code; the code quality assessment model is trained on the GPT language model using open-source code; Based on the static analysis report and the coding quality assessment results, a code quality report of the original code is generated. The code quality report is used to provide real-time feedback to the code development terminal so that the code development terminal can optimize the original code to obtain optimized original code.
[0015] Optionally, the original code and the static analysis report are input into a pre-trained code quality assessment model to obtain the coding quality assessment results of the original code, including: The original code and static analysis report are input into the code quality assessment model in text form; The original code is parsed using the code quality assessment model to extract the syntax tree of the original code, and the code quality features of the original code are obtained based on the syntax tree; the code quality features include at least: code structure, naming conventions, comment quality, and code complexity; The code quality assessment model is used to compare the code quality features of the original code with the reference code quality features learned by the model to obtain the similarity analysis results of the original code; the reference code quality features are the code quality features of open source code. Based on the similarity analysis results obtained through the code quality assessment model, the coding quality assessment results of the original code are obtained, and the coding quality assessment results include at least a code quality score and improvement suggestions.
[0016] Optionally, the method further includes: Based on the code quality reports of multiple evaluated codes and the manual evaluation reports of the multiple evaluated codes, the optimization requirements of the code quality evaluation model are determined; Based on the code quality reports of multiple evaluated codes, the multiple evaluated codes are divided into positive examples and negative examples, wherein the quality of the positive examples is higher than that of the negative examples. Based on the code quality characteristics of the multiple evaluated codes, the positive and negative examples are labeled with their respective types. Based on the type-annotated positive and negative examples, and combined with the optimization requirements of the code quality assessment model, a sample set is constructed. The code quality assessment model is optimized using the sample set to obtain an optimized code quality assessment model.
[0017] Optionally, the method further includes: The original code contains several functions; Based on the function call relationships between the multiple functions, the number of times each function in the original code is called is determined; Based on the number of times each function is called in the multiple functions contained in the original code, the function influence factor of each function in the multiple functions contained in the original code is determined, and the function influence factor is used to represent the importance of the function.
[0018] Optionally, the method further includes: Determine the loop complexity, cognitive complexity, and maintenance complexity of the original code; Based on the loop complexity, the cognitive complexity, and the maintenance complexity, the complexity information of the original code is determined; The method further includes: The original code is tested according to multiple testability metrics to obtain testability information of the original code; The testability metrics include at least: complexity information, coupling, cohesion, and test coverage.
[0019] Optionally, the method further includes: Before the original code is run, potential errors in the original code are identified. The types of potential errors include at least: uninitialized variables, null pointer references, array out-of-bounds errors, resource leaks, and security vulnerabilities. The potential errors in the original code are statistically analyzed according to their types to obtain error information of the original code. The error information is used to indicate the type of potential error in the original code and the frequency of occurrence of different types of potential errors in the original code.
[0020] Secondly, embodiments of this application provide a code quality assessment apparatus, the apparatus comprising: The source code retrieval module is used to retrieve source code from the code repository in response to a client's code evaluation request; The static code analysis module is used to perform static code analysis on the original code without running it, and obtain a static analysis report of the original code; the static analysis report includes: complexity information, testability information, error information, and function impact factor of the original code; The coding quality assessment result acquisition module is used to input the original code and the static analysis report into a pre-trained code quality assessment model to obtain the coding quality assessment result of the original code; the code quality assessment model is trained on the GPT language model using open-source code; The code quality report generation module is used to generate a code quality report for the original code based on the static analysis report and the coding quality assessment results. The code quality report is used to provide real-time feedback to the code development terminal so that the code development terminal can optimize the original code to obtain optimized original code.
[0021] Thirdly, embodiments of this application provide an electronic device including a processor, a memory, and a program or instructions stored in the memory and executable on the processor, wherein the program or instructions, when executed by the processor, implement the steps of the method described in the first aspect.
[0022] Fourthly, embodiments of this application provide a computer-readable storage medium on which a program or instructions are stored, which, when executed by a processor, implement the steps of the method described in the first aspect.
[0023] Fifthly, embodiments of this application provide a chip, the chip including a processor and a communication interface, the communication interface being coupled to the processor, the processor being used to run programs or instructions to implement the method as described in the first aspect.
[0024] This application provides a code quality assessment method, apparatus, device, medium, and chip. The method includes: responding to a client's code assessment request, obtaining raw code from a code repository; performing static code analysis on the raw code without running it, and obtaining a static analysis report of the raw code; the static analysis report includes: complexity information, testability information, error information, and function impact factor of the raw code; inputting the raw code and the static analysis report into a pre-trained code quality assessment model to obtain a coding quality assessment result of the raw code; the code quality assessment model is trained using open-source code on a GPT language model; and generating a code quality report of the raw code based on the static analysis report and the coding quality assessment result, the code quality report being used to provide real-time feedback to a code development terminal so that the code development terminal can optimize the raw code to obtain optimized raw code.
[0025] This application's technical solution, combining static code analysis and the GPT language model, provides an automated and intelligent code quality assessment method. Static code analysis generates a report containing complexity, testability, error information, and function impact factors. The original code and the static analysis report are input into a code quality assessment model trained using the GPT language model. This model, trained on a large amount of open-source code, learns from best coding practices, thus assessing code quality more intelligently and accurately, and generating code quality assessment results. Finally, based on the static analysis report and the code quality assessment results, a code quality report is generated and fed back to the code development terminal in real time, helping developers understand the code quality status and optimize it promptly. This improves the efficiency of code quality assessment, reduces the subjectivity of manual review, and enhances software maintainability, readability, and reliability through code optimization. Attached Figure Description
[0026] Figure 1 This is a flowchart illustrating a code quality assessment method provided in one embodiment of this application; Figure 2 This is a flowchart of a code quality assessment method provided in an embodiment of this application; Figure 3 This is a schematic diagram illustrating the technical principle of downloading source code from a remote code repository based on the SSH protocol, according to an embodiment of this application. Figure 4 This is a flowchart of a cognitive complexity algorithm provided in an embodiment of this application; Figure 5 This is a complete flowchart of a code quality assessment method provided in one embodiment of this application; Figure 6 This is a schematic diagram of the framework of a code quality assessment device provided in one embodiment of this application. Detailed Implementation
[0027] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0028] The terms "first," "second," etc., used in the specification and claims of this application are used to distinguish similar objects and not to describe a specific order or sequence. It should be understood that such use of data can be interchanged where appropriate so that embodiments of this application can be implemented in orders other than those illustrated or described herein. Furthermore, in the specification and claims, "and / or" indicates at least one of the connected objects, and the character " / " generally indicates that the preceding and following objects are in an "or" relationship.
[0029] The present application will now be described in detail with reference to the accompanying drawings, through specific embodiments and application scenarios.
[0030] Figure 1 This is a flowchart illustrating a code quality assessment method according to an embodiment of this application. (Refer to...) Figure 1 , Figure 2 This is a flowchart illustrating a code quality assessment method according to an embodiment of this application. The code quality assessment method provided in this embodiment includes steps S11 to S14: Step S11: In response to the client's code evaluation request, retrieve the original code from the code repository.
[0031] In this embodiment, a code evaluation request is first received from a client. The client is an Integrated Development Environment (IDE), code management tool, or any other software tool capable of initiating code quality evaluation requests. When the client initiates a code evaluation request, it uses the address of the code repository specified in the request and related parameters (such as the address branch, file path of the file containing the original code, etc.). A common version control system is Git. The technical principle of downloading the original code from a remote code repository based on the SSH protocol is as follows... Figure 3 As shown, the SSH protocol can be used to retrieve the source code to be evaluated from a remote code repository. The source code refers to the code requested for evaluation by the client.
[0032] Step S12: Without running the original code, perform static code analysis on the original code to obtain a static analysis report of the original code; the static analysis report includes: complexity information, testability information, error information, and function impact factor of the original code.
[0033] In this embodiment, after obtaining the original code, static code analysis is performed on the obtained original code without running the code. This includes analyzing the code's syntax and structure to obtain code quality characteristics. The code quality characteristics obtained from static code analysis include code complexity information (such as loop complexity, cognitive complexity, etc.), testability information (such as code readability, maintainability, etc.), error information (such as potential errors (syntax errors, logical errors)), and function impact factor. The function impact factor is an indicator that measures the importance of a function in the entire code, calculated by counting the number of function calls.
[0034] Step S13: Input the original code and the static analysis report into the pre-trained code quality assessment model to obtain the coding quality assessment result of the original code; the code quality assessment model is trained on the GPT language model using open source code.
[0035] In this embodiment, the original code and static analysis report are input into a pre-trained code quality assessment model. This model is trained based on the GPT language model, an advanced language model capable of understanding and generating natural language text. During training, the model learns the characteristics and patterns of excellent code by using a large amount of high-quality open-source code as training data. When the original code and static analysis report are input, the model combines this information to evaluate the code quality of the original code and outputs the coding quality assessment result. The evaluation process considers not only the code's syntax and structure but also information such as complexity and testability provided in the static analysis report. The resulting coding quality assessment includes a code quality score and improvement suggestions, helping developers understand the current quality status of the code and providing specific optimization directions. The code quality assessment model trained using the GPT language model can assess code quality more intelligently and accurately.
[0036] Step S14: Based on the static analysis report and the coding quality assessment results, generate a code quality report for the original code. The code quality report is used to provide real-time feedback to the code development terminal so that the code development terminal can optimize the original code to obtain optimized original code.
[0037] In this embodiment, a code quality report is generated based on the static analysis report and the coding quality assessment results. The code quality report is a comprehensive report containing information such as the original code's quality score, a list of issues, and improvement suggestions. The code quality report can be provided to the code development terminal (IDE or other code management tool used by developers) in formats such as HTML and PDF. This allows developers on the code development terminal to optimize the original code, fix identified issues, and improve code quality based on the information in the code quality report. The real-time feedback mechanism of the code quality report ensures that developers can promptly understand the code quality status and take corresponding optimization measures. By optimizing the code, developers can improve the maintainability, readability, and reliability of the software, thereby improving the quality of the original code.
[0038] The technical solutions described in the above embodiments, and the technical solutions of this application, combine static code analysis and the GPT language model to provide an automated and intelligent code quality assessment method. Static code analysis generates a report containing complexity information, testability information, error information, and function impact factor magnitudes. The original code and the static analysis report are input into a code quality assessment model trained using the GPT language model. This model is trained using a large amount of open-source code and can learn from excellent coding practices, thereby assessing code quality more intelligently and accurately, and generating code quality assessment results. Finally, based on the static analysis report and the code quality assessment results, a code quality report is generated and fed back to the code development terminal in real time, helping developers to understand the code quality status and optimize it promptly. This improves the efficiency of code quality assessment, reduces the subjectivity of manual review, and also improves the maintainability, readability, and reliability of the software by optimizing the code.
[0039] In conjunction with the technical solutions of the above embodiments, an embodiment of this application also provides another code quality assessment method. In this method, step S13, "inputting the original code and the static analysis report into a pre-trained code quality assessment model to obtain the coding quality assessment result of the original code," specifically includes steps S13-1 to S13-4: Step S13-1: Input the original code and static analysis report into the code quality assessment model in text form.
[0040] In this embodiment, the raw code and static analysis report obtained from the code repository are input into a pre-trained code quality assessment model in text form. The raw code refers to the code requested for evaluation by the client; this code is stored in the code repository in text form. The static analysis report, generated in step S12, contains information such as code complexity, testability, error information, and function impact factor. After extraction, the raw code and static analysis report are then input into the code quality assessment model in text form.
[0041] Step S13-2: The original code is parsed using the code quality assessment model to extract the syntax tree of the original code, and the code quality features of the original code are obtained based on the syntax tree; the code quality features include at least: code structure, naming conventions, comment quality, and code complexity.
[0042] In this embodiment, the code quality assessment model parses the input raw code to generate a syntax tree (AST). A syntax tree is a tree-like structure that represents the syntactic structure of code, with each node representing an element in the code, such as a function, variable, or operator. Through the syntax tree, the code quality assessment model can more accurately extract various code quality features from the raw code. These features include the code structure (such as the nesting depth of functions and loops), naming conventions (whether variable and function names conform to naming conventions), comment quality (the quantity and quality of comments), and code complexity (such as loop complexity and cognitive complexity). These code quality features are important indicators for subsequent code quality evaluation, helping the model to more comprehensively understand the structure and quality of the code.
[0043] For example, the code quality assessment model parses the following original code: javapublic class Example { public int add(int a, int b) { if (a>0) { return a + b; } else { return a - b; } } } A syntax tree is generated based on the original code above, and the following features are extracted from the syntax tree: Code structure: Function nesting depth = 1; Naming conventions: The function name "add" conforms to the naming conventions; Comment quality: Number of comments = 0; Code complexity: Loop complexity = 2.
[0044] Step S13-3: Using the code quality assessment model, compare the code quality features of the original code with the reference code quality features learned by the code quality assessment model to obtain the similarity analysis results of the original code; the reference code quality features are the code quality features of open source code.
[0045] In this embodiment, the code quality assessment model compares the extracted code quality features of the original code with the reference code quality features learned by the model. These reference code quality features are extracted from a large amount of high-quality open-source code, which has been extensively validated by the community and is considered excellent coding practice. Through comparison, the code quality assessment model calculates the similarity between the code quality features of the original code and the reference code. The similarity analysis result represents the degree of similarity between the original code and the reference code, and can be represented by a numerical value. A higher value indicates a higher similarity, meaning the quality of the original code is closer to the reference code, i.e., the better the code quality. Conversely, a lower value indicates a lower similarity, meaning the quality of the original code is inferior to the reference code, i.e., the worse the code quality. Through similarity analysis, the model can more accurately assess code quality.
[0046] For example, suppose the model learns the following reference code quality features from open-source code: Code structure: Function nesting depth = 1; Naming conventions: Function names conform to the naming conventions; Comment quality: Number of comments = 1; Code complexity: Loop complexity = 2.
[0047] The extracted features of the original code are compared with those of the reference code, and the similarity is calculated (the value ranges from 0 to 1, with higher values indicating higher similarity). The similarity analysis results of the original code are shown below: Code structure similarity: 1 (completely identical); Naming convention similarity: 1 (completely identical); Annotation quality similarity: 0.5 (due to a small number of annotations); Code complexity similarity: 0.7 (high code complexity).
[0048] Step S13-4: Using the code quality assessment model, based on the similarity analysis results, obtain the coding quality assessment results of the original code. The coding quality assessment results include at least a code quality score and improvement suggestions.
[0049] In this embodiment, the code quality assessment model generates a coding quality assessment result for the original code based on the obtained similarity analysis results. The coding quality assessment result includes at least a code quality score and improvement suggestions. The code quality score is calculated based on the similarity analysis results; the higher the similarity, the higher the code quality score. Improvement suggestions are generated based on the similarity analysis results and issues in the static analysis report, aiming to help developers optimize the code. Improvement suggestions may include adding comments, reducing complexity, and improving naming conventions. By providing these detailed assessment results and improvement suggestions, the model can help developers quickly understand the current quality status of the code and take corresponding optimization measures, thereby improving the overall code quality.
[0050] For example, a code quality score can be calculated by combining various similarities to obtain an overall similarity score (e.g., a weighted sum of the similarities across different components). This overall similarity score serves as the result of the similarity analysis between the original code and the reference code. Based on the example in step S13-3, assuming that the weights of code structure similarity, naming convention similarity, comment quality similarity, and code complexity similarity are all the same, all being 0.25, the code quality score is 1*0.25+1*0.25+0.5*0.25+0.7*0.25=0.8.
[0051] Based on the similarity analysis results above, the code quality assessment model can identify the following areas that need improvement and generate improvement suggestions, such as: The number of comments is too small; more comments need to be added. The loop has high complexity, and the code structure needs to be optimized.
[0052] In conjunction with the technical solutions of the above embodiments, an embodiment of this application also provides another code quality assessment method, which further includes steps S21 to S25: Step S21: Based on the code quality reports of multiple evaluated codes and the manual evaluation reports of the multiple evaluated codes, determine the optimization requirements of the code quality evaluation model.
[0053] In this embodiment, code quality reports and corresponding human evaluation reports for multiple evaluated code snippets are first collected. The code quality reports are generated by the code quality assessment model and include information such as code quality scores and improvement suggestions. The human evaluation reports, on the other hand, are generated by professional developers or code review experts after evaluating the same code snippet, and typically contain more in-depth analysis and recommendations. By comparing these two reports, the differences between the code quality assessment model's evaluation results and the human evaluation results can be identified. These differences reflect problems in the code quality assessment model, allowing for the determination of optimization needs based on these issues. For example, if the code quality score obtained by the model from evaluating certain original code differs significantly from the code quality score in the human evaluation results, it is necessary to further determine which code quality features the model identifies and compares in these original code snippets (e.g., the model's assessment of code structure similarity is inaccurate) or if the model fails to identify the problems pointed out in the human evaluation report. These situations indicate that the model needs optimization, and optimization requirements need to be formulated based on these problems.
[0054] For example, suppose we have collected the following two evaluation reports: a code quality report and a human evaluation report for the original code: Original code A: Code quality report: Score 85, suggestion to add comments.
[0055] Human evaluation report: Score 80 points, suggestions include adding comments and optimizing loop complexity.
[0056] Original code B: Code quality report: Score 90, no suggestions for improvement.
[0057] Manual evaluation report: Score 85 points, it is recommended to initialize uninitialized variables.
[0058] By comparing the above reports, it can be determined that the model needs optimization in detecting loop complexity and uninitialized variables.
[0059] Step S22: Based on the code quality reports of multiple evaluated codes, divide the multiple evaluated codes into positive examples and negative examples, wherein the quality of the positive examples is higher than that of the negative examples.
[0060] In this embodiment, after determining the optimization requirements, multiple evaluated code snippets (i.e., the original evaluated code) are divided into positive examples and negative examples based on the quality score in the code quality report or other relevant indicators. Positive examples refer to code segments with high quality scores that meet high-quality standards; negative examples refer to code segments with low quality scores that have obvious problems.
[0061] For example, suppose that multiple evaluated pieces of code are divided into the following two categories based on the code quality report: Positive example: Original code C: Score 95 points, clear code structure, standardized naming, and detailed comments.
[0062] Counterexample: Original code D: Score 60 points, contains uninitialized variables, has high loop complexity, and lacks comments.
[0063] Step S23: According to the code quality characteristics of each of the multiple evaluated codes, the positive example samples and the negative example samples are labeled with types respectively.
[0064] In this embodiment, positive and negative examples are labeled with their respective code quality characteristics. Code quality characteristics include code structure, naming conventions, comment quality, and code complexity. The labeling process associates these characteristics with specific code problems or strengths. For example, labeling a piece of code as having excessively high loop complexity (labeled as: negative example, excessively high loop complexity), or labeling a piece of code as having a function with non-standard naming conventions (labeled as: negative example, non-standard naming conventions). This labeling information will be used as part of the training data to help the model learn the relationship between different types of code features and quality.
[0065] For example, suppose the system labels positive and negative samples with the following types: Positive examples: Original code E: Code structure: clear; Naming conventions: Conforms; Annotation quality: detailed; Code complexity: Low.
[0066] Counterexample: Original code F: Code structure: complex; Naming conventions not met; Annotation quality: Poor; Code complexity: High.
[0067] Step S24: Based on the positive and negative examples after type labeling, and in conjunction with the optimization requirements of the code quality assessment model, a sample set is constructed.
[0068] In this embodiment, the type-labeled positive and negative samples are combined to construct a complete sample set. This sample set will serve as training data for model optimization. When constructing the sample set, the optimization requirements specified in step S21 are taken into account to ensure that the sample set covers all aspects that the model needs to optimize.
[0069] For example, if the model is inadequate in identifying code with excessively high loop complexity, the sample set will include more negative examples labeled with loop complexity issues. In this way, the model can be optimized for these problems during subsequent training, improving the overall performance and accuracy of the model.
[0070] Step S25: Optimize the code quality assessment model using the sample set to obtain the optimized code quality assessment model.
[0071] In this embodiment, the code quality assessment model is optimized using a pre-constructed sample set. The optimization process typically involves adjusting the model's parameters to improve its ability to identify high-quality code and detect low-quality code issues. The optimized model can more accurately assess code quality and generate more accurate code quality reports and improvement suggestions. Through continuous optimization, the model can be continuously improved, enhancing the accuracy and reliability of code quality assessment.
[0072] In conjunction with the technical solutions of the above embodiments, an embodiment of this application also provides another code quality assessment method, which further includes steps S31 to S33: Step S31: Identify the multiple functions contained in the original code.
[0073] In this embodiment, the original code is first parsed to identify all the functions contained in the original code.
[0074] Step S32: Based on the function call relationship between the multiple functions, determine the number of times each function in the original code is called.
[0075] In this embodiment, function A is crucial if it is called by multiple other functions. This is because errors or performance issues with function A will affect other functions that reference it. Therefore, the call relationships between the functions in the original code are analyzed, and the number of times each function is called is counted. Specifically, the function call relationships can be determined by analyzing the control flow graph (CFG) of the original code. A control flow graph is a tool that graphically represents the code execution path and clearly shows the call relationships between multiple functions in the original code.
[0076] Step S33: Based on the number of times each function is called in the multiple functions contained in the original code, determine the function influence factor of each function in the multiple functions contained in the original code. The function influence factor is used to represent the importance of the function.
[0077] In this embodiment, the function impact factor is calculated for each function based on the number of times it is called. The function impact factor is an indicator of a function's importance in the overall code. The more times a function is called, the larger its impact factor, indicating that the function plays a more important role in the original code. The function impact factor can be calculated based on the simple number of calls, or it can be combined with other factors (such as function complexity, call depth, etc.) for a comprehensive calculation.
[0078] Functions with larger impact factors have higher priority in subsequent optimization processes and will also receive special attention during the testing of the original code.
[0079] In conjunction with the technical solutions of the above embodiments, an embodiment of this application also provides another code quality assessment method, which further includes steps S41 to S42 and step S43: Step S41: Determine the loop complexity, cognitive complexity, and maintenance complexity of the original code.
[0080] In this embodiment, the original code is first analyzed to calculate its loop complexity, cognitive complexity, and maintenance complexity. These complexity metrics are important standards for measuring code quality, reflecting the code's readability, maintainability, and potential error risks.
[0081] Loop complexity measures the number of control flow paths in the code. It is calculated based on the control flow graph (CFG) and represents the complexity of branches and loops in the original code. The higher the loop complexity, the lower the testability and maintainability of the original code.
[0082] Cognitive complexity measures how difficult it is for humans to understand code. It reflects the logical structure of the original code, such as nested conditional statements and loops. The higher the cognitive complexity, the lower the readability of the original code.
[0083] Maintenance complexity is used to comprehensively consider factors such as the complexity, readability, and scalability of the original code to evaluate its maintainability. The higher the maintenance complexity, the higher the maintenance cost of the original code.
[0084] Specifically, loop complexity is a commonly used metric for measuring program complexity, typically based on a control flow graph. The control flow graph is used to graphically represent the control flow structure of a program; it forms the basis of static raw code analysis, and many complexity metrics are calculated based on control flow graphs. The control flow structure in the raw code can be modeled using a directed graph: The control flow structure in the original code can be modeled using a directed graph: ; in, A set of nodes represents program statements; The edge represents the set of edges, indicating the control flow. Indicates from statement It can be controlled to transfer to statements .
[0085] The formula for the cyclic complexity model is: ; in, This represents the number of connected components in the control flow graph, and is usually equal to 1. The value represents the loop complexity of the original code, reflecting the complexity of the control flow. Generally speaking, The higher the value, the greater the complexity of the original code, and the higher the testing and maintenance costs.
[0086] The design goal of cognitive complexity is to better reflect the cognitive load required for humans to understand code. Unlike metrics such as loop complexity, which are based on control flow graphs, cognitive complexity focuses more on the readability and understandability of code. Its algorithm uses lexical and syntactic analysis to accumulate statistical values according to defined rules. The algorithm flowchart is shown below. Figure 4 As shown: Keyword matching rules in the flowchart: The basic statistical value is 1; For each switch / case / catch statement, the statistic is incremented by 1; For each nested block (such as if, for, while statements), the statistical value is incremented by 1; For each break or continue statement, the statistic is incremented by 1. The higher the final calculated cognitive complexity value, the lower the readability of the original code.
[0087] Maintenance complexity is a metric used to evaluate code maintainability. It comprehensively considers factors such as code complexity, readability, and scalability, providing developers with a standard for measuring code quality. Its mathematical formula is as follows:
[0088] in, Indicates the loop complexity; This represents the average number of lines of code per function; This represents the vocabulary size used to measure the amount of information in the original code, and its calculation formula is as follows:
[0089] in, ; ; The number of times each operation meets the criteria; The total number of occurrences of all operands; For the number of distinct operators; The number of distinct operands. The value range is [0, 100], and the higher the value, the better the maintainability of the code.
[0090] Step S42: Based on the loop complexity, the cognitive complexity, and the maintenance complexity, determine the complexity information of the original code.
[0091] In this embodiment, the complexity information of the original code is generated by combining loop complexity, cognitive complexity, and maintenance complexity. Complexity information is a comprehensive metric used to fully assess the complexity of the original code. By combining these complexity metrics, the readability, maintainability, and potential error risks of the original code can be more accurately determined.
[0092] The method further includes: Step S43: Perform testability analysis on the original code according to multiple testability metrics to obtain testability information of the original code.
[0093] The testability metrics include at least: complexity information, coupling, cohesion, and test coverage.
[0094] In this embodiment, the original code is analyzed based on multiple testability metrics to generate testability information. These metrics include complexity, coupling, cohesion, and test coverage. These metrics reflect the testability of the code and help developers assess the difficulty of testing the code and potential testing risks.
[0095] The complexity information includes loop complexity, cognitive complexity, and maintenance complexity, which are used to assess the complexity of the original code.
[0096] Coupling is used to measure the dependencies between classes or functions. The lower the coupling, the better the testability of the original code.
[0097] Cohesion measures the degree of aggregation within a class or function. Higher cohesion generally indicates better testability of the original code.
[0098] Test coverage measures which pieces of code are executed during testing, evaluating the breadth and depth of the tests. Higher test coverage indicates better testability of the original code. The formula for test coverage is: .
[0099] Specifically, the meanings of each testability indicator are as follows: Loop complexity: measures the complexity of code. The higher the loop complexity, the lower the testability. Cognitive complexity: measures code readability; the higher the cognitive complexity, the lower the testability. Maintenance complexity: measures code maintainability; the higher the maintenance complexity, the lower the testability. Coupling: Inter-class coupling = number of methods called between classes / total number of methods; Inter-method coupling = number of statements called between methods / total number of statements. The lower the coupling, the better the testability. Cohesion: Intra-class cohesion = number of shared variables in the class / number of methods in the class; Intra-method cohesion = number of variables accessed within the method / number of statements in the method. The higher the cohesion, the better the testability. Test coverage: Represents the breadth and depth of testing. The higher the coverage, the better the testability.
[0100] In conjunction with the technical solutions of the above embodiments, an embodiment of this application also provides another code quality assessment method, which further includes steps S51 to S52: Step S51: Before the original code is run, potential errors in the original code are identified. The types of potential errors include at least: uninitialized variables, null pointer references, array out-of-bounds errors, resource leaks, and security vulnerabilities.
[0101] In this embodiment, potential errors in the code are identified through static code analysis without running the original code. The types of potential errors include, but are not limited to, the following: Uninitialized variable: The variable was not properly initialized before use, which may cause runtime errors.
[0102] Null pointer reference: Attempting to access an uninitialized or undealtized object may cause the program to crash.
[0103] Array out of bounds: Accessing an array outside its valid index range can lead to unpredictable behavior.
[0104] Resource leaks: Failure to properly release allocated resources (such as file handles, memory, etc.) may lead to the exhaustion of system resources.
[0105] Security vulnerabilities: Security issues in the code, such as SQL injection and cross-site scripting (XSS) attacks, may lead to data leakage or system attacks.
[0106] Step S52: Statistically analyze the potential errors in the original code according to their types to obtain error information of the original code. The error information is used to indicate the type of potential errors in the original code and the frequency of occurrence of different types of potential errors in the original code.
[0107] In this embodiment, identified potential errors are categorized and statistically analyzed to generate error information for the original code. The error information includes the type of each potential error and its frequency of occurrence in the code. This error information helps developers quickly understand the problems in the original code and prioritize resolving frequently occurring errors.
[0108] Figure 5 This is a complete flowchart of a code quality assessment method provided in one embodiment of this application, see reference. Figure 5 After generating a static analysis report from the original code's complexity, testability, error information, and function impact factor, the report is stored in a vector database and then processed using a code quality assessment model (corresponding to...). Figure 5 The code quality is further evaluated using the GPT code quality assessment system in China, generating a code quality report of the original code. This report is presented in tabular form, as shown in Table 1 below. Table 1
[0109] The code quality score is a comprehensive measure that considers factors such as the complexity, testability, and potential error detection of the original code. Additionally, for functions with lower code quality, a function influence factor is introduced during scoring; functions with larger influence factors have a significant impact on the quality of their original code. After the code quality report is generated, developers use the problem list and improvement suggestions in the report to make changes and improve code quality.
[0110] This application aims to leverage large-scale language models such as GPT to achieve more intelligent and accurate code quality analysis and optimization, providing software developers with timely and effective feedback and optimization suggestions. This will not only help improve software code quality but also further promote the deep integration and application of artificial intelligence technology in the field of software engineering, which is of great significance for improving software development efficiency and quality.
[0111] Specifically, the beneficial effects of this application are as follows: Large-scale pre-trained language models such as GPT are used for deep semantic understanding and analysis of code. Compared with traditional rule-based or statistical model-based methods, this approach enables more intelligent and accurate code quality assessment.
[0112] Develop an end-to-end code quality analysis and feedback system to automate evaluation and optimization suggestions. Improve the efficiency of code quality management and reduce the cost and limitations of manual review.
[0113] The analysis results are fed back to developers in real time, helping them to promptly identify and fix code quality issues. This helps developers continuously optimize code quality and improve software reliability and maintainability.
[0114] This involves applying cutting-edge GPT language modeling technology to code analysis scenarios. It aims to promote the deep integration of artificial intelligence into all stages of the software development lifecycle and enhance the level of digital transformation in the industry.
[0115] This patent invention provides an impact factor analysis method based on function reference counting. This method counts all function references in a software program and evaluates the impact factor of each function based on the count of function references.
[0116] Figure 6 This is a schematic diagram of the framework of a code quality assessment device provided in an embodiment of this application, with reference to... Figure 6 One embodiment of this application provides a code quality assessment apparatus, which includes 11 to 14: The source code acquisition module 11 is used to retrieve source code from the code repository in response to a client's code evaluation request; The static code analysis module 12 is used to perform static code analysis on the original code without running the original code, and obtain a static analysis report of the original code; the static analysis report includes: complexity information, testability information, error information, and function impact factor of the original code; The coding quality assessment result acquisition module 13 is used to input the original code and the static analysis report into a pre-trained code quality assessment model to obtain the coding quality assessment result of the original code; the code quality assessment model is trained on the GPT language model using open source code; The code quality report generation module 14 is used to generate a code quality report of the original code based on the static analysis report and the coding quality assessment results. The code quality report is used to provide real-time feedback to the code development terminal so that the code development terminal can optimize the original code to obtain optimized original code.
[0117] Optionally, the coding quality assessment result acquisition module 13 includes: An input unit is used to input the original code and static analysis report into the code quality assessment model in text form; The code quality feature acquisition unit is used to parse the original code through the code quality assessment model, extract the syntax tree of the original code, and obtain the code quality features of the original code based on the syntax tree; the code quality features include at least: code structure, naming conventions, comment quality, and code complexity; The similarity analysis result acquisition unit is used to compare the code quality features of the original code with the reference code quality features learned by the code quality assessment model through the code quality assessment model, and obtain the similarity analysis result of the original code; the reference code quality features are the code quality features of open source code; The coding quality assessment result acquisition unit is used to obtain the coding quality assessment result of the original code based on the similarity analysis result through the code quality assessment model. The coding quality assessment result includes at least a code quality score and improvement suggestions.
[0118] Optionally, the device further includes: The optimization requirement determination module is used to determine the optimization requirements of the code quality assessment model based on the code quality reports of multiple evaluated codes and the manual evaluation reports of the multiple evaluated codes. The sample acquisition module is used to divide the multiple evaluated codes into positive examples and negative examples based on the code quality reports of multiple evaluated codes, wherein the quality of the positive examples is higher than that of the negative examples. The sample labeling module is used to label the positive example samples and the negative example samples according to the code quality characteristics of the multiple evaluated codes, respectively. The sample set construction module is used to construct a sample set based on the type-annotated positive sample and the type-annotated negative sample, combined with the optimization requirements of the code quality assessment model; The model optimization module is used to optimize the code quality assessment model using the sample set to obtain an optimized code quality assessment model.
[0119] Optionally, the device further includes: The function determination module is used to determine the multiple functions contained in the original code; The call count determination module is used to determine the call count of each function among the multiple functions contained in the original code based on the function call relationship between the multiple functions; The function impact factor determination module is used to determine the function impact factor of each function among the multiple functions contained in the original code based on the number of times each function is called. The function impact factor is used to represent the importance of the function.
[0120] Optionally, the device further includes: The complexity determination module is used to determine the loop complexity, cognitive complexity, and maintenance complexity of the original code; The complexity information determination module is used to determine the complexity information of the original code based on the loop complexity, the cognitive complexity, and the maintenance complexity. The device further includes: The testability information determination module is used to perform testability analysis on the original code according to multiple testability indicators to obtain the testability information of the original code; The testability metrics include at least: complexity information, coupling, cohesion, and test coverage.
[0121] Optionally, the device further includes: The potential error determination module is used to determine the potential errors in the original code before it is run. The types of potential errors include at least: uninitialized variables, null pointer references, array out-of-bounds errors, resource leaks, and security vulnerabilities. An error information determination module is used to statistically analyze the potential errors in the original code according to their types, and obtain the error information of the original code. The error information is used to indicate the type of potential errors in the original code and the frequency of occurrence of different types of potential errors in the original code.
[0122] It should be noted that the code quality assessment method provided in this application embodiment can be executed by a code quality assessment device, or a control module within the code quality assessment device for executing the loading code quality assessment method. This application embodiment uses the execution of the loading code quality assessment method by a code quality assessment device as an example to illustrate the code quality assessment method provided in this application embodiment.
[0123] The code quality assessment device in this application embodiment can be a device, or it can be a component, integrated circuit, or chip in a terminal. The device can be a mobile electronic device or a non-mobile electronic device. For example, mobile electronic devices can be mobile phones, tablets, laptops, PDAs, in-vehicle electronic devices, wearable devices, ultra-mobile personal computers (UMPCs), netbooks, or personal digital assistants (PDAs), etc., while non-mobile electronic devices can be servers, network-attached storage (NAS), personal computers (PCs), televisions (TVs), ATMs, or self-service machines, etc. This application embodiment does not impose specific limitations.
[0124] The code quality assessment device in this application embodiment can be a device with an operating system. This operating system can be Android, iOS, or other possible operating systems; this application embodiment does not specifically limit it.
[0125] The code quality assessment device provided in this application embodiment can achieve... Figures 1 to 5 The various processes implemented by the code quality assessment device in the method embodiment will not be described again here to avoid repetition.
[0126] Optionally, this application embodiment also provides an electronic device. It should be noted that the electronic device in this application embodiment includes the mobile electronic device and non-mobile electronic device described above.
[0127] The electronic device includes, but is not limited to, components such as: radio frequency unit, network module, audio output unit, input unit, sensor, display unit, user input unit, interface unit, memory, and processor.
[0128] Those skilled in the art will understand that electronic devices may also include a power supply (such as a battery) that supplies power to various components. The power supply can be connected to the processor logic through a power management system, thereby enabling functions such as managing charging, discharging, and power consumption through the power management system. Figure 6 The electronic device structure shown does not constitute a limitation on the electronic device. The electronic device may include more or fewer components than shown, or combine certain components, or have different component arrangements, which will not be elaborated here. As an example, such as Figure 6As shown, the electronic device 600 includes a memory 610 and a processor 620. The memory 610 and the processor 620 are connected via a bus for communication. The memory 610 stores a computer program, which can run on the processor 620 to implement the steps in the code quality assessment method disclosed in the above embodiments of this application.
[0129] As the apparatus is basically similar to the method embodiment, it is described in a relatively simple way. For relevant details, please refer to the description of the method embodiment.
[0130] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. The same or similar parts between the various embodiments can be referred to each other.
[0131] Those skilled in the art will understand that embodiments of this application can be provided as methods, apparatus, or computer program products. Therefore, embodiments of this application can take the form of entirely hardware embodiments, entirely software embodiments, or embodiments combining software and hardware aspects.
[0132] Furthermore, this application embodiment also provides a readable storage medium storing a program or instructions. When the program or instructions are executed by a processor, they implement the various processes of the above-described code quality assessment method embodiment and achieve the same technical effect. To avoid repetition, they will not be described again here.
[0133] The processor is the processor in the electronic device described in the above embodiments. The readable storage medium includes computer-readable storage media, such as computer read-only memory (ROM), random access memory (RAM), magnetic disk, or optical disk.
[0134] This application also provides a chip, which includes a processor and a communication interface. The communication interface is coupled to the processor. The processor is used to run programs or instructions to implement the various processes of the above-described code quality assessment method embodiments and can achieve the same technical effect. To avoid repetition, it will not be described again here.
[0135] It should be understood that the chip mentioned in the embodiments of this application may also be referred to as a system-on-a-chip, system chip, chip system, or system-on-a-chip, etc.
[0136] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element. Furthermore, it should be noted that the scope of the methods and apparatuses in the embodiments of this application is not limited to performing functions in the order shown or discussed, but may also include performing functions substantially simultaneously or in the reverse order, depending on the functions involved. For example, the described methods may be performed in a different order than described, and various steps may be added, omitted, or combined. Additionally, features described with reference to certain examples may be combined in other examples.
[0137] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal (which may be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in the various embodiments of this application.
[0138] The embodiments of this application have been described above with reference to the accompanying drawings. However, this application is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of this application without departing from the spirit and scope of the claims, and all of these forms are within the protection scope of this application.
Claims
1. A code quality assessment method, characterized in that, The method includes: In response to a client's code evaluation request, retrieve the original code from the code repository; Without running the original code, perform static code analysis on the original code to obtain a static analysis report of the original code; the static analysis report includes: complexity information, testability information, error information, and function impact factor of the original code; The original code and the static analysis report are input into a pre-trained code quality assessment model to obtain the coding quality assessment result of the original code; the code quality assessment model is trained on the GPT language model using open-source code; Based on the static analysis report and the coding quality assessment results, a code quality report of the original code is generated. The code quality report is used to provide real-time feedback to the code development terminal so that the code development terminal can optimize the original code to obtain optimized original code.
2. The code quality assessment method according to claim 1, characterized in that, The original code and the static analysis report are input into a pre-trained code quality assessment model to obtain the coding quality assessment results of the original code, including: The original code and static analysis report are input into the code quality assessment model in text form; The original code is parsed using the code quality assessment model to extract the syntax tree of the original code, and the code quality features of the original code are obtained based on the syntax tree; the code quality features include at least: code structure, naming conventions, comment quality, and code complexity; The code quality assessment model is used to compare the code quality features of the original code with the reference code quality features learned by the model to obtain the similarity analysis results of the original code; the reference code quality features are the code quality features of open source code. Based on the similarity analysis results obtained through the code quality assessment model, the coding quality assessment results of the original code are obtained, and the coding quality assessment results include at least a code quality score and improvement suggestions.
3. The code quality assessment method according to claim 2, characterized in that, The method further includes: Based on the code quality reports of multiple evaluated codes and the manual evaluation reports of the multiple evaluated codes, the optimization requirements of the code quality evaluation model are determined; Based on the code quality reports of multiple evaluated codes, the multiple evaluated codes are divided into positive examples and negative examples, wherein the quality of the positive examples is higher than that of the negative examples. Based on the code quality characteristics of the multiple evaluated codes, the positive and negative examples are labeled with their respective types. Based on the type-annotated positive and negative examples, and combined with the optimization requirements of the code quality assessment model, a sample set is constructed. The code quality assessment model is optimized using the sample set to obtain an optimized code quality assessment model.
4. The code quality assessment method according to claim 1, characterized in that, The method further includes: The original code contains several functions; Based on the function call relationships between the multiple functions, the number of times each function in the original code is called is determined; Based on the number of times each function is called in the multiple functions contained in the original code, the function influence factor of each function in the multiple functions contained in the original code is determined, and the function influence factor is used to represent the importance of the function.
5. The code quality assessment method according to claim 1, characterized in that, The method further includes: Determine the loop complexity, cognitive complexity, and maintenance complexity of the original code; Based on the loop complexity, the cognitive complexity, and the maintenance complexity, the complexity information of the original code is determined; The method further includes: The original code is tested according to multiple testability metrics to obtain testability information of the original code; The testability metrics include at least: complexity information, coupling, cohesion, and test coverage.
6. The code quality assessment method according to claim 1, characterized in that, The method further includes: Before the original code is run, potential errors in the original code are identified. The types of potential errors include at least: uninitialized variables, null pointer references, array out-of-bounds errors, resource leaks, and security vulnerabilities. The potential errors in the original code are statistically analyzed according to their types to obtain error information of the original code. The error information is used to indicate the type of potential error in the original code and the frequency of occurrence of different types of potential errors in the original code.
7. A code quality assessment device, characterized in that, The device includes: The source code retrieval module is used to retrieve source code from the code repository in response to a client's code evaluation request; The static code analysis module is used to perform static code analysis on the original code without running it, and obtain a static analysis report of the original code; the static analysis report includes: complexity information, testability information, error information, and function impact factor of the original code; The coding quality assessment result acquisition module is used to input the original code and the static analysis report into a pre-trained code quality assessment model to obtain the coding quality assessment result of the original code; the code quality assessment model is trained on the GPT language model using open-source code; The code quality report generation module is used to generate a code quality report for the original code based on the static analysis report and the coding quality assessment results. The code quality report is used to provide real-time feedback to the code development terminal so that the code development terminal can optimize the original code to obtain optimized original code.
8. An electronic device, characterized in that, It includes a processor, a memory, and a program or instructions stored in the memory and executable on the processor, wherein the program or instructions, when executed by the processor, implement the steps of the code quality assessment method as described in any one of claims 1-6.
9. A readable storage medium, characterized in that, The program or instructions are stored on the readable storage medium, and when the program or instructions are executed by a processor, they implement the steps of the code quality assessment method as described in any one of claims 1-6.
10. A chip, characterized in that, The chip includes a processor and a communication interface, the communication interface being coupled to the processor, the processor being used to run programs or instructions to implement the steps of the code quality assessment method as described in any one of claims 1-6.