Incremental code smell detection and refactoring recommendation system and method
By combining static and dynamic analysis with machine learning, the incremental code smell detection and refactoring recommendation system solves the problems of insufficient coverage and high false positive rate in existing technologies. It provides more accurate code problem identification and personalized suggestions, adapts to rapid iterative development, and improves software development efficiency and quality.
Patent Information
- Application Number
- CN202411416197.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-11
- Publication Date
- 2025-11-04
- Estimated Expiration
- 2044-10-11
AI Technical Summary
Existing code smell detection and refactoring recommendation systems suffer from problems such as insufficient static analysis coverage, insufficient dynamic analysis coverage, high false positive rate, and impractical refactoring suggestions when dealing with complex software systems. They also lack adaptive learning capabilities and are unable to comprehensively and accurately identify code problems and provide effective suggestions.
By combining static analysis, dynamic analysis, machine learning, and incremental update techniques, we construct modules for code extraction, static analysis, runtime information collection, learning, and refactoring recommendation. By integrating static and dynamic features and using neural networks for incremental learning, we provide personalized refactoring suggestions.
It achieves more accurate code smell detection and refactoring recommendations, improves detection coverage and the feasibility of suggestions, adapts to rapid iterative development models, reduces false alarm rate and resource consumption, and improves software development efficiency and quality.
Smart Images

Figure CN119597614B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of code smell detection and reconstruction recommendation system technology, and in particular to an incremental code smell detection and reconstruction recommendation system and method. Background Technology
[0002] As software systems continue to grow in size and complexity, code quality management has become a significant challenge in the field of software engineering. Over the past few decades, industry and academia have explored various methods to detect code smells and provide refactoring recommendations to improve software maintainability and performance.
[0003] Traditional code smell detection methods primarily rely on static analysis techniques. These methods identify potential design flaws and poor programming practices by analyzing the structure and syntax of the source code. For example, common static analysis tools can detect issues such as excessively long methods, duplicate code, and overly complex control flow. These tools play a crucial role in the software development process, helping developers identify and resolve potential code problems early on.
[0004] However, as software systems become increasingly complex, the limitations of relying solely on static analysis are becoming increasingly apparent. First, static analysis cannot capture runtime dynamic behavior, meaning that problems that only surface during program execution, such as memory leaks and concurrency issues, are often difficult to detect. Second, traditional static analysis tools typically use predefined rule sets, which may not be well-suited to the specific needs and coding styles of different projects, leading to a high false positive rate and reducing developer trust in the tools.
[0005] On the other hand, some researchers have attempted to introduce dynamic analysis techniques to compensate for the shortcomings of static analysis. Dynamic analysis, by collecting information during program execution, can more accurately reflect the actual behavior of the program. However, pure dynamic analysis also faces the problem of insufficient coverage, as it can only analyze up to the actual executed code path. Furthermore, dynamic analysis typically requires additional runtime overhead, which may impact system performance in some cases.
[0006] Existing technologies also have some shortcomings in terms of refactoring suggestions. Most tools offer overly general suggestions that fail to adequately consider the context and requirements of a specific project. This results in many refactoring suggestions, while theoretically correct, being unsuitable or difficult to implement in practice. Furthermore, existing refactoring recommendation systems typically 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, while having adaptive learning capabilities. Such a system not only needs to accurately identify potential problems in the code, but also needs to provide practical and targeted refactoring suggestions, and can continuously learn and improve from practice. SUMMARY
[0008] The present application is an innovative solution to the above technical problems. By organically combining static analysis, dynamic analysis, machine learning and incremental updating technologies, the present application aims to build an efficient, accurate and adaptive code smell detection and refactoring recommendation system and method.
[0009] The present application proposes an incremental code smell detection and refactoring recommendation system, which comprises:
[0010] A code extraction and static analysis module for extracting code snippets to be analyzed from a target project and preprocessing them to form an analysis file;
[0011] A runtime information collection and dynamic analysis module for running the analysis file in a sandbox environment, collecting and summarizing runtime information, and forming a dynamic analysis report;
[0012] A learning module for receiving the static analysis results and the dynamic analysis report, considering static information and incremental model update reports, constructing a vector representation of code potential features, and updating the incremental learning model;
[0013] A refactoring recommendation module for scoring each code snippet based on the code feature vector and the incremental learning model, and recommending corresponding refactoring rules.
[0014] Preferably, the code extraction and static analysis module comprises:
[0015] A code extraction unit for extracting code snippets to be analyzed from the target project;
[0016] A syntax analysis unit for retaining basic syntax units corresponding to methods in the project;
[0017] A preprocessing unit for preprocessing the code snippets to generate an analysis file;
[0018] Wherein, the preprocessing unit completes preprocessing by traversing the file, removing comments, and completing each function body with empty statements.
[0019] Preferably, the runtime information collection and dynamic analysis module comprises:
[0020] A sandbox environment unit is configured to provide a virtual code execution environment.
[0021] A code instrumentation unit is configured to use the JVMTI as a code instrumentation tool to dynamically obtain data and perform method calls during program execution.
[0022] An information collection unit is configured to collect runtime information, including class load time, method execution frequency, thrown or raised exceptions, and object instantiation quantity.
[0023] An analysis report generation unit is configured to associate the runtime information with the semantic information of the code and generate a dynamic analysis report.
[0024] Preferably, the learning module comprises:
[0025] A static feature extraction unit is configured to extract static features from the file to be analyzed.
[0026] A dynamic feature extraction unit is configured to extract dynamic features from the dynamic analysis report.
[0027] A feature fusion unit is configured to fuse the static features and the dynamic features to generate a vector representation of the code latent features, and the fusion process uses the following formula:
[0028] V new = α * V static + (1-α) * V dynamic
[0029] wherein V new is the updated feature vector, V static is the static feature vector, V dynamic is the dynamic feature vector, and α is a static analysis weight coefficient with a value range of [0, 1].
[0030] An incremental learning unit is configured to update an incremental learning model based on the vector representation of the code latent features.
[0031] Preferably, the incremental learning unit uses a neural network model, which comprises 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 the vector representation of the code latent features.
[0035] The forward propagation process of the neural network can be represented as:
[0036] H l = σ(W l *H (l-1) + b l )
[0037] where H l is the output of the l-th layer, σ is an activation function, W l is the weight matrix of the l-th layer, b l is the bias vector of the l-th layer, and H (l-1) is the output of the previous layer.
[0038] Preferably, the refactoring recommendation module comprises:
[0039] a scoring unit configured to score each code snippet based on the code feature vector;
[0040] a rule matching unit configured to match rules in a refactoring rule database according to the scoring result;
[0041] a recommendation generation unit configured to generate refactoring recommendations based on the matching result.
[0042] The scoring unit scores according to the following formula:
[0043] Score =∑(w i *f i (V,R))
[0044] where 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.
[0045] The system further comprises an incremental code smell detection and refactoring recommendation method, which comprises the following steps:
[0046] extracting code snippets to be analyzed from a target project and preprocessing them to form files to be analyzed;
[0047] running the files to be analyzed in a sandbox environment, collecting and summarizing runtime information to form a dynamic analysis report;
[0048] based on the files to be analyzed and the dynamic analysis report, constructing a vector representation of code latent features and updating an incremental learning model;
[0049] using the vector representation of code latent features and the incremental learning model to score each code snippet and recommend corresponding refactoring rules.
[0050] Preferably, the step of constructing a vector representation of the latent features of the code includes:
[0051] The input consists of a triplet, which is a vector representation of the code, the potential problems of the code, and the project progress of the code.
[0052] Use the existing model to predict the target vector and generate the prediction results of the existing model;
[0053] Predict the newly added target vector and generate the prediction results of the incremental model;
[0054] Compare the actual results of the newly added target vector with the results 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] Here, cos_sim(a,b) is the cosine similarity between the two vectors, ||a|| is the magnitude of vector a, and ||b|| is the magnitude of vector b.
[0058] Preferably, when the cosine similarity is lower than a preset threshold, the gradient descent algorithm is used to optimize the model, and the optimization objective is:
[0059] L = -cos_sin(y pred ,y true )
[0060]
[0061] Where L is the objective function, y pred For the prediction vector, y true For the true vector, θ represents the direction of updating the model parameters.
[0062] Preferably, the step of scoring each code snippet includes:
[0063] Reconstruct rule records in the rule database using feature vectors, item type, 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] wherein, SQ i is the score of the i-th evaluation index, w i is the weight coefficient of the i-th evaluation index;
[0069] According to the score, the refactoring rules are sorted, and several rules with the highest scores are selected as the refactoring recommendation results.
[0070] The beneficial effects of the present application mainly manifest in the following aspects:
[0071] The core of the present application lies in its unique incremental learning mechanism and multi-dimensional analysis method. By fusing static code features and dynamic runtime information, the system can comprehensively capture various aspects of the code, thus providing more accurate smell detection results. At the same time, the incremental learning model enables the system to continuously learn from new code samples and developer feedback, constantly optimizing its detection and recommendation capabilities. This approach effectively addresses the limitations of static analysis and dynamic analysis, achieving a complementary and synergistic effect of their respective advantages.
[0072] In terms of refactoring recommendations, the present application adopts a multi-factor scoring-based method, taking into account 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 suggestions based on the characteristics of different projects. By introducing machine learning techniques, the system can learn from past refactoring practices, gradually improving the quality and relevance of its recommendations.
[0073] Another significant advantage of the present application lies in its incremental updating mechanism. Traditional code analysis tools typically require a comprehensive scan of the entire codebase, which can consume a large amount of time and resources in large projects. In contrast, the incremental approach of the present application only analyzes the newly added or modified code portions, greatly improving efficiency and enabling the system to better adapt to modern rapid iteration development patterns.
[0074] In addition, the sandbox execution environment of the present application provides a safe and reliable platform for dynamic analysis, effectively addressing the security risks and performance impacts that may arise from dynamic analysis. Through carefully designed code instrumentation techniques, the system can comprehensively collect runtime information without affecting the original code logic, providing rich data support for smell detection and refactoring recommendations.
[0075] In summary, the present invention successfully addresses the many challenges faced by traditional code smell detection and refactoring recommendation systems by innovatively integrating multiple advanced technologies. It not only accurately identifies code problems and provides valuable refactoring suggestions, but also continuously improves its capabilities through learning. This approach is expected to significantly improve software development efficiency and quality, reduce maintenance costs, and bring revolutionary improvements to software engineering practices. BRIEF DESCRIPTION OF DRAWINGS
[0076] Figure 1 The overall logic block diagram of the system of the present invention.
[0077] Figure 2 The logic block diagram of the code extraction and static analysis module of the present invention.
[0078] Figure 3 The logic block diagram of the runtime information collection and dynamic analysis module of the present invention.
[0079] Figure 4 The logic block diagram of the learning module of the present invention.
[0080] Figure 5 The logic block diagram of the refactoring recommendation module of the present invention.
[0081] Figure 6 The logic block diagram of the incremental learning unit of the present invention.
[0082] Figure 7 The logic block diagram of the scoring unit of the present invention. DETAILED DESCRIPTION
[0083] In order to further illustrate the technical means and effects adopted by the present invention to achieve the predetermined invention purpose, the specific implementation, structure, features and effects of the preferred embodiments of the present invention are described in detail below in combination with the accompanying drawings. In the following description, different "one embodiment" or "another embodiment" do not necessarily refer to the same embodiment. In addition, the specific features, structures or characteristics in one or more embodiments can be combined in any suitable form.
[0084] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which the present invention belongs.
[0085] Embodiment 1
[0086] Reference Figures 1-7The application provides an incremental code smell detection and refactoring recommendation system, which comprises 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 for extracting code segments to be analyzed from a target project and pre-processing, thereby forming an analysis file. The runtime information collection and dynamic analysis module 2 is used for running the analysis file in a sandbox environment, collecting and summarizing runtime information, thereby forming a dynamic analysis report. The learning module 3 is used for receiving the static analysis result and the dynamic analysis report, comprehensively considering static information and an incremental model update report, constructing a vector representation of code potential features, and updating an incremental learning model. The refactoring recommendation module 4 is used for scoring each code segment based on the code feature vector and the incremental learning model, and recommending corresponding refactoring rules.
[0087] In an embodiment of the application, the code extraction and static analysis module 1 comprises a code extraction unit 11, a syntax analysis unit 12 and a preprocessing unit 13. The code extraction unit 11 is used for extracting code segments to be analyzed from the target project. The syntax analysis unit 12 is used for retaining basic syntax units corresponding to methods in the project. The preprocessing unit 13 is used for preprocessing the code segments to generate an analysis file. The preprocessing unit 13 completes preprocessing by traversing the file, removing comments and supplementing each function body with an empty statement. This design enables the system to effectively extract and preprocess code to be analyzed, laying a foundation for subsequent analysis work.
[0088] Next, in another embodiment of the application, the runtime information collection and dynamic analysis module 2 comprises a sandbox environment unit 21, a code instrumentation unit 22, an information collection unit 23 and an analysis report generation unit 24. The sandbox environment unit 21 is used for providing a virtual code execution environment to ensure the safety of code execution. The code instrumentation unit 22 uses JVMTI as a code instrumentation tool to dynamically obtain data and perform method calls during program execution. The information collection unit 23 is used for collecting runtime information, including class load time, method execution frequency, occurrence of thrown or raised exceptions, object instantiation quantity and other key indicators. The analysis report generation unit 24 is used for associating the runtime information with code semantic information to generate a dynamic analysis report. This module design can comprehensively capture the behavior characteristics of code runtime, providing rich dynamic information for subsequent code smell detection.
[0089] Again, in another embodiment of the present application, the learning module 3 comprises 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 configured to extract static features from the file to be analyzed, such as code structure, complexity, etc. The dynamic feature extraction unit 32 is configured to extract dynamic features from the dynamic analysis report, such as execution frequency, abnormal situation, etc. The feature fusion unit 33 is configured to fuse the static features and the dynamic features to generate a vector representation of the code latent features. Its fusion process adopts the following formula:
[0090] V new = a * V static + (1-a) * V dynamic
[0091] where V new is the updated feature vector, V static is the static feature vector, V dynamic is the dynamic feature vector, and a is the static analysis weight coefficient, with a value range of [0, 1]. In practical applications, the value of a can be adjusted according to the characteristics of the project, for example, for a computationally intensive project, a can be set to 0.3 to give more consideration to dynamic features; while for a logically complex project, a can be set to 0.7 to give more consideration to static features.
[0092] The incremental learning unit 34 is configured to update the incremental learning model based on the vector representation of the code latent features. This design can effectively fuse 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 abstract syntax tree and token, which can fully utilize 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 ReLU or sigmoid function to introduce non-linear characteristics. The output layer outputs the vector representation of the code latent features. The forward propagation process of the neural network can be represented as:
[0094] H l = σ(W l *H (l-1) + b l )
[0095] where H l is the output of the l-th layer, σ is the activation function, W l is the weight matrix of the l-th layer, b l is the bias vector of the l-th layer, and H (l-1)The output of the upper 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 application, the refactoring recommendation module 4 includes a scoring unit 41, a rule matching unit 42, and a recommendation generation 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 refactoring rule database according to the scoring results. The recommendation generation unit 43 is used to generate refactoring recommendations based on the matching results. Wherein, the scoring unit 41 uses the following formula for scoring:
[0097] Score = ∑(w i *f i (V,R)
[0098] Wherein, 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 consider multiple factors such as code complexity, maintainability, performance impact, etc., to generate more accurate and targeted refactoring suggestions.
[0099] The present application also provides an incremental code smell detection and refactoring recommendation method based on the above system. The method first extracts the code snippets to be analyzed from the target project and pre-processes them to form the files to be analyzed. This step is completed by the code extraction and static analysis module 1, which can effectively prepare the code materials to be analyzed.
[0100] Next, the method runs the files 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 about code runtime.
[0101] Then, the method constructs a vector representation of the code's potential features based on the files 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 code's potential features 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 application, the step of constructing the vector representation of the latent features of the code comprises using a triple as input, the triple consisting of the vector representation of the code, the latent problems of the code, and the project progress in which the code is located. This design can comprehensively consider the features of the code, quality problems, and project context, thereby generating a more accurate feature representation.
[0104] Next, the method uses the existing model to predict the target vector, generating the prediction result of the existing model. Then, the newly added target vector is predicted, generating the prediction result of the incremental model. These two steps can effectively utilize existing knowledge and adapt to new code features.
[0105] Subsequently, the method compares the real result of the newly added target vector with the prediction result of the incremental model. If they are inconsistent, the incremental model is adjusted. This step uses cosine similarity to calculate the similarity between the real vector and the predicted vector:
[0106]
[0107] where cos_sim(a, b) is the cosine similarity of two vectors, ||a|| is the norm of vector a, and ||b|| is the norm of vector b. In practical applications, a similarity threshold can be set, for example, 0.95. When the similarity is lower than this threshold, the system considers that there is a significant difference between the prediction result and the real 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 application, when the cosine similarity is lower than the preset threshold, a gradient descent algorithm is used to optimize the model, and the optimization target is:
[0109] L = -cos_sim(y pred ,y true )
[0110]
[0111] where L is the objective function, y pred is the predicted vector, y true is the real vector, is the update direction of the model parameters, and θ is the model parameters. 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 application, the step of scoring each code snippet comprises using the feature vector, project, and language type to match the rule records in the reconstruction rule database:
[0113] R = Match(Q, DB)
[0114] where Q is the input file to be analyzed, R is the rule matched by the file, and DB is the refactoring rule database. Then, the score of each rule record is calculated:
[0115] Score = ∑(w i SQ i )
[0116] where SQ i is the score of the i-th evaluation index, and w i is the weight coefficient of the i-th evaluation index. In practical applications, different weights can be set according to different code quality concerns. For example, for a performance-sensitive project, 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 weights 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 the top several rules with the highest scores as the refactoring recommendation results. Typically, the top 5 rules with the highest scores can be selected as the recommendation results, which can provide sufficient refactoring options without overwhelming the developers with too much information.
[0118] Through the above design, the incremental code smell detection and refactoring recommendation system and method of the present application can effectively identify potential problems in the code and provide targeted refactoring suggestions. The system not only considers the static features of the code, but also integrates dynamic information at runtime, thereby enabling more comprehensive and accurate evaluation of code quality. At the same time, through the incremental learning mechanism, the system can continuously adapt to new code patterns and project characteristics, improving 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 maintenance software projects.
[0119] To verify the superiority of the incremental code smell detection and refactoring recommendation system of the present application, we selected a large open-source project, Apache Hadoop, as the test data set. Apache Hadoop is a widely used distributed computing framework with a large and complex code base, making it an ideal choice for evaluating the performance of our system.
[0120] Example 1: Incremental code smell detection and refactoring recommendation system of the present application
[0121] In this example, we used the complete system described in the present application, including the code extraction and static analysis module, the runtime information collection and dynamic analysis module, the learning module, and the 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 layers of hidden layers 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 reconstruction rule scoring, the performance indicator weight is 0.4, the maintainability indicator weight is 0.3, and the other indicator weight is 0.3.
[0126] Comparative Example 1: Traditional Static Code Analysis Tool
[0127] As a comparison, we selected a widely used static code analysis tool, SonarQube. SonarQube mainly relies on static analysis and does not include dynamic analysis and incremental learning functions.
[0128] We conducted a 3-month tracking analysis on the core modules of the Apache Hadoop project, with weekly code checks and reconstruction suggestion generation. The following indicators were mainly focused on:
[0129] 1. Code smell detection accuracy: the ratio of correctly identified code smells to the total number of identified code smells.
[0130] 2. False positive rate: the ratio of incorrectly identified code smells to the total number of identified code smells.
[0131] 3. Reconstruction suggestion adoption rate: the ratio of adopted reconstruction suggestions to the total number of suggestions.
[0132] 4. Code quality improvement: measured by the improvement in code complexity, maintainability index, etc.
[0133] 5. Performance impact: the change in code execution time before and after reconstruction.
[0134] Detection method:
[0135] 1. Code smell detection accuracy and false positive rate: manually reviewed by 3 experienced senior developers to judge the accuracy of system-labeled code smells.
[0136] 2. Reconstruction suggestion adoption rate: statistics of adopted reconstruction suggestions in project submission records.
[0137] 3. Code quality improvement: use tools to measure code complexity (such as cyclomatic complexity) and maintainability index, and compare changes before and after reconstruction.
[0138] 4. Performance Impact: Using Apache Hadoop's built-in performance test suite, compare execution times before and after refactoring.
[0139] Test results are shown in the following table:
[0140]
[0141]
[0142] From the test results, it can be seen that the system of the present invention is significantly superior to traditional static analysis tools in various aspects. Let's analyze these results in depth:
[0143] 1. Code smell detection accuracy: The accuracy of the system of the present invention reached 92%, much higher than the 78% of traditional tools. This is mainly due to the combination of static analysis and dynamic analysis, which can more comprehensively capture code problems. Especially some problems that can only be found at runtime, such as memory leaks or concurrency problems, traditional static analysis tools often have difficulty identifying.
[0144] 2. False positive rate: The false positive rate of the system of the present invention is only 5%, while the traditional tool is as high as 15%. Low false positive rate means that developers can trust the system's suggestions more, thereby improving work efficiency. This is due to our incremental learning module, which can continuously learn from feedback to improve the accuracy of judgment.
[0145] 3. Refactoring suggestion adoption rate: Our system has achieved an adoption rate of 76%, much higher than the 45% of traditional tools. This shows that our suggestions are more practical and meet the actual needs of the project. This is because our system not only considers the code itself, but also considers the project context and runtime behavior, and can provide more accurate suggestions.
[0146] 4. Code quality improvement: In terms of reducing code complexity and improving maintainability, our system has achieved better results. The complexity has been reduced by 18%, and the maintainability has been improved by 22%, which will greatly reduce future maintenance costs.
[0147] 5. Performance impact: Interestingly, although our refactoring suggestions result in more code changes, performance improvement is more significant (5% vs. 1%). This shows that our system can identify some deep-seated problems that affect performance, which may be overlooked by traditional tools.
[0148] These results fully demonstrate the superiority of the system of the present invention. It not only can more accurately identify code problems, but also can provide more valuable refactoring suggestions. It is particularly worth noting that the high adoption rate and significant code quality improvement show that our system can adapt well to the actual development environment and bring substantial help to the development team.
[0149] Based on the test results, we believe that the current parameter settings have achieved good results and can be considered as the best embodiment. In particular, the choice of static analysis weight coefficient α as 0.6 well balances the advantages of static analysis and dynamic analysis. In future applications, this parameter can be adjusted according to the characteristics of specific projects, for example, for computationally intensive projects, the α value can be slightly reduced, while for projects with complex logic, the α value can be slightly increased.
[0150] Overall, these test results fully demonstrate the great potential of the system in improving code quality and reducing maintenance costs. It not only helps the development team to identify and solve code problems more quickly, but also continuously improves its analysis ability 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 application and is not intended to limit the present application. Any modification, equivalent replacement, improvement, etc. within the principles of the present application shall be included in the protection scope of the present application.
Claims
1. An incremental code smell detection and refactoring recommendation system, characterized in that, The system includes: The code extraction and static analysis module is used to extract code snippets to be analyzed from the target project and preprocess them to form files to be analyzed. The runtime information acquisition and dynamic analysis module is used to run the file to be analyzed in a sandbox environment, collect and summarize runtime information, and generate a dynamic analysis report. The learning module is used to receive static analysis results and the dynamic analysis report, comprehensively consider static and dynamic information, construct vector representations of potential features of the code, and update the incremental learning model. The reconstruction recommendation module is used to score each code snippet based on the code feature vector and the incremental learning model, and recommend corresponding reconstruction rules. The runtime information acquisition and dynamic analysis module includes: The sandbox environment unit is used to provide a virtual code execution environment; The code instrumentation unit uses JVMTI as the code instrumentation tool to dynamically acquire data and execute method calls during program execution. The information collection unit is used to collect runtime information, including class load time, number of method executions, exceptions thrown or raised, and number of object instantiations. An analysis report generation unit is used to associate the runtime information with the semantic information of the code to generate a dynamic analysis report; The learning module includes: A static feature extraction unit is used to extract static features from the file to be analyzed. A dynamic feature extraction unit is 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 latent features of the code. The fusion process adopts the following formula: , in, For the updated feature vector, For static feature vectors, For dynamic feature vectors, These are the static analysis weighting coefficients, with values ranging from [0, 1]. An incremental learning unit is used to update the incremental learning model based on the vector representation of the latent features of the code; The code extraction and static analysis module includes: The code extraction unit is used to extract code fragments to be analyzed from the target project. Syntax analysis units are used to retain the basic syntax units corresponding to the methods in the project; The preprocessing unit is used to preprocess the code fragments to generate a file to be analyzed; The preprocessing unit completes preprocessing by traversing the file, removing comments, and completing each function body with empty statements. 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 an abstract syntax tree and a token; The hidden layer consists of multiple layers of neurons, each layer having an activation function; The output layer outputs a vector representation of the latent features of the code; The forward propagation process of the neural network is represented as follows: , in, For the first The output of the layer, For activation function, For the first The weight matrix of the layer, For the first Layer bias vector, This is the output of the previous layer; The reconstruction recommendation module includes: A scoring unit is used to score each code segment based on the code feature vector; The rule matching unit is used to match and reconstruct rules in the rule database based on the scoring results; Recommendation generation unit, used to generate reconstructed recommendations based on matching results; The scoring unit uses the following formula for scoring: , Here, Score represents the rating of the code snippet. Let i be the weight of the i-th rating factor. Let V be the i-th scoring function, V be the code feature vector, and R be the set of reconstruction rules.
2. The incremental code odor detection and reconstruction recommendation method based on the system described in claim 1, employing the incremental code odor detection and reconstruction recommendation system described in claim 1, is characterized in that... The method includes the following steps: Extract code snippets to be analyzed from the target project and preprocess them to form the analysis file; Run the file to be analyzed in a sandbox environment, collect and summarize runtime information, and generate a dynamic analysis report. Based on the file to be analyzed and the dynamic analysis report, a vector representation of the code's latent features is constructed, and the incremental learning model is updated. Using the vector representation of the latent features of the code and the incremental learning model, each code snippet is scored, and corresponding refactoring rules are recommended.
3. The method according to claim 2, characterized in that, The steps for constructing vector representations of latent features of the code include: The input consists of a triplet, which is a vector representation of the code, the potential problems of the code, and the project progress of the code. Use the existing model to predict the target vector and generate the prediction results of the existing model; Predict the newly added target vector and generate the prediction results of the incremental model; Compare the actual results of the newly added target vector with the results 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: , in, Let ||a|| be the cosine similarity between two vectors, where ||a|| is the magnitude of vector a and ||b|| is the magnitude of vector b.
4. The method according to claim 3, characterized in that, When the cosine similarity is lower than a preset threshold, the gradient descent algorithm is used to optimize the model, with the optimization objective being: , , in, Let be the objective function. For the prediction vector, For the true vector, To determine the direction of updating the model parameters, These are the model parameters.
5. The method according to claim 4, characterized in that, The step of scoring each code snippet includes: Reconstruct rule records in the rule database using feature vectors, item type, and language type matching: Match ,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 , in, It is the score of the i-th evaluation indicator. It is the weight coefficient of the i-th evaluation index; The reconstruction rules are sorted according to the scores, and the rules with the highest scores are selected as the reconstruction recommendation results.
Citation Information
Patent Citations
Software system semi-automatic reconstruction method based on class-level network
CN114371871A
Evaluation method based on code syntax tree analysis
CN117331836A
Machine learning to facilitate incremental static program analysis
US20180060068A1