Code quality optimization method and device based on causal reasoning and LLM and storage medium
By combining causal reasoning and large language models in code optimization, establishing a causal relationship model and simulating optimization strategies, the problem of code optimization relying on historical data in the existing technology is solved, and optimization solutions that are automatically generated that conform to the code style are improved, improving the efficiency and accuracy of code quality optimization.
Patent Information
- Application Number
- CN202510436553.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-09
- Publication Date
- 2025-05-06
- Estimated Expiration
- 2045-04-09
AI Technical Summary
When using large language models (LLM) for code optimization, it is difficult to identify code modifications that affect code quality, and the generated optimization solutions often rely on the correlation of historical data and cannot automatically generate optimization solutions that conform to the code style, resulting in engineers spending a lot of time to optimize and troubleshoot.
The code quality optimization method based on causal reasoning and LLM is adopted. By analyzing the target code data, a causal relationship model is established, the causal intervention effect of different optimization strategies on code quality is simulated, and the optimization scheme is generated using a large language model, and finally the optimization effect is verified through regression test.
It realizes the identification of code modifications that affect code quality, and automatically generates optimization solutions that conform to the corresponding code style, saving engineers' time and energy and improving the efficiency and accuracy of code quality optimization.
Smart Images

Figure CN119938061A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a code quality optimization method based on causal reasoning and LLM, a device and a storage medium. Background Art
[0002] With long-term technical accumulation, artificial intelligence technology has developed rapidly in the past two years. One of the main achievements is the large language model (LLM). Large language model products such as GPT, DeepSeek, and Grok have been put into operation. Large language models are essentially an analytical tool. At present, various companies and institutions have invested huge costs in the training of large language models. The further problem lies in how to apply them to specific practical applications, so as to solve problems in the actual production process and improve the operation efficiency in related application scenarios.
[0003] At present, one of the main possible application scenarios is the analysis and optimization of code. In the past, the analysis and optimization of program code usually required a large number of people to participate. Although there are many automated script programs as code analysis tools, their flexibility and adaptability are often insufficient. In the end, they can only play a part of the auxiliary role, and it is difficult to further save labor costs. For example, the current rule-based static analysis solutions (such as SonarQube) rely heavily on fixed rules set by senior engineers and cannot adapt to different Java development scenarios. Another example: code optimization based on machine learning is mainly based on statistical correlation. It actually provides statistical results and cannot be used to explain the causal mechanism of optimization decisions. Engineers still need to spend a lot of time to derive specific optimization directions based on statistical results. The most advanced way is to optimize code through LLM. However, although it can provide optimization suggestions, it is often based on pattern matching. It is difficult to determine whether the optimized code will cause new problems. Engineers still need to spend a lot of time on bug hunting and troubleshooting.
[0004] Therefore, it is necessary to redevelop the current LLM-based code optimization solution so that it not only relies on the correlation in historical data, but can also identify code modifications that affect code quality and automatically generate optimization solutions that conform to the corresponding code style, thereby saving engineers' time and energy. Summary of the invention
[0005] The embodiments of the present invention provide a code quality optimization method, device and storage medium based on causal reasoning and LLM, which can identify code modifications that affect code quality and can also automatically generate optimization solutions that conform to the corresponding code style, thereby saving engineers' time and energy.
[0006] To achieve the above object, the embodiments of the present invention adopt the following technical solutions: In a first aspect, an embodiment of the present invention provides a code quality optimization method based on causal reasoning and LLM, characterized in that it includes: S1. Analyze the target code data and establish the corresponding causal relationship model; S2. Using the causal relationship model, simulate the causal intervention effects of different optimization strategies on code quality; S3, using the large language model to generate an optimization plan based on the obtained simulation results; S4. Record the modification to the target code data, perform regression testing and verify the optimization effect.
[0007] In a second aspect, an embodiment of the present invention provides a code quality optimization device based on causal reasoning and LLM, characterized in that it includes: An analysis engine module, used to parse the target code data and establish a corresponding causal relationship model; A simulation module, used to simulate the causal intervention effects of different optimization strategies on code quality by using the causal relationship model; A solution generation module is used to generate an optimization solution based on the obtained simulation results using a large language model; The update module is used to record the modification to the target code data, perform regression testing and verify the optimization effect.
[0008] In a third aspect, an embodiment of the present invention provides a storage medium storing a computer program or instructions, which implements the method in the embodiment when the computer program or instructions are executed.
[0009] In an embodiment of the present invention, various influencing factors in the code, such as code complexity, repetition rate, defect density, etc., are analyzed by causal reasoning, and a causal relationship model (or causal relationship network) is constructed. The causal relationship model is used to identify which Java code modifications really affect the code quality, rather than just relying on the correlation in historical data. In addition, in Java code optimization, customized optimization strategies can be adopted for different types of code modules, especially core business code parts (such as Java network communication, database operations, service layer logic, etc.). For example, for modules with high complexity, redundant code can be reduced, logic can be simplified, and maintainability can be improved; while for core business logic code, optimization should focus on performance improvement and stability assurance, avoid large-scale reconstruction and unnecessary modifications, so as to reduce the possibility of introducing potential risks. In addition, during the optimization process, optimization suggestions that conform to the Java code style are generated by combining LLM. Thereby, the code modifications that affect the code quality are identified, and the optimization scheme that conforms to the corresponding code style can be automatically generated, thereby saving the time and energy of engineers. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary engineers in this field, other drawings can be obtained based on these drawings without creative work.
[0011] Figure 1 A schematic diagram of a method flow chart provided by an embodiment of the present invention; Figure 2 A schematic diagram of the structure of a device provided in an embodiment of the present invention; Figure 3 A network diagram of the training process provided by an embodiment of the present invention; Figure 4 The algorithm operation topology diagram provided by the embodiment of the present invention; Figure 5 A schematic diagram of a specific example provided for an embodiment of the present invention. DETAILED DESCRIPTION
[0012] In order to make the technical scheme of the present invention better understood by the engineers in the field, the present invention is further described in detail below in conjunction with the drawings and specific embodiments. The embodiments of the present invention will be described in detail below, and examples of the embodiments are shown in the drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements with the same or similar functions. The embodiments described below by reference to the drawings are exemplary and are only used to explain the present invention, and cannot be interpreted as limiting the present invention. Engineers in the field of technology can understand that, unless specifically stated, the singular forms "one", "one", "said" and "the" used here may also include plural forms. It should be further understood that the wording "including" used in the specification of the present invention refers to the presence of the features, integers, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or their groups. It should be understood that when an element is said to be "connected" or "coupled" to another element, it can be directly connected or coupled to other elements, or there may also be intermediate elements. In addition, the "connection" or "coupling" used here may include wireless connection or coupling. The term "and / or" used herein includes any unit and all combinations of one or more associated listed items. Engineers in the field of this technology can understand that, unless otherwise defined, all terms (including technical terms and scientific terms) used herein have the same meaning as the general understanding of ordinary engineers in the field to which the present invention belongs. It should also be understood that those terms such as those defined in general dictionaries should be understood to have a meaning consistent with the meaning in the context of the prior art, and will not be interpreted with an idealized or overly formal meaning unless defined as here.
[0013] This embodiment is mainly used in Java code quality optimization. The general design purpose is to combine causal reasoning and LLM to optimize the Java code quality. By constructing a causal graph, the impact of Java code optimization on quality indicators is quantified, and optimization suggestions are generated with the help of LLM to achieve efficient, accurate and explainable Java code quality optimization. Among them, the causal relationship between Java variables is analyzed through causal reasoning technology to identify which code modifications really affect the code quality, rather than relying solely on the correlation in historical data. Combining LLM with causal reasoning can not only provide Java code optimization suggestions, but also explain the rationality of these suggestions, helping developers make more informed decisions.
[0014] In practical applications, the implementation process of this embodiment can be roughly divided into: 1. Build a causal network: First, analyze Java code quality data and extract core indicators such as code complexity, maintainability, and defect density. These data can come from static analysis tools (such as SonarQube), code review records, and historical project data. Causal modeling: Use Bayesian networks, structural equation models (SEM), or causal graphs to build causal relationships between Java code quality factors. For example, the influence chain between factors such as code complexity, repetition rate, and function length. Calculate causal influence: Use causal reasoning to calculate the impact of each factor on the target variable (such as maintainability, code reliability, etc.) and screen out key influencing factors.
[0015] 2. Optimization strategy based on causal reasoning calculation: Causal intervention simulation: Use do-calculus to simulate the causal intervention effects of different optimization strategies on code quality. For example, simulate the impact on maintainability and defect density after reducing code repetition or reducing function complexity. Counterfactual analysis: Through counterfactual analysis, evaluate the potential effects of different optimization schemes and exclude schemes that may lead to negative optimization (such as some optimizations may cause code performance to degrade). Customized optimization strategy: Develop more targeted optimization strategies based on different types of code (such as core business code, algorithm core modules, UI components, etc.). For modules with higher complexity, you can focus on optimizing code refactoring and modularization; for core functional modules, avoid large-scale modifications and focus on performance improvement and stability assurance.
[0016] 3. Generate optimization suggestions and plans using LLM: Based on the results of causal reasoning, use LLM (such as GPT, CodeT5, etc.) to generate optimization plans that conform to Java code style and best practices. LLM will make more feasible optimization suggestions based on context analysis. Optimization plan explanation: When LLM generates optimization suggestions, it will provide detailed explanations of the expected effects before and after optimization, possible side effects, and how to balance different optimization goals (such as the trade-off between maintainability and performance). Optimization report generation: Generate a detailed report containing optimization strategies, expected effects, potential risks, and implementation suggestions to help developers understand the optimization process and results.
[0017] 4. Code optimization execution and feedback: Execute optimization: Apply the optimization solution generated by LLM to the Java code and perform regression testing to ensure that the optimization does not affect existing functions or introduce new problems. Quality assessment: Verify the optimization effect through static analysis tools (such as SonarQube) or dynamic testing to ensure that the optimized Java code meets the expected quality standards (such as maintainability, performance, etc.). Feedback and learning: LLM records optimization results, analyzes developer feedback, and dynamically adjusts optimization strategies to improve the accuracy and feasibility of subsequent optimization suggestions.
[0018] 5. Continuous improvement: Incremental learning: The optimization strategy is constantly fed back and adjusted based on historical optimization data, combined with the actual usage scenarios and feedback of developers, to continuously improve the intelligence level of optimization models and optimization suggestions. Evolution of optimization strategy: With the accumulation of more projects and optimization cases, the optimization strategy will gradually evolve to form a more accurate and personalized optimization solution.
[0019] Combined with the actual application scenario, the preferred solution of the embodiment design is finalized, such as Figure 1 , 3 , as shown in 4, including: S1. Analyze the target code data and establish a corresponding causal relationship model.
[0020] S2. Using the causal relationship model, simulate the causal intervention effects of different optimization strategies on code quality.
[0021] In this embodiment, more targeted optimization strategies are formulated according to different types of codes (such as core business codes, algorithm core modules, UI components, etc.); for modules with higher complexity, code reconstruction and modularization can be optimized; for core functional modules, large-scale modifications should be avoided, and the focus should be on performance improvement and stability assurance. For example: core business code refers to the code part that carries the core business logic. Generally, these codes have high complexity and are directly related to the functions and performance of the system. When optimizing core business codes, the focus is on code reconstruction, improving maintainability, and optimizing performance, but frequent and large-scale modifications need to be avoided to ensure the stability of the business logic.
[0022] Specific optimization strategies include: Code refactoring: By analyzing the functions and classes in the core business modules, find out the complex and lengthy parts and refactor them. For example: Split long methods: If a function is very complex and contains multiple responsibilities, it should be split into multiple small functions with a single responsibility. Simplify conditional statements: Avoid too many nested conditions, and use design patterns such as strategy pattern or state pattern to simplify complex business logic. Improve modularity: Split complex business logic into multiple small modules or services to make the code easier to understand and modify. Performance optimization: Core business modules usually need to process large amounts of data or perform high-frequency calculations, so performance improvement needs to be focused on during optimization. Optimization methods include: Algorithm optimization: Check the efficiency of existing algorithms and use more efficient algorithms (for example, optimize sorting algorithms, improve data queries, etc.). Cache optimization: Cache repeated calculations to avoid unnecessary repeated calculations.
[0023] The algorithm core module is the core code part of the system for data processing, calculation or conversion. When optimizing the algorithm module, the most important thing is to improve the efficiency of the algorithm, especially in scenarios where large-scale data is processed or high real-time performance is required, the optimization effect is particularly critical. Optimization strategies include: Optimizing algorithm complexity: Find more efficient algorithms by analyzing the time complexity and space complexity of existing algorithms. For example, replace the brute force search algorithm with a hash table lookup, or use dynamic programming to reduce redundant calculations. Reduce resource consumption: Optimize memory usage to avoid memory leaks and resource waste, especially when processing big data. Parallel computing and distributed processing: Perform parallel processing or distributed computing on computationally intensive tasks to improve processing capabilities. Algorithm stability and robustness: Ensure that the algorithm runs stably under various boundary conditions to avoid algorithm crashes due to irregular input or extreme situations.
[0024] UI components are responsible for interacting with users. When optimizing the code of UI components, we mainly focus on response speed, interface fluency, user experience and maintainability. UI optimization is not only about reducing code complexity, but also about improving user experience. Optimization strategies include: Improving rendering efficiency: For complex UI interfaces, optimize the rendering process to avoid unnecessary re-rendering. Performance bottlenecks during rendering can be reduced by virtualizing lists, lazy loading, or incremental rendering. Reduce DOM operations: For front-end web applications, reduce frequent operations on the DOM to avoid rearrangement and redrawing of a large number of DOM elements for each interaction. UI componentization: Split complex UI interfaces into multiple reusable UI components to improve the maintainability and scalability of the code. Responsive design: Ensure that the UI runs smoothly on different devices and adapts to different screen sizes and resolutions.
[0025] S3. Use the large language model to generate an optimization plan based on the simulation results.
[0026] Among them, the code optimization dataset is used to train the large language model. The code optimization dataset is specially designed for training the large language model to generate, improve and optimize code quality. This dataset contains multiple dimensions related to code optimization, with the aim of helping the large language model learn how to generate high-quality code optimization strategies based on different project requirements and goals. The dataset will include a large number of original code and optimized code comparisons. These codes can come from different application scenarios, such as core business logic, algorithm core modules, UI components, etc. These data come from open source code libraries, technical documents, and programming tutorials to ensure that the model has high accuracy and practicality when generating and understanding code. The code optimization dataset will also include various optimization strategies and implementation methods. These strategies target different types of code and modules. After each code comparison record, there will be indicators related to code quality, such as complexity, defect density, and maintainability score. The code optimization dataset also includes examples of error repair and prevention, covering repairing common programming errors (such as memory leaks, null pointer exceptions, deadlocks, etc.) and enhancing the robustness of code, especially error repair in high-concurrency and distributed environments. Large language models (LLMs) are commonly used in a variety of application scenarios, such as text generation, translation, and dialogue systems. When training these models, the training datasets used are mostly natural language texts, and the uniqueness of the code optimization dataset is that it focuses on the structure, performance, and quality optimization of the code level.
[0027] S4. Record the modification to the target code data, perform regression testing and verify the optimization effect.
[0028] The target code data is modified according to the generated optimization scheme, and regression testing is performed to verify the optimization effect. For example, the large language model (LLM) generates an optimization scheme based on the analyzed code optimization data set and project goals. The developer or automated tool or large language model automatically optimizes and modifies the target code according to the optimization scheme.
[0029] Specifically, in S1 of this embodiment, it includes: obtaining the core indicator information of the target code data; establishing a causal relationship model using the core indicator information, and the causal relationship model is used to calculate the impact of each core indicator on the target variable.
[0030] Specifically, by calculating the impact of each core indicator on the target variable (such as code quality), some key influencing factors are obtained. The subsequent effects of key influencing factors include the design of optimization strategies, resource allocation and priority setting. For example: If code complexity (C) is a key influencing factor and its increase has a negative impact on code quality, then the optimization strategy may be: refactoring complex functions or splitting long methods to reduce code complexity, thereby improving code quality. If defect density (D) is determined as a key factor and it has a negative impact on code quality, then the strategy may be to strengthen code review and unit testing to reduce the number of defects, thereby improving code quality. If maintainability (M) is a key factor affecting code quality and its improvement can maximize the improvement of code quality compared with other factors, then the development team should prioritize optimizing the maintainability of the code. After generating an optimization plan through S3, the technical R&D team can focus resources on key influencing factors such as code complexity and defect density, rather than wasting too much time and resources on other variables with less impact. At the same time, the computer records the modifications made by technicians to the target code data. In the subsequent development process, key influencing factors can also be used as monitoring and evaluation indicators. The R&D team can continuously monitor changes in these factors and adjust development and optimization strategies based on the changes. By continuously evaluating key influencing factors, a feedback loop can be formed to ensure continuous improvement and enable continuous adjustment of optimization strategies. The R&D team can use the model to predict the intervention effect after implementing certain optimization strategies. By predicting how the key influencing factors in the model change, the R&D team can predict the future code quality at the beginning of the project and make appropriate interventions.
[0031] It should be noted that, in this embodiment, the optimization solution generated by S3 can be directly provided to the technical staff of the R&D team in text form. After the technical staff makes code modifications, the computer records the modifications made by the technical staff to the target code data and executes S4 to pass regression testing and verify the optimization effect.
[0032] Specifically, the core indicator information of the target code data is obtained by scanning the code base of the target code data through a static analysis tool (such as SonarQube) to obtain the code complexity of the target code data, and determine the maintainability and defect density; wherein the indicators corresponding to the code complexity include: obtaining the cyclomatic complexity, function complexity and nesting depth of the code of the corresponding module, class and method from the target code, wherein the cyclomatic complexity is expressed as V(G)=E-N+2P, the function complexity is expressed as Fc=L+B, the nesting depth N represents the number of layers of the maximum nested conditional statements or loop statements, V(G) represents the loop complexity of the graph (i.e., cyclomatic complexity), E is the number of edges in the control flow graph, N is the number of nodes in the control flow graph, P is the number of independent connected regions in the program (usually 1, representing the main region of the program), Fc is the function complexity, L is the number of code lines in the function (for example, the number of code lines does not include blank lines and comment lines), and B is the number of branches in the function (for example, the number of branch statements such as if and switch).
[0033] Among them, Cyclomatic Complexity: measures the number of independent paths in the code, usually using the McCabe Cyclomatic Complexity metric. A higher complexity value may mean that the code is difficult to understand and difficult to test. Function Complexity: measures the number of lines of code, branch structures, and loop structures within a function. Larger functions usually mean higher complexity, and it is recommended to split them into multiple smaller functions. Nesting Depth: measures the nesting depth of conditional statements and loop statements. Deep nesting increases the difficulty of understanding and maintaining the code, especially during debugging. Code Duplication: Duplicate code increases the complexity of the code because it requires changing multiple code locations when modifying, which can easily lead to maintenance errors.
[0034] Cyclomatic complexity is used to measure the number of independent paths in the code. By analyzing the control flow graph and calculating the number of paths in the program, the complexity of the code is obtained. For each function or method, analyze its control flow graph (ControlFlowGraph, CFG), calculate the number of edges and nodes in it, and substitute them into the formula to calculate the complexity. If the complexity value is too high (usually greater than 10), the code needs to be split or reconstructed. For example: in the process of calculating the cyclomatic complexity V(G)=E-N+2P=2, if there are two main nodes in the target code, such as: while(true) loop condition judgment, operations in the loop body (memory allocation, printing, pause). Therefore, the number of nodes N=2 in the control flow graph can be obtained; if in the target code, the loop condition judgment while(true), which means that there is an edge in E, then the operations in the loop body (memory allocation, printing, pause, etc.) are also an edge, so the number of edges in the control flow graph E=2; if the structure of the program is a single infinite loop with no other independent areas, the number of independent connected areas P=1. For another example: suppose the control flow graph of a function contains 7 nodes, 8 edges, and the function is a single connected region (P=1), then its cyclomatic complexity is: V(G)=8−7+2(1)=3.
[0035] Function complexity mainly considers the size of the function (number of lines of code) and its internal control structure. It can be evaluated by counting the number of lines of the function and some other metrics. Perform static analysis on the function and count its number of lines of code (L) and the number of branches (B). If the number of lines of a function exceeds the set threshold (for example, 20 lines) or there are too many branches, you should consider splitting or simplifying it. For example: the main structure in the target code is an infinite loop, then the main number of lines of code after removing blank lines and comments is 6 lines, that is, L=6; if there is only one while(true) loop in the code and no other conditional branches (such as if statements, switch statements, etc.), then 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), then its complexity is: Fc=25+4=29.
[0036] The nesting depth measures the level of conditional statements or loop statements in the code. Too much nesting depth usually means that the logic of the code is difficult to track, which increases the complexity of maintenance: N=max(Nesting Depth of all branches), where N is the maximum nesting depth. The nesting depth refers to the number of levels of the maximum nested conditional statements or loop statements. Specifically, analyze each control flow, calculate the nesting depth of each branch and loop, and find the maximum value. If the nesting depth exceeds the set threshold (for example, 3 layers), consider optimizing the code structure. For example: there is only one infinite loop while(true) and no other nested loops or conditional statements, then the nesting depth N=1. For another example: suppose a function has the following nesting: first layer: if(condition1), second layer: if(condition2), third layer: for(i=0;i<10;i++) then the nesting depth is 3, indicating that the complexity of the function is high.
[0037] Code duplication refers to the presence of multiple duplicate code blocks in a program. This not only increases the complexity of the code, but may also lead to maintenance difficulties and errors: D=Number of duplicate lines / Total lines of code*100%, where D is the code duplication. "Number of duplicate lines" refers to the total number of lines of code that appear multiple times. "Total number of code lines" is the number of code lines in the entire project or module. Specifically, perform code analysis, find duplicate code blocks, and calculate the duplication. If the duplication is too high (usually more than 10%), it is necessary to eliminate duplicate code by extracting functions or modularizing. For example: Assuming that the total number of lines of code in a project is 1,000, of which 120 lines are duplicate code, the duplication is: D=120 / 1,000*100%=12% If the duplication is too high, it is recommended to refactor and abstract the duplicate code.
[0038] In this embodiment, in order to comprehensively evaluate the complexity and quality of the code, multiple complexity indicators can be combined to give a comprehensive score. 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 four code complexity indicators, and the preferred scheme is shown in Table 1.
[0039] Table 1 Project scenario type <![CDATA[Cyclomatic complexity (w1)]]> <![CDATA[Function complexity (w2)]]> <![CDATA[Depth of nesting (w3)]]> <![CDATA[Code duplication degree (w4)]]> explain Rapid project development (prototype, MVP) 0.2 0.3 0.2 0.3 Development speed is prioritized, complexity and maintainability requirements are low, and duplicate code is acceptable. Enterprise-level applications (long-term maintenance systems) 0.3 0.4 0.2 0.1 Maintainability and code quality are the focus, complexity and code duplication are low, and stability is the most critical. Performance Optimization Project (High Performance Computing) 0.4 0.2 0.3 0.1 Performance optimization is the core. Complexity and nesting depth affect performance, while function complexity and code duplication are less important. Rapid product iteration (SaaS, Internet applications) 0.25 0.25 0.25 0.25 Focus on the balance between development speed and code quality, and the weights of various indicators are relatively balanced. Maintainability indicators include: cyclomatic complexity, repeated code, in addition to: function length (i.e. the number of lines in the function), module coupling (Coupling), coupling refers to the degree of dependency between modules, code with high coupling is difficult to modify, because changing one module may affect other modules, Coupling = Number of dependencies betweenmodules / Number of modules, comments are the key to helping developers understand the code, good comments can significantly improve the maintainability of the code. CR = Lines of Comments Total Lines of Code * 100%, CR is the comment rate, the number of comment lines is the number of comment lines in the code, and the total number of code lines is the total number of code lines in the entire code base.
[0040] The comprehensive score of maintainability can combine the above indicators and give a comprehensive score by weighting to quantify the maintainability of the code. The maintainability index is expressed as M=w'1*V(G)+w'2*L+w'3*D+w'4*C+w'5*CR, where M is the maintainability score of the code, D is the code duplication, C is the module coupling, CR is the comment rate, and w'1, w'2, w'3, w'4, and w'5 represent the weight coefficients corresponding to the five maintainability indicators. The preferred solution is shown in Table 2.
[0041] Table 2 Project scenario type <![CDATA[Cyclomatic complexity (w’1)]]> <![CDATA[Function length (w’2)]]> <![CDATA[Code duplication degree (w’3)]]> <![CDATA[Module coupling degree (w’4)]]> <![CDATA[Annotation rate (w’5)]]> explain Rapid project development (prototype, MVP) 0.3 0.2 0.2 0.15 0.15 Development speed is the main goal, allowing a certain degree of code complexity and repetition, focusing on functional implementation and rapid iteration. Enterprise-level applications (long-term maintenance systems) 0.35 0.3 0.15 0.1 0.1 Long-term maintenance requirements, code quality and maintainability are crucial, focusing on cyclomatic complexity and function length, and reducing code duplication. Performance Optimization Project (High Performance Computing) 0.4 0.2 0.15 0.15 0.1 The performance requirements are high, and code complexity and coupling directly affect performance optimization, so the cyclomatic complexity and module coupling are weighted higher. Rapid product iteration (SaaS, Internet applications) 0.25 0.25 0.2 0.2 0.1 Balance development speed and code quality, pay moderate attention to code complexity and module coupling, have a low comment rate, and moderate code duplication. Defect Density is one of the important indicators for measuring code quality. It indicates the number of defects per thousand lines of code (KLOC). It is used to measure defects and problems in the code and is usually used to reflect the quality level in software development. A higher defect density usually means poor code quality, and vice versa, a lower defect density means better code quality. Design of defect density: Defect density = number of defects / number of lines of code*1000, number of defects: the number of defects found in the code, usually from test reports, code reviews or bug tracking systems (such as JIRA, Bugzilla), number of lines of code: the total number of lines in the code file (usually excluding blank lines and comment lines), 1000: in order to make the defect density unit "per thousand lines of code" (KLOC). Causal Inference is used to identify causal relationships between code quality factors, not just statistical correlations. For example: code complexity → increased maintenance costs, duplicate code → increased code redundancy → reduced readability, defect density → decreased code reliability. In the present invention, a causal graph is used to model Java code quality, and code quality is optimized through do-calculus and counterfactual analysis. These causal reasoning techniques can help to deeply understand the causal mechanism of code optimization and ensure that each optimization decision has a reasonable explanation. In the process of establishing a causal relationship model using the core indicator information in this embodiment, a structural learning algorithm (such as PC algorithm, GIES algorithm) is used to automatically construct a causal relationship network of code quality factors. The variables contained in the causal graph may include: X1: number of lines of code, X2: code repetition rate, X3: cyclomatic complexity, X4: defect density, X5: maintainability. Example causal relationship: X1→X2→X3→X4→X5, X2→X5, X3→X5, or X1→X2→X3→X4→X5, that is, the number of lines of code affects the code repetition rate, which in turn affects the complexity of the code, and ultimately affects the maintainability of the code. This causal graph can be modeled by a Bayesian network or a structural equation model (SEM). For each code optimization suggestion, it is necessary to evaluate whether the code modification (intervention) can really improve the quality, rather than blindly optimizing based on statistical correlation as in the prior art. When performing 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, simulate the impact of reducing duplicate code on maintainability.Example: Suppose you want to evaluate the impact of reducing duplicate code on the maintainability of Java code: P(X5|do(X2=0.1)), which represents the change in maintainability X5 after reducing the code duplication rate (intervention X2). Through counterfactual analysis, evaluate the question of "whether the code quality will deteriorate if a certain optimization is not performed." For example, if duplicate code is not reduced, will the maintenance cost of the code be higher? This analysis helps identify which optimizations are effective and which may lead to negative optimizations. This method can be used to screen the most effective optimization strategies and avoid negative optimizations (such as over-optimization that reduces code readability).
[0042] And we can further optimize the strategy screening, and through causal reasoning calculation, we can screen out the most effective optimization strategy to avoid negative optimization (for example, the code readability is reduced after optimization). For core modules (such as database access code, service layer logic, etc.), we should avoid over-optimization, and focus on performance and stability assurance. For code modules with high complexity or high repetition rate, we can optimize their structure, reduce redundant code, and simplify logic; for core codes that have been fully verified, we should pay more attention to performance improvement and maintainability, and avoid large-scale reconstruction.
[0043] Based on the above ideas, this embodiment deeply analyzes the causal relationship, simulates the impact of different optimization strategies on code quality, and uses Do-Calculus to represent the 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 inference can be performed through Do-Calculus: P(Q|do(C=c o ),do(d=d o ),do(M=m o )), which means that C, d, and M are each a specific value c o d o 、m o In the case of o d o 、m oThe specific value of can be set or changed dynamically. Using this causal reasoning method, the intervention effect of different variables can be simulated, that is, "how does the code quality change if a certain indicator is changed". In causal reasoning, variable intervention is usually performed to observe its impact on the target variable. Suppose you want to analyze the impact of code complexity (C) on code quality (Q), you can express the intervention model in the following way: Q=β0+β1*C+β2*d+β3*M+ϵ2, and you can simulate the change of Q when C changes, that is, through Do-Calculus simulation: P(Q|do(C=c)). In this way, you can evaluate how different code complexity values affect code quality and help optimize the code structure. Considering the multiple causal relationships and feedback mechanisms between variables, in the causal reasoning model (Do-Calculus) finally established, 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 the constant term (intercept), β1, β2, β3 are three regression coefficients to be estimated, which are used to represent the causal impact of the core indicators on code quality, γ1, γ2 represent the influence coefficients of defect density and maintainability on code complexity, λ1 represents the influence coefficient of maintainability on defect density, and ϵ1~ϵ3 represent the first to third error terms.
[0044] Specifically, in S2 of this embodiment, it includes: simulating the causal intervention effects of different optimization strategies on code quality through Do-Calculus method and counterfactual analysis; when the large language model provides modified code, causal analysis can simulate the impact of the modified code on the target variable through Do-Calculus or counterfactual analysis, that is, deducing the causal intervention effect.
[0045] Simulate the impact of modified code on target variables: Use the Do-Calculus method to simulate the changes in target variables (such as code quality) after code modification. For example: P(Q|do(C=c′),do(d=d′),do(M=m′)), where c′, d′, m′ represent the complexity, defect density, and maintainability of the modified code. Through this simulation, the model can predict the changes in code quality after code modification.
[0046] The counterfactual analysis is used to exclude strategies that will lead to negative optimization, expressed as: P(Q|C,d,M)vsP(Q|C′,d′,M′), where C′,d′,M′ represents the complexity, defect density, and maintainability of the modified code in the counterfactual analysis. Counterfactual analysis is used to compare the results before and after the modification. For example, counterfactual analysis can be used to compare the difference between the code before the modification (such as C,d,M) and after the modification (such as C′,d′,M′) to evaluate whether the modification is effective. The counterfactual model can analyze the effect of the modification by comparing the actual result with the hypothetical result: P(Q|C,d,M)vsP(Q|C′,d′,M′). By comparing the code quality before and after the modification, counterfactual analysis helps evaluate whether the modification strategy has brought the expected positive impact or whether it may introduce negative effects.
[0047] To illustrate more clearly, we can combine the above examples. There are two main nodes in the target code, the while(true) loop condition judgment, and the operations in the loop body (memory allocation, printing, pause), then the number of nodes in the control flow graph N=2; the loop condition judgment while(true), then the operation in the loop body is also an edge, then the number of edges in the control flow graph E=2; if the structure of the program is a single infinite loop with no other independent areas, then the number of independent connected areas P=1; the main structure in the target code is an infinite loop, then L=6; if there is only one while(true) loop in the code and no other conditional branches (such as if statements, switch statements, etc.), then B=1, and the function complexity Fc=L+B=7 is obtained; there is only one infinite loop while(true) and no other nested loops or conditional statements, then the nesting depth N=1. In the scenario of this example, if the project scenario type is selected as "rapid development project", substitute the following formula: code complexity C=w1*V(G)+w2*Fc+w3*N+w4*D=2.7; maintainability index M=w'1*V(G)+w'2*L+w'3*D+w'4*C+w'5*CR=2.42; defect density d=number of defects (1) / number of lines of code (6)*1000=166.67.
[0048] If you choose the "Rapid Development Project" project scenario type, you will build a general causal model to represent the causal relationship between the program's input, processing, and output. The static analysis program analyzes the code, defines the causal relationship between the variables in the code, and builds a causal model. Then you can find that the main variables are: memory allocation function (memory allocation): allocate 1MB of memory multiple times and add it to memoryLeakList; memory usage (memoryLeakList.size()); memory-related problems include memory overflow (OutOfMemoryError). This can generate the following Figure 5 Cause and effect diagram shown.
[0049] Continue to combine the above Figure 5 For example, in the intervention analysis, 1. Limit the size of memoryLeakList to 1000, stop adding new elements when it reaches 1000 elements, and: code complexity C = 3.2; maintainability index M = 2.925; defect density d = 111.11. 2. Clean up the elements in memoryLeakList regularly, and: code complexity C = 3.4; maintainability index M = 3.185; defect density d = 100. The program is expressed as: If L(t) ≥ MemoryLimit, then stopadding new elements or clear the list.
[0050] In counterfactual reasoning, the quality of the current code is calculated first, which is expressed through the program as: Qcurrent=−0.5*2.7+0.3*166.67+0.2*2.42=−1.35+50.00+0.484=49.134.
[0051] Moreover, the counterfactual analysis process can be divided into multiple stages. For example, the first stage is bug fixes, which focuses on ensuring that bugs in the code are fixed. Bug fixes may not directly improve performance or code quality, but they can avoid errors and reduce unexpected crashes and memory overflows. There are two optimization strategies given in the first stage: Optimization strategy 1-1, Qoptimized1=−0.5*3.2+0.3*111.11+0.2*2.925=−1.6+33.33+0.585=32.315; Optimization strategy 1-2, Qoptimized2=−0.5*3.4+0.3*100+0.2*3.185=−1.7+30.00+0.637=28.937. According to the results of counterfactual reasoning of the two optimization strategies: Optimization strategy 1 (limiting the size of memoryLeakList to 1000) and optimization strategy 2 (regularly clearing the elements in memoryLeakList) did not improve the code quality, but actually caused the code quality to deteriorate. Because this problem involves memory leaks, the optimization strategy is necessary. According to calculations, optimization strategy 1 is obviously better than optimization strategy 2, so optimization strategy 1 is used for subsequent solution optimization.
[0052] The second stage is to optimize the performance bottlenecks in the code, that is, after the vulnerability problem is solved, the performance can be further improved, such as reducing code complexity, improving computing efficiency, etc. There is one optimization strategy given in the first stage, optimization strategy 2-1, Qoptimized1'=−0.5*3.2+0.3*125+0.2*2.925=−1.6+37.5+0.585=36.485.
[0053] In this embodiment, the modification of the target code data can be performed by a technician operating a computer. The computer records the modification traces and verifies the optimization effect after the modification. For example, the effect is evaluated at each stage. For example, in stage 1 (after fixing the vulnerability), the code stability can be evaluated first to check whether the repair goal has been achieved, and then enter the next stage. The optimization process will continue only if too much complexity is not introduced in stage 1 and the problem is solved. Stage 1: After fixing the vulnerability, run the program to ensure that the vulnerability is resolved. If the code quality has dropped slightly, but the program has been running stably, enter stage 2. Stage 2: After introducing performance optimization, re-evaluate to detect whether the performance has been improved without introducing additional complexity.
[0054] In the actual Do-Calculus simulation, the model can automatically generate optimization strategies for technicians operating computers to perform optimizations. Based on the model's analysis of code complexity, the model may suggest splitting a complex function into multiple small functions to reduce cyclomatic complexity. Based on defect density, the model may suggest more unit testing for certain high-risk areas or more rigorous code review. The model may suggest improving maintainability by improving the quality of code comments or simplifying code structure. The model can automatically analyze which optimization strategies have had a significant impact on improving code quality in the past and generate similar optimization solutions.
[0055] In this embodiment, a code quality optimization device based on causal reasoning and LLM is also provided. Figure 2 As shown, including: An analysis engine module, used to parse the target code data and establish a corresponding causal relationship model; A simulation module, used to simulate the causal intervention effects of different optimization strategies on code quality by using the causal relationship model; A solution generation module is used to generate an optimization solution based on the obtained simulation results using a large language model; The update module is used to record the modification to the target code data, perform regression testing and verify the optimization effect.
[0056] This embodiment also provides a storage medium, including: a computer program or instruction stored therein, which, when executed, implements the method described in this embodiment. For example, the code for implementing the algorithm for causal reasoning may be stored in the storage medium: The following is an example code using the dowhy library in Python for causal inference calculations, demonstrating how to use causal inference models to evaluate the impact of code duplication on Java code maintainability.
[0057] fromdowhyimportCausalModel importpandas aspd # 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] }) # Build a causal graph model = CausalModel( data=data, treatment="duplication", # intervention variable outcome="maintainability", # target variable graph="digraph{duplication->complexity;complexity->maintainability}" ) # Estimating causal effects identified_estimand=model.identify_effect() estimate=model.estimate_effect(identified_estimand,method_name="backdoor.linear_regression") print(estimate.value).
[0058] This embodiment is mainly used in Java code quality optimization. The optimization process targets the key factors in Java code. First, various influencing factors in the code, such as code complexity, repetition rate, defect density, etc., are analyzed through causal reasoning, and a causal relationship model (or causal relationship network) is constructed. Through this causal relationship model, it is identified which Java code modifications really affect the code quality, rather than just relying on the correlation in historical data. In addition, in Java code optimization, customized optimization strategies can be adopted for different types of code modules, especially core business code parts (such as Java network communication, database operations, service layer logic, etc.). For example, for modules with high complexity, redundant code can be reduced, logic can be simplified, and maintainability can be improved; while for core business logic code, optimization should focus on performance improvement and stability assurance, avoid large-scale reconstruction and unnecessary modifications, so as to reduce the possibility of introducing potential risks. In addition, during the optimization process, optimization suggestions that conform to the Java code style are generated by combining LLM, and the rationality and feasibility of the optimization suggestions are ensured by the results of causal reasoning. LLM will generate a detailed optimization explanation report to help developers understand the reasons, expected effects and possible side effects of the optimization plan, and provide optimization suggestions on how to balance performance and maintainability. In summary, the present invention provides an accurate, explainable, and efficient Java code quality optimization method, which can help developers reduce unnecessary optimization risks while improving code quality, 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 implemented to ensure that the quality improvement of Java code meets the actual needs of the project.
[0059] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment. The above is only a specific implementation method of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or replacements that can be easily thought of by any engineer familiar with the technical field disclosed by the present invention should be covered within the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A code quality optimization method based on causal reasoning and LLM, characterized in that: include: S1. Analyze the target code data and establish the corresponding causal relationship model; S2. Using the causal relationship model, simulate the causal intervention effects of different optimization strategies on code quality; S3, using the large language model to generate an optimization plan based on the obtained simulation results; S4. Record the modification to the target code data and verify the optimization effect.
2. The code quality optimization method based on causal reasoning and LLM according to claim 1 is characterized in that: In S1, it includes: Obtaining core indicator information of the target code data; The core indicator information is used to establish a causal relationship model, and the causal relationship model is used to calculate the impact of each core indicator on the target variable.
3. The code quality optimization method based on causal reasoning and LLM according to claim 1, characterized in that: The core indicator information of obtaining the target code data includes: Scanning the code base of the target code data by using a static analysis tool to obtain the code complexity of the target code data and determine maintainability and defect density; Among them, the indicators corresponding to the code complexity include: the cyclomatic complexity, function complexity and nesting depth of the code of the corresponding module, class and method obtained from the target code, wherein the cyclomatic complexity is expressed as V(G)=E-N+2P, the function complexity is expressed as Fc=L+B, the value of the nesting depth N is equal to the number of layers of the maximum nested conditional statement or loop statement, V(G) represents the loop complexity of the graph, E is the number of edges in the control flow graph, N is the number of nodes in the control flow graph, P is the number of independent connected areas in the program, Fc is the function complexity, L is the number of lines of code in the function, and B is the number of branches in the function; the code complexity index is expressed as C=w1*V(G)+w2*Fc+w3*N+w4*D, w1, w2, w3, w4 are the weight coefficients corresponding to the four code complexity indicators, and D is the code repetition; The maintainability index is expressed as M=w'1*V(G)+w'2*L+w'3*D+w'4*C+w'5*CR, where M is the maintainability score of the code, C is the module coupling degree, CR is the comment rate, and w'1, w'2, w'3, w'4, and w'5 represent the weight coefficients corresponding to the five maintainability indicators; Defect density d = number of defects / number of lines of code*1000.
4. The code quality optimization method based on causal reasoning and LLM according to claim 3 is characterized in that: The corresponding relationship between the weight coefficients of the four code complexity indicators and the project scenario types includes: For rapid development project scenarios: w1=0.2,w2=0.3,w3=0.2,w4=0.3; For enterprise-level application scenarios: w1=0.3, w2=0.4, w3=0.2, w4=0.1; For performance optimization project scenarios: w1=0.4, w2=0.2, w3=0.3, w4=0.1; For fast iteration product scenarios: w1=0.25,w2=0.25,w3=0.25,w4=0.
25.
5. The code quality optimization method based on causal reasoning and LLM according to claim 3 or 4, characterized in that: The corresponding relationship between the weight coefficients of the five maintainability indicators and the project scenario types includes: For rapid development project scenarios: w'1=0.3, w'2=0.2, w'3=0.2, w'4=0.15, w'5=0.15; For enterprise-level application scenarios: w'1=0.35, w'2=0.3, w'3=0.15, w'4=0.1, w'5=0.1; For performance optimization project scenarios: w'1=0.4, w'2=0.2, w'3=0.15, w'4=0.15, w'5=0.1; For fast iteration product scenarios: w'1=0.25,w'2=0.25,w'3=0.2,w'4=0.2,w'5=0.
1.
6. The code quality optimization method based on causal reasoning and LLM according to claim 3 is characterized in that: The use of the core indicator information to establish a causal relationship model includes: A causal reasoning model Do-Calculus is established, in which the causal graph shows: C → Q, which is used to indicate that code complexity affects code quality, d → Q, which is used to indicate that defect density affects code quality, and M → Q, which is used to indicate that maintainability affects code quality; Code quality is expressed as Q=β0+β1*C+β2*d+β3*M+ϵ1, C=γ1*D+γ2*M+ϵ2, d=λ1*M+ϵ3, where Q is the code quality, β0 is the constant term, β1, β2, β3 are three regression coefficients to be estimated, which are used to indicate the causal impact of the core indicators on code quality, γ1, γ2 represent the influence coefficients of defect density and maintainability on code complexity, λ1 represents the influence coefficient of maintainability on defect density, and ϵ1~ϵ3 represent the first to third error terms.
7. The code quality optimization method based on causal reasoning and LLM according to claim 1 is characterized in that: In S2, it includes: Through the Do-Calculus method and Counterfactual Analysis, the causal intervention effects of different optimization strategies on code quality are simulated; The Do-Calculus method is used to deduce the causal intervention effect, which is expressed as: P(Q|do(C=c′),do(d=d′),do(M=m′)), where c′, d′, m′ represent the modified code complexity, defect density and maintainability in the Do-Calculus method; The counterfactual analysis is used to exclude strategies that will lead to negative optimization, expressed as: P(Q|C,d,M)vsP(Q|C′,d′,M′), where C′,d′,M′ represent the modified code complexity, defect density and maintainability in the counterfactual analysis.
8. A code quality optimization device based on causal reasoning and LLM, characterized in that: include: An analysis engine module, used to parse the target code data and establish a corresponding causal relationship model; A simulation module, used to simulate the causal intervention effects of different optimization strategies on code quality by using the causal relationship model; A solution generation module is used to generate an optimization solution based on the obtained simulation results using a large language model; The update module is used to record the modification to the target code data, perform regression testing and verify the optimization effect.
9. A storage medium, characterized in that: include: A computer program or instruction is stored, and when the computer program or instruction is executed, the method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Software testability analysis, design and evaluation method
CN116303038A
Code generation method and device, equipment, storage medium and program product
CN118502768A
Code review and optimization method driven by large language model
CN119512556A
Model validation as a service
US12086053B1
Systems and methods for generating natural language using language models trained on computer code
US20240020116A1
Cited By
Agent research and development-oriented workflow template verification method and system, and Agent research and development-oriented workflow template optimization method and system
CN122132269A