Incremental code odor detection and reconstruction recommendation system and method thereof
By combining static analysis, dynamic analysis, machine learning and incremental update technologies, an incremental code odor detection and reconstruction recommendation system is built, which solves the problem of insufficient quality of code odor detection accuracy and reconstruction recommendation in the existing technology, and achieves more efficient and accurate code quality management.
Patent Information
- Application Number
- CN202411416197.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-11
- Publication Date
- 2025-03-11
- Estimated Expiration
- 2044-10-11
AI Technical Summary
Existing code odor detection methods rely on static analysis and cannot capture the dynamic behavior of runtime. The refactoring recommendation lacks adaptive learning ability, resulting in insufficient detection accuracy and recommendation quality.
Combining static analysis, dynamic analysis, machine learning and incremental update technologies, an incremental code odor detection and reconstruction recommendation system is built. The system generates vector representations of the potential features of the code through code extraction and static analysis, runtime information collection and dynamic analysis, and the comprehensive use of learning modules, and continuously optimizes detection and recommendation capabilities through incremental learning models.
It realizes more accurate code odor detection and more targeted reconstruction suggestions, which can adapt to the characteristics of different projects and the feedback from developers, and improves code quality and development efficiency.
Smart Images

Figure CN119597614A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of code smell detection and reconstruction recommendation system, in particular to an incremental code smell detection and reconstruction recommendation system and method thereof. Background Art
[0002] As software systems continue to expand in size and complexity, code quality management has become an important challenge in the field of software engineering. In the past few decades, the industry and academia have been exploring various methods to detect code smells and provide refactoring suggestions to improve the maintainability and performance of software.
[0003] Traditional code smell detection methods mainly rely on static analysis techniques. These methods identify potential design flaws and bad programming practices by analyzing the structure and syntax of source code. For example, common static analysis tools can detect problems such as overly long methods, duplicated code, and overly complex control flows. These tools play an important role in the software development process, helping developers to discover and resolve potential code problems early.
[0004] However, as software systems become more and more complex, methods that rely solely on static analysis have gradually revealed their limitations. First, static analysis cannot capture dynamic behavior at runtime, which means that some problems that can only be exposed during program execution, such as memory leaks and concurrency issues, are often difficult to detect. Second, traditional static analysis tools usually use predefined rule sets that may not be well adapted to the specific needs and coding styles of different projects, resulting in a high false positive rate and reducing developers' trust in the tools.
[0005] On the other hand, some researchers have tried to introduce dynamic analysis techniques to make up for the shortcomings of static analysis. Dynamic analysis can more accurately reflect the actual behavior of a program by collecting information while the program is running. However, pure dynamic analysis also faces the problem of insufficient coverage because it can only analyze the code path that is actually executed. In addition, dynamic analysis usually requires additional runtime overhead, which may affect the performance of the system in some cases.
[0006] In terms of refactoring suggestions, existing technologies also have some shortcomings. The refactoring suggestions provided by most tools are often too general and do not fully consider the context and requirements of specific projects. This leads to many refactoring suggestions that, although correct in theory, may not be appropriate or difficult to implement in practical applications. In addition, existing refactoring recommendation systems usually lack learning capabilities and cannot improve the quality of their suggestions based on developer feedback.
[0007] In the face of these challenges, the industry urgently needs a code smell detection and refactoring recommendation system that can comprehensively utilize the advantages of static analysis and dynamic analysis and has adaptive learning capabilities. Such a system should not only be able to accurately identify potential problems in the code, but also provide practical and targeted refactoring suggestions, and be able to continuously learn and improve from practice. Summary of the invention
[0008] The present invention proposes an innovative solution to the above technical problems. By organically combining static analysis, dynamic analysis, machine learning and incremental update technologies, the present invention aims to build an efficient, accurate and adaptive code smell detection and refactoring recommendation system and method.
[0009] The present invention proposes an incremental code smell detection and reconstruction recommendation system, which includes:
[0010] The code extraction and static analysis module is used to extract the code fragments to be analyzed from the target project and preprocess them to form the files to be analyzed;
[0011] A runtime information collection and dynamic analysis module is used to run the file to be analyzed in a sandbox environment, collect and summarize runtime information, and form a dynamic analysis report;
[0012] A learning module, configured to receive the static analysis result and the dynamic analysis report, comprehensively consider the static information and the incremental model update report, construct a vector representation of the potential features of the code, and update the incremental learning model;
[0013] The refactoring recommendation module is used to score each code snippet based on the code feature vector and the incremental learning model, and recommend corresponding refactoring rules.
[0014] Preferably, the code extraction and static analysis module includes:
[0015] A code extraction unit, used to extract code snippets to be analyzed from the target project;
[0016] Syntax analysis units, which are used to retain the basic syntax units corresponding to the methods in the project;
[0017] A preprocessing unit, used for preprocessing the code snippet to generate a file to be analyzed;
[0018] The preprocessing unit completes the preprocessing by traversing the file, removing comments, and completing each function body with an empty statement.
[0019] Preferably, the runtime information collection and dynamic analysis module includes:
[0020] Sandbox environment unit, used to provide a virtual code execution environment;
[0021] The code instrumentation unit uses JVMTI as a code instrumentation tool to dynamically obtain data and execute method calls during program execution;
[0022] An information collection unit, which is used to collect runtime information, including class load time, number of method executions, exceptions thrown or triggered, and number of object instantiations;
[0023] The analysis report generating unit is used to associate the runtime information with the semantic information of the code to generate a dynamic analysis report.
[0024] Preferably, the learning module includes:
[0025] A static feature extraction unit, used to extract static features from the file to be analyzed;
[0026] A dynamic feature extraction unit, used to extract dynamic features from the dynamic analysis report;
[0027] The feature fusion unit is used to fuse the static features and the dynamic features to generate a vector representation of the potential features of the code. The fusion process adopts the following formula:
[0028] V new =α*V static +(1-α)*V dynamic
[0029] Among them, V new is the updated feature vector, V static is the static eigenvector, V dynamic is the dynamic feature vector, α is the static analysis weight coefficient, and its value range is [0,1];
[0030] The incremental learning unit is used to update the incremental learning model based on the vector representation of the potential features of the code.
[0031] Preferably, the incremental learning unit adopts a neural network model, including an input layer, a hidden layer and an output layer, wherein:
[0032] The input layer receives sequence data based on abstract syntax trees and tokens;
[0033] The hidden layer is composed of multiple layers of neurons, each layer having an activation function;
[0034] The output layer outputs a vector representation of potential features of the code;
[0035] The forward propagation process of the neural network can be expressed as:
[0036] H l =σ(W l *H (l-1) +b l )
[0037] Among them, H l is the output of the lth layer, σ is the activation function, W l is the weight matrix of the lth layer, b l is the bias vector of the lth layer, H (l-1) is the output of the previous layer.
[0038] Preferably, the reconstruction recommendation module includes:
[0039] A scoring unit, used for scoring each code snippet based on the code feature vector;
[0040] A rule matching unit, used for matching rules in a reconstruction rule database according to the scoring result;
[0041] A recommendation generating unit, configured to generate a reconstruction recommendation based on the matching result;
[0042] The scoring unit uses the following formula for scoring:
[0043] Score=∑(w i *f i (V,R)
[0044] Among them, Score is the score of the code snippet, w i is the weight of the i-th scoring factor, f i is the i-th scoring function, V is the code feature vector, and R is the reconstruction rule set.
[0045] The incremental code smell detection and refactoring recommendation method of the system comprises the following steps:
[0046] Extract the code snippets to be analyzed from the target project and preprocess them to form files to be analyzed;
[0047] Run the file to be analyzed in the sandbox environment, collect and summarize runtime information, and form a dynamic analysis report;
[0048] Based on the file to be analyzed and the dynamic analysis report, construct a vector representation of potential features of the code, and update the incremental learning model;
[0049] Each code snippet is scored using the vector representation of the code's latent features and the incremental learning model, and corresponding refactoring rules are recommended.
[0050] Preferably, the step of constructing a vector representation of the potential features of the code comprises:
[0051] Using as input a triplet consisting of a vector representation of the code, a potential problem with the code, and the project progress the code is in;
[0052] Use the existing model to predict the target vector and generate the prediction result of the existing model;
[0053] Predict the newly added target vector and generate the prediction result of the incremental model;
[0054] Compare the actual result of the newly added target vector with the result predicted by the incremental model. If they are inconsistent, adjust the incremental model.
[0055] The comparison step uses cosine similarity to calculate the similarity between the true vector and the predicted vector:
[0056]
[0057] Among them, cos_sim(a,b) is the cosine similarity of two vectors, ||a|| is the modulus of vector a, and ||b|| is the modulus of vector b.
[0058] Preferably, when the cosine similarity is lower than a preset threshold, the model is optimized using a gradient descent algorithm, and the optimization goal is:
[0059] L = -cos_sin(y pred ,y true )
[0060]
[0061] Among them, L is the objective function, y pred is the prediction vector, y true is the real vector, is the update direction of the model parameters, and θ is the model parameter.
[0062] Preferably, the step of scoring each code snippet comprises:
[0063] Reconstruct rule records in the rule database using feature vectors, items, and language type matching:
[0064] R=Match(Q,DB)
[0065] Where Q is the input file to be analyzed, R is the rule matched by the file, and DB is the reconstruction rule database;
[0066] Calculate the score for each rule record:
[0067] Score=∑(w i *SQ i )
[0068] Among them, SQ i is the score of the i-th evaluation index, w i is the weight coefficient of the i-th evaluation index;
[0069] The reconstruction rules are sorted according to the scores, and several rules with the highest scores are selected as reconstruction recommendation results.
[0070] The beneficial effects of the present invention are mainly reflected in the following aspects:
[0071] The core of the present invention lies in its unique incremental learning mechanism and multi-dimensional analysis method. By integrating static code features and dynamic runtime information, the system can fully capture all aspects of the code, thereby providing more accurate odor detection results. At the same time, the incremental learning model enables the system to continuously learn from new code samples and developer feedback, and continuously optimize its detection and recommendation capabilities. This method effectively solves the limitations of static analysis and dynamic analysis, and realizes the complementarity and synergy of the advantages of both.
[0072] In terms of refactoring recommendations, this paper adopts a multi-factor scoring-based approach that comprehensively considers multiple dimensions such as code features, project context, and historical data. This approach not only improves the accuracy and feasibility of recommendations, but also provides personalized recommendations based on the characteristics of different projects. By introducing machine learning technology, the system can learn from past refactoring practices and gradually improve the quality and pertinence of its recommendations.
[0073] Another significant advantage of the present invention is its incremental update mechanism. Traditional code analysis tools usually require a comprehensive scan of the entire code base, which may consume a lot of time and resources in large projects. In contrast, the incremental method of the present invention only needs to analyze the newly added or modified code parts, which greatly improves efficiency and enables the system to better adapt to the modern rapid iteration development model.
[0074] In addition, the sandbox execution environment of the present invention provides a safe and reliable platform for dynamic analysis, effectively solving the security risks and performance impacts that may be caused by dynamic analysis. Through the carefully designed code instrumentation technology, the system can comprehensively collect runtime information without affecting the original code logic, providing rich data support for odor detection and reconstruction recommendations.
[0075] In general, this paper successfully solves many challenges faced by traditional code smell detection and refactoring recommendation systems by innovatively integrating multiple advanced technologies. It can not only identify code problems more accurately and provide more valuable refactoring suggestions, but also continuously improve its own capabilities through continuous learning. This method is expected to significantly improve the efficiency and quality of software development, reduce maintenance costs, and bring revolutionary improvements to software engineering practice. BRIEF DESCRIPTION OF THE DRAWINGS
[0076] Figure 1 It is the overall logic block diagram of the system of the present invention.
[0077] Figure 2 It is a logic block diagram of the code extraction and static analysis module of the present invention.
[0078] Figure 3 It is a logic block diagram of the runtime information collection and dynamic analysis module of the present invention.
[0079] Figure 4 It is a logic block diagram of the learning module of the present invention.
[0080] Figure 5 It is a logical block diagram of the reconstruction recommendation module of the present invention.
[0081] Figure 6 It is a logical block diagram of the incremental learning unit of the present invention.
[0082] Figure 7 It is a logic block diagram of the scoring unit of the present invention. DETAILED DESCRIPTION
[0083] In order to further explain the technical means and effects adopted by the present invention to achieve the predetermined invention purpose, the specific implementation methods, structures, features and effects thereof are described in detail below in conjunction with the accompanying drawings and preferred embodiments. In the following description, different "one embodiment" or "another embodiment" does not necessarily refer to the same embodiment. In addition, specific features, structures or characteristics in one or more embodiments may be combined in any suitable form.
[0084] Unless defined otherwise, all 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.
[0085] Example 1
[0086] See also Figure 1-7The present invention provides an incremental code smell detection and refactoring recommendation system, which includes a code extraction and static analysis module 1, a runtime information collection and dynamic analysis module 2, a learning module 3 and a refactoring recommendation module 4. The code extraction and static analysis module 1 is used to extract the code fragments to be analyzed from the target project and preprocess them to form files to be analyzed. The runtime information collection and dynamic analysis module 2 is used to run the files to be analyzed in a sandbox environment, collect and summarize runtime information, and form a dynamic analysis report. The learning module 3 is used to receive the static analysis results and the dynamic analysis report, comprehensively consider the static information and the incremental model update report, construct a vector representation of the potential features of the code, and update the incremental learning model. The refactoring recommendation module 4 is used to score each code fragment based on the code feature vector and the incremental learning model, and recommend corresponding refactoring rules.
[0087] In one embodiment of the present invention, the code extraction and static analysis module 1 includes a code extraction unit 11, a syntax analysis unit 12 and a preprocessing unit 13. The code extraction unit 11 is used to extract the code fragments to be analyzed from the target project. The syntax analysis unit 12 is used to retain the basic syntax units corresponding to the methods in the project. The preprocessing unit 13 is used to preprocess the code fragments to generate files to be analyzed. Among them, the preprocessing unit 13 completes the preprocessing by traversing the file, removing comments, and completing each function body with an empty statement. This design enables the system to effectively extract and preprocess the code to be analyzed, laying the foundation for subsequent analysis work.
[0088] Next, in another embodiment of the present invention, the runtime information collection and dynamic analysis module 2 includes a sandbox environment unit 21, a code plug-in unit 22, an information collection unit 23 and an analysis report generation unit 24. The sandbox environment unit 21 is used to provide a virtual code execution environment to ensure the security of code execution. The code plug-in unit 22 uses JVMTI as a code plug-in tool to dynamically obtain data and execute method calls during program execution. The information collection unit 23 is used to collect runtime information, including key indicators such as class load time, number of executions of methods, exceptions thrown or triggered, and number of object instantiations. The analysis report generation unit 24 is used to associate the runtime information with the semantic information of the code to generate a dynamic analysis report. This module design can fully capture the behavioral characteristics of the code at runtime and provide rich dynamic information for subsequent code odor detection.
[0089] Again, in another embodiment of the present invention, the learning module 3 includes a static feature extraction unit 31, a dynamic feature extraction unit 32, a feature fusion unit 33 and an incremental learning unit 34. The static feature extraction unit 31 is used to extract static features from the file to be analyzed, such as code structure, complexity, etc. The dynamic feature extraction unit 32 is used to extract dynamic features from the dynamic analysis report, such as execution frequency, abnormal conditions, etc. The feature fusion unit 33 is used to fuse the static features and the dynamic features to generate a vector representation of the potential features of the code. The fusion process adopts the following formula:
[0090] V new =α*V static +(1-α)*V dynamic
[0091] Among them, V new is the updated feature vector, V static is the static eigenvector, V dynamic is the dynamic feature vector, and α is the static analysis weight coefficient, which ranges from [0,1]. In practical applications, the value of α can be adjusted according to the characteristics of the project. For example, for computationally intensive projects, α can be set to 0.3 to consider more dynamic features; for logically complex projects, α can be set to 0.7 to consider more static features.
[0092] The incremental learning unit 34 is used to update the incremental learning model based on the vector representation of the potential features of the code. This design can effectively integrate static and dynamic features, and continuously optimize the model through incremental learning to improve the accuracy and adaptability of code smell detection.
[0093] Preferably, the incremental learning unit 34 adopts a neural network model, including an input layer, a hidden layer and an output layer. The input layer receives sequence data based on an abstract syntax tree and a token. This design can make full use of the structure and semantic information of the code. The hidden layer is composed of multiple layers of neurons, each layer having an activation function, such as a ReLU or sigmoid function, for introducing nonlinear characteristics. The output layer outputs a vector representation of the potential features of the code. The forward propagation process of the neural network can be expressed as:
[0094] H l =σ(W l *H (l-1) +b l )
[0095] Among them, H l is the output of the lth layer, σ is the activation function, W l is the weight matrix of the lth layer, b l is the bias vector of the lth layer, H (l-1)is the output of the previous layer. This neural network structure can effectively learn and represent the complex features of the code, providing strong support for code smell detection.
[0096] Then, in another embodiment of the present invention, the reconstruction recommendation module 4 includes a scoring unit 41, a rule matching unit 42 and a recommendation generating unit 43. The scoring unit 41 is used to score each code snippet based on the code feature vector. The rule matching unit 42 is used to match the rules in the reconstruction rule database according to the scoring result. The recommendation generating unit 43 is used to generate a reconstruction recommendation based on the matching result. Among them, the scoring unit 41 uses the following formula for scoring:
[0097] Score=∑(w i *f i (V,R)
[0098] Among them, Score is the score of the code snippet, w i is the weight of the i-th scoring factor, f i is the i-th scoring function, V is the code feature vector, and R is the refactoring rule set. This scoring mechanism can comprehensively consider multiple factors, such as code complexity, maintainability, performance impact, etc., to generate more accurate and targeted refactoring suggestions.
[0099] The present invention also provides an incremental code smell detection and refactoring recommendation method based on the above system. The method first extracts the code fragment to be analyzed from the target project and preprocesses it to form a file to be analyzed. This step is completed by the code extraction and static analysis module 1, which can effectively prepare the code material to be analyzed.
[0100] Next, the method runs the file to be analyzed in a sandbox environment, collects and summarizes runtime information, and forms a dynamic analysis report. This step is completed by the runtime information collection and dynamic analysis module 2, which can safely collect key information when the code is running.
[0101] Then, the method constructs a vector representation of the potential features of the code based on the file to be analyzed and the dynamic analysis report, and updates the incremental learning model. This step is completed by the learning module 3, which can effectively integrate static and dynamic features and continuously optimize the learning model.
[0102] Finally, the method uses the vector representation of the potential features of the code and the incremental learning model to score each code snippet and recommend corresponding refactoring rules. This step is completed by the refactoring recommendation module 4, which can provide developers with accurate refactoring suggestions.
[0103] In a preferred embodiment of the present invention, the step of constructing a vector representation of potential features of the code includes using a triple as input, the triple consisting of the vector representation of the code, the potential problems of the code, and the project progress of the code. This design can comprehensively consider the characteristics, quality issues, and project context of the code, thereby generating a more accurate feature representation.
[0104] Next, the method uses the existing model to predict the target vector and generates the prediction results of the existing model. Then, the newly added target vector is predicted to generate the prediction results of the incremental model. These two steps can effectively utilize existing knowledge and adapt to new code features.
[0105] Then, the method compares the actual result of the newly added target vector with the result predicted by the incremental model. If they are inconsistent, the incremental model is adjusted. This step uses cosine similarity to calculate the similarity between the actual vector and the predicted vector:
[0106]
[0107] Among them, cos_sim(a,b) is the cosine similarity of two vectors, ||a|| is the modulus of vector a, and ||b|| is the modulus of vector b. In practical applications, a similarity threshold can be set, such as 0.95. When the similarity is lower than this threshold, the system will consider that there is a significant difference between the predicted result and the actual result, and the model needs to be adjusted. The selection of this threshold is based on the balance between prediction accuracy and computational efficiency.
[0108] In another preferred embodiment of the present invention, when the cosine similarity is lower than a preset threshold, a gradient descent algorithm is used to optimize the model, and the optimization goal is:
[0109] L = -cos_sin(y pred ,y true )
[0110]
[0111] Among them, L is the objective function, y pred is the prediction vector, y true is the real vector, is the update direction of the model parameters, and θ is the model parameter. This optimization method can effectively improve the prediction accuracy of the model, thereby continuously improving the effect of code smell detection.
[0112] Finally, in another preferred embodiment of the present invention, the step of scoring each code snippet includes matching the rule record in the reconstruction rule database using the feature vector, the project and the language type:
[0113] R=Match(Q,DB)
[0114] Among them, Q is the input file to be analyzed, R is the rule matched by the file, and DB is the reconstruction rule database. Then, calculate the score of each rule record:
[0115] Score=∑(w i *SQ i )
[0116] Among them, SQ i is the score of the i-th evaluation index, w i is the weight coefficient of the i-th evaluation indicator. In practical applications, different weights can be set according to different code quality concerns. For example, for performance-sensitive projects, the weight of performance-related indicators can be set to 0.4, the weight of maintainability indicators can be set to 0.3, and the weight of other indicators can be set to 0.3. This flexible weight setting can adapt to the needs of different projects and generate more targeted refactoring suggestions.
[0117] Finally, the method sorts the refactoring rules according to the scores and selects several rules with the highest scores as refactoring recommendation results. Usually, the top 5 rules with the highest scores can be selected as recommendation results, which can provide enough refactoring options without making developers feel overloaded with information.
[0118] Through the above design, the incremental code smell detection and refactoring recommendation system and method of the present invention can effectively identify potential problems in the code and provide targeted refactoring suggestions. The system not only takes into account the static characteristics of the code, but also integrates the dynamic information at runtime, so that the code quality can be evaluated more comprehensively and accurately. At the same time, through the incremental learning mechanism, the system can continuously adapt to new code patterns and project characteristics, and improve the accuracy of detection and recommendation. This method is of great significance for improving software quality and reducing maintenance costs, and is particularly suitable for large-scale, long-term maintained software projects.
[0119] In order to verify the superiority of the incremental code smell detection and refactoring recommendation system of the present invention, we selected a large open source project Apache Hadoop as the test dataset. Apache Hadoop is a widely used distributed computing framework with a large and complex code base, which is very suitable for evaluating the performance of our system.
[0120] Example 1: Incremental code smell detection and reconstruction recommendation system of the present invention
[0121] In this example, we used the complete system described in the present invention, including code extraction and static analysis module, runtime information collection and dynamic analysis module, learning module and refactoring recommendation module. The system parameters are set as follows:
[0122] 1. The static analysis weight coefficient α is set to 0.6 to balance the influence of static and dynamic features.
[0123] 2. The neural network uses 3 hidden layers, each with 128, 64, and 32 neurons respectively.
[0124] 3. The similarity threshold is set to 0.95 to trigger model adjustment.
[0125] 4. In the refactoring rule scoring, the performance index weight is 0.4, the maintainability index weight is 0.3, and the other index weights are 0.3.
[0126] Comparative Example 1: Traditional Static Code Analysis Tools
[0127] For comparison, we chose a widely used static code analysis tool, SonarQube. SonarQube mainly relies on static analysis and does not include dynamic analysis and incremental learning capabilities.
[0128] We conducted a three-month tracking analysis of the core modules of the Apache Hadoop project, performing code inspections and generating refactoring suggestions every week. We focused on the following indicators:
[0129] 1. Code smell detection accuracy: the ratio of the number of correctly identified code smells to the total number of identified code smells.
[0130] 2. False Alarm Rate: The ratio of the number of incorrectly identified code smells to the total number of identifications.
[0131] 3. Refactoring suggestion adoption rate: the ratio of the number of refactoring suggestions adopted by developers to the total number of suggestions.
[0132] 4. Code quality improvement: measured by the degree of improvement in indicators such as code complexity and maintainability index.
[0133] 5. Performance impact: Changes in code execution time before and after refactoring.
[0134] Detection method:
[0135] 1. Code smell detection accuracy and false positive rate: Three experienced senior developers manually review the code smells marked by the system to determine their accuracy.
[0136] 2. Refactoring suggestion adoption rate: Count the number of refactoring suggestions adopted in the project submission records.
[0137] 3. Improve code quality: Use tools to measure code complexity (such as cyclomatic complexity) and maintainability index, and compare the changes before and after refactoring.
[0138] 4. Performance impact: Use the performance test suite provided by Apache Hadoop to compare the execution time before and after the reconstruction.
[0139] The test results are shown in the following table:
[0140]
[0141]
[0142] From the test results, we can see that the system of the present invention is significantly better than traditional static analysis tools in all aspects. Let's analyze these results in depth:
[0143] 1. Code smell detection accuracy: The accuracy of the system of the present invention reaches 92%, which is much higher than the 78% of traditional tools. This is mainly due to the fact that we combine static analysis and dynamic analysis to capture code problems more comprehensively. In particular, some problems that can only be found at runtime, such as memory leaks or concurrency problems, are often difficult to identify with traditional static analysis tools.
[0144] 2. False alarm rate: The false alarm rate of the system of the present invention is only 5%, while that of traditional tools is as high as 15%. Low false alarm rate means that developers can trust the suggestions of the system more, thus improving work efficiency. This is due to our incremental learning module, which can continuously learn from feedback and improve the accuracy of judgment.
[0145] 3. Refactoring suggestion adoption rate: Our system achieved a 76% adoption rate, much higher than the 45% of traditional tools. This shows that our suggestions are more feasible and more in line with the actual needs of the project. This is because our system not only considers the code itself, but also the project context and runtime behavior, and can provide more accurate suggestions.
[0146] 4. Improved code quality: Our system has achieved better results in reducing code complexity and improving maintainability. The complexity has been reduced by 18% and the maintainability has been improved by 22%. These improvements will greatly reduce future maintenance costs.
[0147] 5. Performance impact: Interestingly, although our refactoring suggestions resulted in more code changes, the performance improvement was more significant (5% vs. 1%). This suggests that our system is able to identify some deep-seated issues that affect performance, which may have been overlooked by traditional tools.
[0148] These results fully demonstrate the superiority of our system. It can not only identify code problems more accurately, but also provide more valuable refactoring suggestions. It is particularly noteworthy that the high adoption rate and significant improvement in code quality show that our system can adapt well to the actual development environment and bring substantial help to the development team.
[0149] Based on the above test results, we believe that the current parameter settings have achieved good results and can be used as the best implementation. In particular, the static analysis weight coefficient α is set to 0.6, which well balances the advantages of static analysis and dynamic analysis. In future applications, this parameter can be adjusted appropriately according to the characteristics of specific projects. For example, the α value can be slightly reduced for computationally intensive projects, and slightly increased for logically complex projects.
[0150] In general, these test results fully demonstrate the great potential of the system in improving code quality and reducing maintenance costs. It can not only help the development team identify and solve code problems faster, but also continuously improve their own analysis capabilities through continuous learning, bringing long-term positive impact to the software development process.
[0151] It should be noted that the above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the principles of the present invention should be included in the protection scope of the present invention.
Claims
1. Incremental code smell detection and refactoring recommendation system, characterized by: The system includes: The code extraction and static analysis module is used to extract the code fragments to be analyzed from the target project and preprocess them to form the files to be analyzed; A runtime information collection and dynamic analysis module is used to run the file to be analyzed in a sandbox environment, collect and summarize runtime information, and form a dynamic analysis report; A learning module, configured to receive the static analysis result and the dynamic analysis report, comprehensively consider the static information and the incremental model update report, construct a vector representation of the potential features of the code, and update the incremental learning model; The refactoring recommendation module is used to score each code snippet based on the code feature vector and the incremental learning model, and recommend corresponding refactoring rules.
2. The system according to claim 1, characterized in that The code extraction and static analysis module includes: A code extraction unit, used to extract code snippets to be analyzed from the target project; Syntax analysis units, which are used to retain the basic syntax units corresponding to the methods in the project; A preprocessing unit, used for preprocessing the code snippet to generate a file to be analyzed; The preprocessing unit completes the preprocessing by traversing the file, removing comments, and completing each function body with an empty statement.
3. The system according to claim 1, characterized in that The runtime information collection and dynamic analysis module includes: Sandbox environment unit, used to provide a virtual code execution environment; The code instrumentation unit uses JVMTI as a code instrumentation tool to dynamically obtain data and execute method calls during program execution; An information collection unit, which is used to collect runtime information, including class load time, number of method executions, exceptions thrown or triggered, and number of object instantiations; The analysis report generating unit is used to associate the runtime information with the semantic information of the code to generate a dynamic analysis report.
4. The system according to claim 1, characterized in that The learning modules include: A static feature extraction unit, used to extract static features from the file to be analyzed; A dynamic feature extraction unit, used to extract dynamic features from the dynamic analysis report; The feature fusion unit is used to fuse the static features and the dynamic features to generate a vector representation of the potential features of the code. The fusion process adopts the following formula: V new =α*V static +(1-α)*V dynamuc Among them, V new is the updated feature vector, V static is the static eigenvector, V dynamic is the dynamic feature vector, α is the static analysis weight coefficient, and its value range is [0,1]; The incremental learning unit is used to update the incremental learning model based on the vector representation of the potential features of the code.
5. The system according to claim 4, characterized in that The incremental learning unit adopts a neural network model, including an input layer, a hidden layer and an output layer, wherein: The input layer receives sequence data based on abstract syntax trees and tokens; The hidden layer is composed of multiple layers of neurons, each layer having an activation function; The output layer outputs a vector representation of potential features of the code; The forward propagation process of the neural network can be expressed as: H l =σ(W l *H (l-1) +b l ) Among them, H l is the output of the lth layer, σ is the activation function, W l is the weight matrix of the lth layer, b l is the bias vector of the lth layer, H (l-1) is the output of the previous layer.
6. The system according to claim 1, characterized in that The reconstruction recommendation module includes: A scoring unit, used for scoring each code snippet based on the code feature vector; A rule matching unit, used for matching rules in a reconstruction rule database according to the scoring result; A recommendation generating unit, configured to generate a reconstruction recommendation based on the matching result; The scoring unit uses the following formula for scoring: Score=∑(w i *f i (V,R)) Among them, Score is the score of the code snippet, w i is the weight of the i-th scoring factor, f i is the i-th scoring function, V is the code feature vector, and R is the reconstruction rule set.
7. The incremental code smell detection and reconstruction recommendation method based on the system according to any one of claims 1 to 6, characterized in that: The method comprises the following steps: Extract the code snippets to be analyzed from the target project and preprocess them to form files to be analyzed; Run the file to be analyzed in the sandbox environment, collect and summarize runtime information, and form a dynamic analysis report; Based on the file to be analyzed and the dynamic analysis report, construct a vector representation of potential features of the code, and update the incremental learning model; Each code snippet is scored using the vector representation of the code's latent features and the incremental learning model, and corresponding refactoring rules are recommended.
8. The method according to claim 7, characterized in that The step of constructing a vector representation of the potential features of the code comprises: Using as input a triplet consisting of a vector representation of the code, a potential problem with the code, and the project progress the code is in; Use the existing model to predict the target vector and generate the prediction result of the existing model; Predict the newly added target vector and generate the prediction result of the incremental model; Compare the actual result of the newly added target vector with the result predicted by the incremental model. If they are inconsistent, adjust the incremental model. The comparison step uses cosine similarity to calculate the similarity between the true vector and the predicted vector: Among them, cos_sim(a,b) is the cosine similarity of two vectors, ||a|| is the modulus of vector a, and ||b|| is the modulus of vector b.
9. The method according to claim 8, characterized in that When the cosine similarity is lower than the preset threshold, the model is optimized using the gradient descent algorithm, and its optimization goal is: L=-cos_sin(y pred ,and true ) Among them, L is the objective function, y pred is the prediction vector, y true is the real vector, is the update direction of the model parameters, and θ is the model parameter.
10. The method according to claim 7, characterized in that The steps of scoring each code snippet include: Reconstruct rule records in the rule database using feature vectors, items, and language type matching: R=Match(Q,DB) Where Q is the input file to be analyzed, R is the rule matched by the file, and DB is the reconstruction rule database; Calculate the score for each rule record: Score=∑(w i *SQ i ) Among them, SQ i is the score of the i-th evaluation index, w i is the weight coefficient of the i-th evaluation index; The reconstruction rules are sorted according to the scores, and several rules with the highest scores are selected as reconstruction recommendation results.
Citation Information
Patent Citations
Software reconstruction prediction method based on code peculiar smell
CN114138328A
Software system semi-automatic reconstruction method based on class-level network
CN114371871A
Code peculiar smell recognition method based on code semantics and measurement
CN115952076A
Evaluation method based on code syntax tree analysis
CN117331836A
Code odor detection and reconstruction guide technology based on large model
CN118210729A
Cited By
Document slicing and vectorization retrieval optimization method oriented to programming scene
CN121658436A
A document slicing and vectorized retrieval optimization method for programming scenarios
CN121658436B