CN119938061B - Code quality optimization method, device and storage medium based on causal reasoning and LLM - Google Patents
Code quality optimization method, device and storage medium based on causal reasoning and LLM Download PDFInfo
- Publication number
- CN119938061B CN119938061B CN202510436553.XA CN202510436553A CN119938061B CN 119938061 B CN119938061 B CN 119938061B CN 202510436553 A CN202510436553 A CN 202510436553A CN 119938061 B CN119938061 B CN 119938061B
- Authority
- CN
- China
- Prior art keywords
- code
- complexity
- optimization
- causal
- maintainability
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
Landscapes
- Stored Programmes (AREA)
Abstract
The embodiment of the invention discloses a code quality optimization method, a device and a storage medium based on causal reasoning and LLM, and relates to the technical field of artificial intelligence. The invention builds a causal relation model by analyzing various influencing factors in codes through causal reasoning. Factors that truly affect code quality are identified by the causal relationship model, rather than just relying on dependencies in the historical data. And the code modification affecting the code quality is realized, and the optimization scheme conforming to the corresponding code style can be automatically generated, so that the time and energy of engineers are saved.
Description
Technical Field
The invention relates to the technical field of artificial intelligence, in particular to a code quality optimization method, a code quality optimization device and a code quality optimization storage medium based on causal reasoning and LLM.
Background
With long-term technology accumulation, artificial intelligence technology has been rapidly developed in recent two years, and one of the main achievements is Large Language Models (LLM), such as GPT, deepSeek, grok, which are already on-line. The large language model is essentially an analysis tool, so that various enterprises and institutions have put great cost into the training work of the large language model at present, and a further problem is how to apply the large language model to specific practical applications, so that the problems in the practical production process are solved, and the operation efficiency in related application scenes is improved.
One of the main possible application scenarios is currently in analysis optimization for codes. In the prior art, analysis and optimization of program codes usually need a large amount of personnel to participate, and although a large number of automated script programs are used as code analysis tools, the flexibility and the adaptability of the automated script programs are often insufficient, and finally only a part of auxiliary functions can be achieved, so that the labor cost is difficult to further save. For example, the current static analysis scheme (such as SonarQube) based on rules is very dependent on the fixed rules set by the senior engineers, and cannot adapt to different java development scenes. For another example, machine learning based code optimization, such schemes are based primarily on statistical correlations, whether they provide statistical results in fact, cannot be used to explain causal mechanisms for optimization decisions, or require significant time spent by engineers to derive specific optimization directions from statistical results. At present, the most advanced mode is that code optimization is carried out through LLM, but at present, although optimization suggestions can be provided, the mode matching is often based, whether the optimized code can cause new problems or not is difficult to judge, and engineers still need to spend a great deal of time to carry out insect capturing and obstacle removing work.
Therefore, the scheme of performing code optimization based on LLM needs to be developed secondarily so that the scheme not only depends on the correlation in the historical data, but also can identify code modification affecting the code quality, and can automatically generate an optimized scheme conforming to the corresponding code style, thereby saving time and effort of engineers.
Disclosure of Invention
The embodiment of the invention provides a code quality optimization method, device and storage medium based on causal reasoning and LLM, which can identify code modification influencing code quality and automatically generate an optimization scheme conforming to corresponding code style, thereby saving time and effort of engineers.
In order to achieve the above purpose, the embodiment of the present invention adopts the following technical scheme:
in a first aspect, an embodiment of the present invention provides a method for optimizing code quality based on causal reasoning and LLM, which is characterized by comprising:
s1, analyzing object code data and establishing a corresponding causal relationship model;
s2, simulating causal intervention effects of different optimization strategies on code quality by using the causal relation model;
s3, generating an optimization scheme according to the obtained simulation result by using the large language model;
S4, recording modification of the target code data, executing regression test and verifying optimization effect.
In a second aspect, an embodiment of the present invention provides a code quality optimization apparatus based on causal reasoning and LLM, which is characterized by comprising:
the analysis engine module is used for analyzing the target code data and establishing a corresponding causal relationship model;
the simulation module is used for simulating the causal intervention effect of different optimization strategies on the code quality by utilizing the causal relation model;
The scheme generating module is used for generating an optimization scheme according to the obtained simulation result by utilizing the large language model;
And the updating module is used for recording the modification of the target code data, executing regression test and verifying the optimization effect.
In a third aspect, embodiments of the present invention provide a storage medium storing a computer program or instructions that, when executed, implement the method of the present embodiment.
In the embodiment of the invention, various influencing factors in the code, such as code complexity, repetition rate, defect density and the like, are analyzed through causal reasoning, and a causal relation model (or called a causal relation network) is constructed. It is recognized by the causality model which Java code modifications actually affect the code quality, not just dependencies in the historical data. Moreover, in Java code optimization, customized optimization policies may be employed for different types of code modules, particularly core business code portions (e.g., java's network communications, database operations, service layer logic, etc.). For example, for high complexity modules, redundant code can be reduced, logic simplified, and maintainability improved, while for core business logic code optimization should focus on performance improvement and stability assurance, avoiding extensive reconfiguration and unnecessary modification to reduce the likelihood of introducing potential risks. Furthermore, in the optimization process, optimization suggestions conforming to the Java code style are generated by combining LLMs. Therefore, the purpose of identifying code modification affecting code quality and automatically generating an optimization scheme conforming to the corresponding code style is achieved, and time and energy of engineers are saved.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings that are needed in the embodiments will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and that other drawings can be obtained according to these drawings without inventive effort for an engineer of ordinary skill in the art.
FIG. 1 is a schematic flow chart of a method according to an embodiment of the present invention;
FIG. 2 is a schematic diagram of a device according to an embodiment of the present invention;
FIG. 3 is a network diagram of a training process according to an embodiment of the present invention;
FIG. 4 is a topology diagram of an algorithm operation provided by an embodiment of the present invention;
Fig. 5 is a schematic diagram of a specific example provided in an embodiment of the present invention.
Detailed Description
In order to enable a person skilled in the art to better understand the technical solutions of the present invention, the present invention is described in further detail below with reference to the accompanying drawings and detailed description. Embodiments of the present invention will hereinafter be described in detail, examples of which are illustrated in the accompanying drawings, wherein the same or similar reference numerals refer to the same or similar elements or elements having the same or similar functions throughout. The embodiments described below by referring to the drawings are exemplary only for explaining the present invention and are not to be construed as limiting the present invention. As used herein, the singular forms "a", "an", "the" and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It will be further understood that the terms "comprises" and/or "comprising," when used in this specification, specify the presence of stated features, integers, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or groups thereof. It will be understood that when an element is referred to as being "connected" or "coupled" to another element, it can be directly connected or coupled to the other element or intervening elements may also be present. Further, "connected" or "coupled" as used herein may include wirelessly connected or coupled. The term "and/or" as used herein includes any and all combinations of one or more of the associated listed items. It will be understood by those skilled in the art that all terms (including technical and scientific terms) used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention belongs unless defined otherwise. It will be further understood that terms, such as those defined in commonly used dictionaries, should be interpreted as having a meaning that is consistent with their meaning in the context of the prior art and will not be interpreted in an idealized or overly formal sense unless expressly so defined herein.
The Java code quality optimization method is mainly applied to Java code quality optimization, and is mainly designed to combine causal reasoning and LLM Java code quality optimization methods, the influence of Java code optimization on quality indexes is quantized by constructing a causal graph, and optimization suggestions are generated by means of LLM, so that efficient, accurate and interpretable Java code quality optimization is achieved. Wherein causal relationships between Java variables are analyzed by causal reasoning techniques to identify which code modifications actually affect code quality, rather than relying solely on dependencies in historical data. By combining LLM with causal reasoning, not only Java code optimization suggestions can be provided, but also the rationality of the suggestions can be interpreted, thereby helping developers to make more intelligent decisions.
In practical applications, the implementation process of this embodiment can be roughly divided into:
1. Firstly, analyzing Java code quality data, and extracting core indexes such as code complexity, maintainability, defect density and the like. These data may come from static analysis tools (such as SonarQube), code audit records, and historical project data. Causal relationship modeling-causal relationships between Java code quality factors are constructed using bayesian networks, structural Equation Models (SEM), or causal graphs. For example, the chain is affected by factors such as code complexity, repetition rate, function length, etc. And calculating causal influence, namely calculating influence of each factor on target variables (such as maintainability, code reliability and the like) through causal reasoning, and screening out key influence factors.
2. The causal intervention simulation adopts Do-algorithm (Do-Calculus) to simulate causal intervention effects of different optimization strategies on code quality. For example, modeling reduces code repetition rate or reduces the effect of functional complexity on maintainability and defect density. Counterfactual analysis the potential effects of different optimization schemes are evaluated by counterfactual analysis (CounterfactualAnalysis) and schemes that may lead to negative optimizations are excluded (e.g., some optimizations may lead to reduced code performance). Customizing optimization strategies, namely formulating more targeted optimization strategies according to different types of codes (such as core business codes, algorithm core modules, UI components and the like). For a module with higher complexity, code reconstruction and modularization can be optimized with emphasis, and for a core function module, large-scale modification is avoided, and the emphasis is placed on performance improvement and stability assurance.
3. Generating optimization suggestions by using LLM, namely generating an optimization scheme which accords with Java code style and best practice by using LLM (such as GPT, codeT5 and the like) based on causal reasoning results. LLM can propose more viable optimization suggestions based on contextual analysis. Optimization schemes explain that LLM provides detailed explanations when generating optimization suggestions, explaining the expected effects before and after optimization, possible side effects and how to balance different optimization objectives (e.g. trade-offs between maintainability and performance). Optimization report generation, namely generating a detailed report containing optimization strategies, effect expectations, potential risks and implementation suggestions, and helping developers understand the optimization process and results.
4. Code optimization execution and feedback, namely executing optimization, namely applying an optimization scheme generated by LLM to Java codes and carrying out regression test to ensure that the optimization does not influence the existing functions or introduce new problems. Quality assessment, verifying the optimization effect through a static analysis tool (such as SonarQube) or dynamic test, and ensuring that the optimized Java code meets the expected quality standard (such as maintainability, performance and the like). And (3) feeding back and learning, namely, LLM recording an optimization result, analyzing feedback of a developer, and dynamically adjusting an optimization strategy to improve accuracy and feasibility of follow-up optimization suggestions.
5. And (3) continuously improving, namely performing incremental learning, continuously feeding back and adjusting an optimization strategy according to historical optimization data, and continuously improving the intelligent level of an optimization model and optimization suggestions by combining actual use scenes and feedback of developers. And (3) evolution of an optimization strategy, namely gradually evolving the optimization strategy along with accumulation of more projects and optimization cases to form a more accurate and personalized optimization scheme.
In combination with practical application scenarios, the embodiment designs a preferred scheme of shaping, as shown in fig. 1, 3 and 4, including:
s1, analyzing the target code data and establishing a corresponding causal relationship model.
S2, simulating causal intervention effects of different optimization strategies on code quality by using the causal relation model.
In this embodiment, a more targeted optimization strategy is formulated according to different types of codes (such as core service codes, algorithm core modules, UI components, etc.), for modules with higher complexity, code reconstruction and modularization can be optimized with emphasis, and for core function modules, large-scale modification should be avoided, focusing on performance improvement and stability assurance. For example, core service codes refer to the portion of code that carries the core service logic. Typically, these codes have a high complexity and are directly related to the functionality and performance of the system. When optimizing core service codes, emphasis is on code reconstruction, maintainability improvement and performance optimization, but frequent and large-scale modification needs to be avoided so as to ensure the stability of service logic.
The specific optimization strategy comprises code reconstruction, namely finding out complex and lengthy parts by analyzing functions and classes in a core service module, and carrying out reconstruction. For example, a split long method, if a function is very complex, containing multiple responsibilities, should split into multiple small functions with a single responsibilities. Simplifying conditional statement, namely avoiding excessive nesting conditions, and simplifying complex business logic by using design modes such as strategy mode or state mode. The modularization is improved, namely the complex business logic is split into a plurality of small modules or services, so that the codes are easier to understand and modify. Performance optimization-core service modules typically need to process large amounts of data or perform high frequency calculations, and therefore, performance improvement needs to be focused on during optimization. Methods of optimization include algorithm optimization, checking the efficiency of existing algorithms, using more efficient algorithms (e.g., optimizing ranking algorithms, improving data queries, etc.). And (3) caching repeated calculation to avoid unnecessary repeated calculation.
The algorithm core module is a core code part for data processing, calculation or conversion in the system. When optimizing the algorithm module, the most important is to improve the efficiency of the algorithm, and especially in the scene of processing large-scale data or needing high real-time performance, the optimizing effect is particularly critical. The optimization strategy comprises optimizing algorithm complexity, namely searching for a more efficient algorithm by analyzing the time complexity and the space complexity of the existing algorithm. For example, alternative brute force search algorithms reduce redundant computation for hash table lookups, or using dynamic programming. The resource consumption is reduced, the memory use is optimized, and the memory leakage and the resource waste are avoided, especially when large data are processed. Parallel computing and distributed processing, namely, the computation-intensive tasks are processed in parallel or are processed in a distributed mode, so that the processing capacity is improved. And the stability and the robustness of the algorithm are that the algorithm runs stably under various boundary conditions, and the algorithm breakdown caused by the irregular input or extreme conditions is avoided.
The UI component is responsible for interacting with a user, and when codes of the UI component are optimized, response speed, interface fluency, user experience and maintainability are mainly focused. UI optimization not only reduces code complexity, but also improves user experience. The optimization strategy comprises the steps of improving the rendering efficiency, optimizing the rendering process for a complex UI interface, and avoiding unnecessary re-rendering. Performance bottlenecks in rendering may be reduced by virtualizing lists, lazy loads, or incremental rendering. And the DOM operation is reduced, namely, for front-end Web application, frequent operation on DOM is reduced, and rearrangement and redrawing of a large number of DOM elements caused by each interaction are avoided. UI componentization, namely splitting a complex UI interface into a plurality of reusable UI components, and improving maintainability and expansibility of codes. Responsive design, ensuring that the UI can run smoothly on different devices, adapting to different screen sizes and resolutions.
S3, generating an optimization scheme according to the obtained simulation result by using the large language model.
Wherein the large language model is trained using the code optimization dataset. Code optimization datasets are specifically designed for training large language models to generate, improve, and optimize code quality. This dataset contains multiple dimensions related to code optimization in order to help the large language model learn from it how to generate high quality code optimization strategies based on different project requirements and goals. The dataset will include a large amount of original code and optimized code comparisons. The code may come from different application scenarios, such as core business logic, algorithm core modules, UI components, etc. These data originate from open source code libraries, technical documents, and programming courses, ensuring that the model has high accuracy and utility in generating and understanding code. The code optimization dataset may also include various optimization strategies and implementations. These policies are directed to different types of code and modules. After each code is compared with the record, indexes related to the quality of the code, such as complexity, defect density and maintainability score are attached. Code optimization datasets also include examples of error repair and prevention, covering repair of common programming errors (e.g., memory leaks, null pointer exceptions, deadlocks, etc.) and enhancing robustness of the code, especially in highly concurrent, distributed environments. Large Language Models (LLMs) are commonly used in a variety of application scenarios, such as text generation, translation, dialog systems, and the like. In training these models, the training dataset used is mostly natural language text, whereas the code optimization dataset is unique in that it focuses on the structural, performance and quality optimization at the code level.
S4, recording modification of the target code data, executing regression test and verifying optimization effect.
And modifying the target code data according to the generated optimization scheme, performing regression testing and verifying the optimization effect. For example, a Large Language Model (LLM) will generate an optimization scheme based on the analyzed code optimization dataset and project objectives. The developer or an automation tool or a large language model is automatically optimized, and the target code is modified according to an optimization scheme.
Specifically, in S1 of the embodiment, the method includes the steps of obtaining core index information of the target code data, and establishing a causal relation model by utilizing the core index information, wherein the causal relation model is used for calculating influences of each core index on target variables.
Specifically, some key influencing factors are obtained after calculating the influence of each core index on a target variable (such as code quality). The subsequent roles of the key influencing factors include the design of the optimization strategy, resource allocation and priority setting. For example, if code complexity (C) is a key contributor and its increase has a negative impact on code quality, then the optimization strategy may be to reconstruct complex functions or split long methods to reduce the complexity of the code, thereby improving code quality. If defect density (d) is determined to be a critical factor and it has a negative impact on code quality, then the strategy may be to enhance code inspection and unit testing to reduce the number of defects and thereby improve code quality. If maintainability (M) is a key factor affecting code quality and its improvement can maximize the improvement in code quality over other factors, then the development team should prioritize the maintainability of the code. After the optimization scheme is generated through the S3, the technical development team can concentrate resources on key influencing factors such as code complexity and defect density, excessive time and resources are wasted on other variables with smaller influence, and meanwhile, a computer records modification of technical staff on the target code data. In the subsequent development process, key influencing factors can also be used as monitoring and evaluation indexes. The development team can continuously monitor the changes in these factors and adjust the development and optimization strategies based on the changes. By continuous evaluation of the key influencing factors, a feedback loop can be formed, ensuring continuous improvement and constant adjustment of the optimization strategy. The development team may use the model to predict the effects of intervention after implementing certain optimization strategies. By predicting how key influencing factors in the model change, a research and development team can predict future code quality at the beginning of a project and make appropriate interventions.
It should be noted that, in this embodiment, the optimization scheme generated by S3 may be directly provided to technicians of the development team in text form, and after the technicians modify the code, the computer records the modification of the technician for the target code data and executes S4 to pass the regression test and verify the optimization effect.
Specifically, the acquiring the core index information of the target code data includes scanning a code library of the target code data through a static analysis tool (such as SonarQube), acquiring the code complexity of the target code data, and determining maintainability and defect density, wherein the indexes of the corresponding code complexity include acquiring the cycle complexity, function complexity and nesting depth of codes of corresponding modules, classes and methods from the target code, wherein the cycle complexity is expressed as V (G) =e-n+2p, the function complexity is expressed as fc=l+b, the nesting depth N' is expressed as the number of layers of the largest nested conditional statement or cyclic statement, V (G) is expressed as the number of loops of the graph (i.e. the cycle complexity), E is the number of sides in the control flow graph, N is the number of nodes in the control flow graph, P is the number of independent communication areas in the program (usually 1) and represents the main area of the program), fc is the function complexity, L is the number of lines in the function (e.g. not including lines and B is the number of empty statements in the function, and the number of branches is annotated.
The loop complexity (CyclomaticComplexity) is measured by measuring the number of independent paths in the code, and McCabeCyclomaticComplexity index is usually used. Higher complexity values may mean that the code is difficult to understand and test. Function complexity (FunctionComplexity) the number of code lines, branching and looping structures, etc. inside the function are measured, larger functions usually mean higher complexity, suggesting splitting into multiple smaller functions. Nesting depth (NESTINGDEPTH) the nesting depth of the conditional statement and the loop statement is measured. Deep nesting increases the understanding and maintenance difficulties of code, especially when debugging. Code repetition (CodeDuplication) repeated codes increase the complexity of the code because it makes it necessary to change the code at multiple points when modified, which is prone to maintenance errors.
The loop complexity is used to measure the number of independent paths in the code. By analyzing the control flow graph, the number of paths in the program is calculated, thereby deriving the complexity of the code. For each function or method, its control flow graph (ControlFlowGraph, CFG) is analyzed, the number of edges and nodes therein is calculated, and the complexity is calculated by substituting the formula. If the complexity value is too high (typically greater than 10), the code needs to be split or reconstructed. For example, in the process of calculating the loop complexity V (G) =e-n+2p=2, if there are two main nodes in the object code, such as a while (true) loop condition judgment, the operations in the loop (memory allocation, printing, suspension). Therefore, the node number N=2 of the control flow graph can be obtained, if the loop condition in the object code judges that there is one side in E, the operation (memory allocation, printing, suspension and the like) in the loop body is also one side, so that the side number E=2 of the control flow graph, and if the structure of the program is a single infinite loop without other independent areas, the independent communication area number P=1. For another example, assuming that the control flow graph of a function contains 7 nodes, 8 edges, and the function is a single connected region (p=1), its loop complexity is V (G) =8-7+2 (1) =3.
The complexity of the function mainly considers the size of the function (number of code lines) and its internal control structure. It can be evaluated by calculating the number of rows of the function and some other measure. The function is statically analyzed, and the number of code lines (L) and the number of branches (B) are counted. If the number of rows of the function exceeds a set threshold (e.g., 20 rows), or there are too many branches, it should be considered to split or simplify it. For example, if the main structure in the target code is an infinite loop, the number of main lines of the code after the blank line and the annotation is removed is 6 lines, namely, l=6, and if there is only one while (true) loop in the code and there is no other conditional branch (such as if statement, switch statement, etc.), b=1, and the function complexity fc=l+b=7 is obtained. For another example, a function contains 25 lines of code and has 4 conditional branches (if, else), its complexity is fc=25+4=29.
The nesting depth measures the level of conditional statements or loop statements in the code. Excessive nesting depth typically means that the logic of the code is difficult to track, increasing the complexity of maintenance: N '=max (NESTING DEPTH of all branches), N' being the maximum nesting depth. The nesting depth refers to the number of layers of the most nested conditional statement or loop statement. Specifically, each control flow is analyzed, the nesting depth of each branch and cycle is calculated, and the maximum value is found. If the nesting depth exceeds a set threshold (e.g., layer 3), the optimized code structure should be considered. For example, if there is only one infinite loop while (true), there is no other nested loop or conditional statement, then the nesting depth N' =1. For another example, it is assumed that a function has a nesting depth of 3 for a first layer if (condition 1), a second layer if (condition 2), and a third layer for (i=0, i <10, i++), indicating that the function has a high complexity.
Code repetition refers to the existence of multiple repeated code blocks in a program. This not only increases the code complexity, but may also lead to maintenance difficulties and errors of d= Number of duplicate lines/Total lines of code x 100%, D being the code repetition. The "number of repetition lines" refers to the total number of code lines that occur multiple times. The "total number of code lines" is the number of code lines for the entire item or module. Specifically, code analysis is performed to find out repeated code blocks, and the repetition degree is calculated. If the repetition rate is too high (typically over 10%), then the repetition code needs to be eliminated by decimation functions or modularization. For example, assuming that the total number of code lines of an item is 1000 lines, where 120 lines are repeated codes, the repetition degree is d=120/1000×100+=12%, if the repetition degree is too high, it is recommended to reconstruct and abstract the repeated codes.
In this embodiment, to comprehensively evaluate the complexity and quality of the code, a composite score may be given by combining multiple complexity indexes. The code complexity index is expressed as c=w1×v (G) +w2×fc+w3×n' +w4×d, w1, w2, w3, w4 are weight coefficients corresponding to the 4 code complexity indexes, wherein the preferred scheme is shown in table 1.
TABLE 1
| Item scene type | Complexity of cycle (w 1) | Complexity of function (w 2) | Nesting depth (w 3) | Code repetition level (w 4) | Interpretation of the drawings |
| Rapid development project (prototype, MVP) | 0.2 | 0.3 | 0.2 | 0.3 | Development speed is priority, complexity and maintainability requirements are low, and repeated codes are acceptable. |
| Enterprise-level application (Long-term maintenance system) | 0.3 | 0.4 | 0.2 | 0.1 | Maintainability and code quality are important, complexity and code repeatability are low, and stability is the most critical. |
| Performance optimization project (high performance computing) | 0.4 | 0.2 | 0.3 | 0.1 | The performance optimization is core, complexity and nesting depth affect performance, and function complexity and code repetition are minor. |
| Rapid iteration product (SaaS, internet application) | 0.25 | 0.25 | 0.25 | 0.25 | And the balance of development speed and code quality is concerned, and the weights of all indexes are balanced. |
The maintainability index comprises cyclic complexity and repeated codes, and in addition, function length (namely the number of lines of the functions) and module Coupling degree (Coupling), wherein the Coupling degree refers to the dependence degree among the modules, the codes with the excessively high Coupling degree are difficult to modify, because other modules can be influenced by changing one module, coupling= Number of dependencies between modules/Number of modules, notes are key for helping developers understand the codes, and good notes can obviously improve the maintainability of the codes. Cr= Lines of CommentsTotal Lines of Code ×100%, CR is the annotation rate, the number of annotation lines is the number of annotation lines in the code, and the total number of code lines is the total number of code lines of the entire code library.
The maintainability comprehensive score can be combined with the above indexes, and one comprehensive score is given in a weighted mode, so that the maintainability of the code is quantified. Maintainability index is expressed as m=w '1*V (G) +w '2×l+w '3×d+w '4×c ' +w '5×cr, M is maintainability score of code, D is code repetition, C is module coupling, CR is annotation rate, w '1, w '2, w '3, w '4, w '5 represent weight coefficients corresponding to 5 maintainability indexes, wherein preferred schemes are shown in table 2.
TABLE 2
| Item scene type | Complexity of cycle (w' 1) | Function length (w' 2) | Code repetition level (w' 3) | Module coupling degree (w' 4) | Annotating rate (w' 5) | Interpretation of the drawings |
| Rapid development project (prototype, MVP) | 0.3 | 0.2 | 0.2 | 0.15 | 0.15 | Development speed is a major goal, allowing for certain code complexity and repetition, focusing on functional implementation and fast iteration. |
| Enterprise-level application (Long-term maintenance system) | 0.35 | 0.3 | 0.15 | 0.1 | 0.1 | Long-term maintenance requirements, code quality and maintainability are critical, and important concerns are about loop complexity and function length, and code repetition is reduced. |
| Performance optimization project (high performance computing) | 0.4 | 0.2 | 0.15 | 0.15 | 0.1 | The performance requirement is high, the code complexity and the coupling degree directly influence the performance optimization, so that the cycle complexity and the module coupling degree weight are higher. |
| Rapid iteration product (SaaS, internet application) | 0.25 | 0.25 | 0.2 | 0.2 | 0.1 | The development speed and the code quality are balanced, the code complexity and the module coupling degree are properly concerned, the annotation rate is low, and the code repeatability is moderate. |
Defect density (DEFECTDENSITY) is one of the important indicators of code quality, which represents the number of defects present in each thousand lines of code (KLOC). It is used to measure defects and problems in code, and is typically used to reflect quality levels in software development. Higher defect density generally means poorer code quality, whereas lower defect density means better code quality. Defect density design defect density = defect number/number of code lines 1000, defect number: number of defects found in the code typically originates from test reports, code censoring or bug tracking systems (e.g., JIRA, bugzilla), number of code lines: total number of lines of code file (typically excluding empty and annotated lines), 1000 for defect density units of "code per thousand lines" (KLOC). Causal reasoning (CausalInference) is used to identify causal relationships between code quality factors, not just statistical correlations. For example, code complexity, increased maintenance cost, repeated codes, increased code redundancy, reduced readability, defect density, and reduced code reliability. In the present invention, the Java code quality is modeled using a causal graph (CausalGraph), and the code quality is optimized by Do-calculus (Do-Calculus) and counterfactual analysis (CounterfactualAnalysis). These causal reasoning techniques can help to understand the causal mechanisms of code optimization in depth, ensuring that every optimization decision is reasonably interpreted. In the process of establishing a causal relationship model by using the core index information in this embodiment, a structural learning algorithm (such as a PC algorithm and GIES algorithm) is used to automatically construct a causal relationship network of code quality factors. Variables contained in the causal graph may include X1 number of code lines, X2 code repetition rate, X3 circle complexity (CyclomaticComplexity), X4 defect density (DEFECTDENSITY), and X5 maintainability (Maintainability). Example causality relationships, X1-X2-X3-X4-X5, X2-X5, X3-X5, or X1-X2-X3-X4-X5, i.e., the number of lines of code affects the code repetition rate, and thus the complexity of the code, and ultimately the maintainability of the code, this causality graph can be modeled by a Bayesian network (BayesianNetwork) or a Structural Equation Model (SEM). For each code optimization suggestion, it is necessary to evaluate whether the code modification (intervention) actually improves quality, rather than blind optimization based on statistical correlations alone, as in the prior art. In code optimization, do-calculus is used to simulate the causal impact of code modification (intervention) on Java code quality. In this way, the potential effects of different optimization schemes can be evaluated. For example, simulation reduces the impact of duplicate codes on maintainability. Example suppose that it is desired to evaluate the effect of reducing duplicate code on maintainability of Java code, P (X5|do (X2=0.1)), indicating a change in maintainability X5 after reducing the code repetition rate (intervention X2). By the counterfactual analysis (CounterfactualAnalysis), the question of "if there is no optimization, whether the code quality will deteriorate" is evaluated. For example, if repeated code is not reduced, the maintenance cost of the code may be higher. The method can be used for screening the most effective optimization strategy to avoid negative optimization (such as code readability reduction caused by over optimization).
And the strategy screening can be further optimized, and the most effective optimization strategy is screened out through causal reasoning calculation, so that negative optimization (such as code readability reduction caused by optimization) is avoided. For core modules (e.g., database access code, service layer logic, etc.), excessive optimization should be avoided, with significant attention paid to performance and stability guarantees. For code modules with high complexity or high repetition rate, the structure can be optimized, redundant codes can be reduced, logic is simplified, and for core codes which are fully verified, the improvement of performance and maintainability are more concerned, and large-scale reconstruction is avoided.
Based on the above thought, in this embodiment, causal relationships are deeply analyzed, the influence of different optimization strategies on code quality is simulated, and Do-Calculus is used to represent a causal model. In the actual causal graph:
c→q (code complexity affects code quality);
d→q (defect density affects code quality);
m→q (maintainability affects code quality);
Causal reasoning can be performed by Do-Calculus, P (q|do (c=c o),do(d=do),do(M=mo)), which represents the probability distribution of Q for a particular value of C o、do、mo for C, d, M, respectively, where the specific value of C o、do、mo can be set or dynamically changed. By means of this causal reasoning method, the intervention effect of different variables, i.e. "how the code quality changes if a certain index is changed", can be simulated. In causal inference, variable intervention is typically performed to observe its effect on a target variable. Assuming that it is desired to analyze the effect of code complexity (C) on code quality (Q), the intervention model can be represented by q=β0+β 1*C +β 2*d +β 3*M +ε2 and can simulate the change in Q when C changes, i.e., P (q|do (c=c)) by Do-Calculus. In this way, it is possible to evaluate how the values of different code complexity affect the code quality and help optimize the code structure. Considering the multiple causal relationships among variables and feedback mechanisms, in a finally established causal reasoning model (Do-Calculus), the code quality is expressed as q=β0+β 1*C +β 2*d +β 3*M +ε1, c=γ 1*d +γ 2*M +ε2, d=λ 1*M +ε3, Q is the code quality (target variable), β0 is a constant term (intercept), β1, β2, β3 are 3 regression coefficients to be estimated for representing causal effects of core indexes on the code quality, γ1, γ2 represent the defect density and the degree of influence coefficient of maintainability on the code complexity, λ1 represents the degree of influence coefficient of maintainability on the defect density, and ε1- ε3 represent first to third error terms.
Specifically, in S2 of the present embodiment, the causal intervention effect of different optimization strategies on the code quality is simulated through a Do-Calculus method and a counterfactual analysis (CounterfactualAnalysis), and when the modified code is provided by the large language model, the causal analysis can simulate the influence of the modified code on the target variable through a Do-Calculus or a counterfactual analysis. I.e. deducing causal intervention effects.
Simulating the effect of the modified code on the target variable using the Do-Calculus method, simulating the change in the target variable (e.g., code quality) after the code is modified. For example, P (q|do (c=c '), do (d=d'), do (m=m ')), where C', d ', M' represent modified code complexity, defect density, and maintainability. Through such simulation, the model can predict changes in code quality after modifying the code.
The counterfactual analysis is used to exclude strategies that would lead to negative optimizations, denoted as P (Q|C, d, M) vsP (Q|C ', d', M '), C', d ', M' representing the modified code complexity, defect density, and maintainability in the counterfactual analysis. The counterfactual analysis is used to compare the results before and after modification. For example, a counterfactual analysis may be used to compare the differences between the code before modification (e.g., C, d, M) and after modification (e.g., C ', d ', M ') to evaluate if the modification is valid. The counterfactual model may analyze the effect of the modification by comparing the actual results with the hypothetical results, P (Q|C, d, M) vsP (Q|C ', d ', M '), and by comparing the code quality before and after modification, the counterfactual analysis helps to evaluate whether the modification strategy brings about the expected positive effect, or whether negative effects are likely to be introduced.
To illustrate more clearly, in combination with the above example, there are two main nodes in the object code, where a while (true) loop condition is determined, an operation in the loop body (such as memory allocation, printing, and suspension) is performed, then the node number n=2 of the control flow graph is obtained, a loop condition is determined, where the operation in the loop body is also one edge, then the edge number e=2 of the control flow graph is determined, if the structure of the program is a single infinite loop, there are no other independent areas, then the independent connected area number p=1, if the structure of the program is a single infinite loop, then l=6, if there is only one while (true) loop in the object code, and there are no other conditional branches (such as if statement, switch statement, etc.), then b=1, and a function complexity fc=l+b=7 is obtained, and there is only one infinite loop while there is no other nested loop or condition, then the nesting depth N' =1. In the scene of this example, if the item scene type selects the "fast developing item", the formula is substituted for the code complexity c=w1×v (G) +w2×fc+w3×n ' +w4×d=2.7, the maintainability index m=w '1*V (G) +w2×l+w3×d+w4×c ' +w5×cr=2.42, and the defect density d=defect number (1)/number of codes (6) ×1000= 166.67.
If an application scenario of the item scenario type of "quick development item" is selected, a general causal model is constructed to represent causal relationships among inputs, processes and outputs of a program, wherein a static analysis program defines causal relationships among various variables in the code by analyzing the code, and builds the causal model, and then a main variable can be found to be a memory allocation function (memory allocation), which allocates 1MB of memory for multiple times and adds the memory to memoryLeakList, a memory usage amount (memory LeakList.size ()), and memory related problems include memory overflow (OutOfMemoryError). Whereby a causal graph as shown in fig. 5 can be generated.
Continuing with the example of fig. 5 above, in the intervention analysis, the size of 1, limit memoryLeakList is 1000, and when 1000 elements are reached, the addition of new elements is stopped, and the code complexity c=3.2, maintainability index m= 2.925, defect density d=111.11. 2. The elements in memoryLeakList are cleaned periodically and the code complexity c=3.4, maintainability index m=3.185, defect density d=100. The program is expressed as If L (t) is not less than MemoryLimit, the rest stop ADDING NEW ELEMENTS or CLEAR THE LIST.
In the counterfactual theory, the quality of the current code is calculated first, and the quality is expressed as Qcurrent = -0.5 x 2.7+0.3 x 166.67+0.2 x 2.42= -1.35+50.00+0.484= 49.134 by a program.
In addition, the inverse fact analysis process can be divided into a plurality of stages, for example, the first stage is bug repair, and the important point is to ensure bug repair in codes, wherein bug repair may not directly improve performance or code quality, but the inverse fact analysis process can avoid error occurrence and reduce problems such as unexpected breakdown and memory overflow. The optimization strategies given in the first stage are 2 optimization strategies 1-1, qoptimized 1= -0.5 x 3.2+0.3 x 111.11+0.2 x 2.925= -1.6+33.33+0.585= 32.315, and optimization strategies 1-2, qoptimized 2= -0.5 x 3.4+0.3 x 100+0.2 x 3.185= -1.7+30.00+0.637= 28.937. The result of 2 optimization strategies is inferred from the counterfactual that neither optimization strategy 1 (limit memoryLeakList size 1000) nor optimization strategy 2 (periodic cleaning memoryLeakList of elements) improves code quality, effectively resulting in a degradation of code quality. Since this problem involves memory leaks, an optimization strategy is necessary, and according to the calculation, the optimization strategy 1 is significantly better than the optimization strategy 2, so that the optimization strategy 1 is adopted for the subsequent scheme optimization.
The second stage is to optimize the performance bottleneck in the code, i.e. after the vulnerability problem is solved, the performance can be further improved, such as reducing the complexity of the code, improving the computing efficiency, etc. The first stage gives 1 optimization strategy, and the optimization strategies 2-1, qoptimized1' = -0.5 x 3.2+0.3 x 125+0.2 x 2.925= -1.6+37.5+0.585= 36.485.
In this embodiment, the modification to the target code data may be performed by a technician operating a computer, which records the trace of the modification and verifies the optimized effect after the modification, for example, the effect evaluation performed at each stage, for example, at stage 1 (after repairing the bug), may first evaluate the stability of the code, check whether the target of repair is reached, and then enter the next stage. If excessive complexity is not introduced in stage 1 and the problem is solved, the optimization process will continue. And (1) after repairing the loopholes, running the program to ensure that the loopholes are solved. If the code quality has somewhat degraded, but the program has been running steadily, stage 2 is entered. And 2, after introducing performance optimization, carrying out re-evaluation, and detecting whether the performance is improved or not without extra complexity.
In an actual Do-Calculus simulation, the model can automatically generate an optimization strategy for a technician operating the computer for optimization. Based on the analysis of the code complexity by the model, the model may suggest splitting a complex function into multiple small functions, thereby reducing the loop complexity. Depending on the defect density, the model may suggest more unit testing for certain high risk areas or more rigorous inspection of the code. The model may suggest improving maintainability by improving the annotated quality of the code or simplifying the code structure. The model can automatically analyze which optimization strategies in the past have significant impact on code quality improvement and generate similar optimization schemes.
In this embodiment, there is also provided a code quality optimization apparatus based on causal reasoning and LLM, as shown in fig. 2, including:
the analysis engine module is used for analyzing the target code data and establishing a corresponding causal relationship model;
the simulation module is used for simulating the causal intervention effect of different optimization strategies on the code quality by utilizing the causal relation model;
The scheme generating module is used for generating an optimization scheme according to the obtained simulation result by utilizing the large language model;
And the updating module is used for recording the modification of the target code data, executing regression test and verifying the optimization effect.
The present embodiment also provides a storage medium storing a computer program or instructions which, when executed, implement the method described in the present embodiment. For example, code for an algorithmic implementation of causal reasoning may be stored in a storage medium:
the following is an example code for causal inference calculations using the dowhy library in Python, demonstrating how the causal inference model can be used to evaluate the impact of code repetition rate on Java code maintainability.
fromdowhyimportCausalModel
importpandasaspd
Code quality data example
data=pd.DataFrame({
"code_lines":[100,200,150,300],
"duplication":[0.2,0.3,0.15,0.4],
"complexity":[10,15,12,20],
"defect_density":[0.05,0.1,0.07,0.15],
"maintainability":[0.8,0.6,0.75,0.5]
})
Construction of causal graph
model=CausalModel(
data=data,
Treatment= "duplication", # intervention variable
Outcome = "maintainability" # target variable
graph="digraph{duplication->complexity;complexity->maintainability}"
)
# Estimate causal influence
identified_estimand=model.identify_effect()
estimate=model.estimate_effect(identified_estimand,method_name="backdoor.linear_regression")
print(estimate.value)。
The embodiment is mainly applied to the Java code quality optimization, and an optimization flow is used for analyzing various influencing factors in the code, such as code complexity, repetition rate, defect density and the like, aiming at key factors in the Java code through causal reasoning, and constructing a causal relation model (or called a causal relation network). It is recognized by this causality model which Java code modifications actually affect the code quality, not just dependencies in the historical data. Moreover, in Java code optimization, customized optimization policies may be employed for different types of code modules, particularly core business code portions (e.g., java's network communications, database operations, service layer logic, etc.). For example, for high complexity modules, redundant code can be reduced, logic simplified, and maintainability improved, while for core business logic code optimization should focus on performance improvement and stability assurance, avoiding extensive reconfiguration and unnecessary modification to reduce the likelihood of introducing potential risks. In addition, in the optimization process, optimization suggestions conforming to Java code styles are generated by combining LLM, and the rationality and feasibility of the optimization suggestions are ensured by the results of causal reasoning. The LLM will generate detailed optimization interpretation reports that help the developer understand the cause, expected effects, and possible side effects of the optimization scheme and provide optimization suggestions on how to balance performance and maintainability. In a word, the invention provides a precise, interpretable and efficient Java code quality optimization method, which can help a developer to improve code quality, reduce unnecessary optimization risks and further improve the optimization effect through real-time feedback and adjustment. Through the combination of causal reasoning and LLM, a highly-targeted optimization strategy can be realized, and the quality improvement of Java codes is ensured to meet the actual requirements of projects.
In this specification, each embodiment is described in a progressive manner, and identical and similar parts of each embodiment are all referred to each other, and each embodiment mainly describes differences from other embodiments. In particular, for the apparatus embodiments, since they are substantially similar to the method embodiments, the description is relatively simple, and reference is made to the description of the method embodiments for relevant points. The foregoing is merely illustrative of the present invention, and the present invention is not limited thereto, and any changes or substitutions easily contemplated by those skilled in the art within the technical scope of the present invention should be included in the scope of the present invention. Therefore, the protection scope of the present invention should be subject to the protection scope of the claims.
Claims (6)
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202510436553.XA CN119938061B (en) | 2025-04-09 | 2025-04-09 | Code quality optimization method, device and storage medium based on causal reasoning and LLM |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202510436553.XA CN119938061B (en) | 2025-04-09 | 2025-04-09 | Code quality optimization method, device and storage medium based on causal reasoning and LLM |
Publications (2)
| Publication Number | Publication Date |
|---|---|
| CN119938061A CN119938061A (en) | 2025-05-06 |
| CN119938061B true CN119938061B (en) | 2025-06-20 |
Family
ID=95545147
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN202510436553.XA Active CN119938061B (en) | 2025-04-09 | 2025-04-09 | Code quality optimization method, device and storage medium based on causal reasoning and LLM |
Country Status (1)
| Country | Link |
|---|---|
| CN (1) | CN119938061B (en) |
Citations (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US12086053B1 (en) * | 2021-06-03 | 2024-09-10 | Wells Fargo Bank, N.A. | Model validation as a service |
Family Cites Families (4)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US12061880B2 (en) * | 2022-07-14 | 2024-08-13 | OpenAI Opco, LLC | Systems and methods for generating code using language models trained on computer code |
| CN116303038A (en) * | 2023-03-21 | 2023-06-23 | 中国船舶集团有限公司综合技术经济研究院 | Software testability analysis, design and evaluation method |
| CN118502768A (en) * | 2024-05-09 | 2024-08-16 | 杭州网易数之帆科技有限公司 | Code generation method, device, apparatus, storage medium and program product |
| CN119512556A (en) * | 2024-10-09 | 2025-02-25 | 浪潮软件集团有限公司 | A code review and optimization method driven by a large language model |
-
2025
- 2025-04-09 CN CN202510436553.XA patent/CN119938061B/en active Active
Patent Citations (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US12086053B1 (en) * | 2021-06-03 | 2024-09-10 | Wells Fargo Bank, N.A. | Model validation as a service |
Also Published As
| Publication number | Publication date |
|---|---|
| CN119938061A (en) | 2025-05-06 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| CN117806980B (en) | Automatic test case generating device based on large language model | |
| Jouck et al. | Generating artificial data for empirical analysis of control-flow discovery algorithms: A process tree and log generator | |
| CN119512556A (en) | A code review and optimization method driven by a large language model | |
| CN118605850B (en) | An optimization system and optimization method for Triton compiler pipeline | |
| CN116011468A (en) | Reasoning method of deep learning model, machine translation method and device | |
| Galinac Grbac et al. | A quality cost reduction model for large-scale software development | |
| CN119690512A (en) | A code defect detection method and system based on large model | |
| CN118426872B (en) | A cross-platform tensor program performance prediction method | |
| Friedrich et al. | Knowledge engineering for configuration systems | |
| Tamanampudi | Development of Real-Time Evaluation Frameworks for Large Language Models (LLMs): Simulating Production Environments to Assess Performance Stability Under Variable System Loads and Usage Scenarios | |
| CN119938061B (en) | Code quality optimization method, device and storage medium based on causal reasoning and LLM | |
| Fournier et al. | Towards a Benchmark for Causal Business Process Reasoning with LLMs | |
| Bokkena | Optimizing Cloud Infrastructure Management Using Large Language Models: A DevOps Perspective | |
| CN117764057A (en) | Instruction execution method for realizing cross-platform unified data by utilizing grammar analysis | |
| Han et al. | Two-phase assessment approach to improve the efficiency of refactoring identification | |
| Granda et al. | A model-level mutation tool to support the assessment of the test case quality | |
| Bures et al. | Identification of potential reusable subroutines in recorded automated test scripts | |
| Tatale et al. | A Survey on Test Case Generation using UML Diagrams and Feasibility Study to Generate Combinatorial Logic Oriented Test Cases. | |
| Owen et al. | Verifying accuracy and precision of AI accelerators in deep neural network inference tasks | |
| Jantke et al. | Flexible simulation scenarios for real-time planning in dynamic environments | |
| Wang et al. | DALO-APR: LLM-based automatic program repair with data augmentation and loss function optimization | |
| Singh et al. | Leveraging artificial intelligence and machine learning in software engineering: current trends and future directions | |
| Sharma et al. | Study and Estimation of Existing Software Quality Models to Predict the Reliability of Component-Based Software | |
| Jia | A Brief Analysis of the Progress and Trends in Software Defect Prediction Methods | |
| Einabadi et al. | A Search-Based Method For optimizing Software Architecture Reliability |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| PB01 | Publication | ||
| PB01 | Publication | ||
| SE01 | Entry into force of request for substantive examination | ||
| SE01 | Entry into force of request for substantive examination | ||
| GR01 | Patent grant | ||
| GR01 | Patent grant |