Code smell detection method and device
Through the LSTM-based code bad taste detection model, combined with text and structural characteristics, the problem of insufficient accuracy of code bad taste detection methods in the existing technology is solved, the accuracy and efficiency of detection are improved, and the life cycle of the software is extended.
Patent Information
- Application Number
- CN202110079933.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-01-21
- Publication Date
- 2024-12-17
- Estimated Expiration
- 2041-01-21
AI Technical Summary
The existing code bad taste detection methods rely on code measurement and heuristic rules. The accuracy and comprehensiveness are not ideal and are difficult to widely use in the industry.
The LSTM-based code bad taste detection model is used, combined with text features and structural features, and the detection is carried out through pre-trained models to improve the accuracy and comprehensiveness of the inspection.
Improve the accuracy of code bad taste detection, saves time, energy and cost of software maintenance and expansion, extends the software life cycle, and enhances code understanding by improving code logic.
Smart Images

Figure CN112764758B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software engineering, and in particular to a method and device for detecting code smell. Background Art
[0002] As the scale of software development in the Internet industry grows, the requirements for software quality and management are also getting higher and higher. Developers need to expand and maintain increasingly large and complex program frameworks, and spend more and more time on code structure sorting and improvement. Therefore, it is urgent to take some optimization measures to improve the reusability, understandability and maintainability of the code, and software refactoring came into being.
[0003] One of the key steps in software refactoring is to identify the code snippets that need to be refactored. In order to help developers determine where in the program they need to refactor, Fowler et al. proposed the concept of code smell, which refers to program codes that are introduced into the program due to design defects or bad coding habits and affect the software structure and quality. Code smell is a warning signal for potential problems in the code. Although not all bad smells indicate real problems, it is necessary to check most of them and make corresponding modifications. Code smells include data classes, feature attachments, long methods, etc.
[0004] Test code smell refers to bad programming practices in unit test code (for example, how test cases are organized, implemented, and how they interact with each other), which indicates potential design problems in the test source code. At present, researchers have proposed a series of automatic or semi-automatic methods to detect these code smells from the code. The current main code smell detection methods and tools, including JDeodorant, iPlasma, and PMD, mainly rely on code metrics and heuristic rules to detect various code smells. Since code metrics cannot fully reflect the quality of the code, simple heuristic rules are not enough to accurately describe the complex mapping relationship between code metrics and code smell inspection results; and the selection of code metrics is highly subjective, and the selected code metrics may not be the best choice. Therefore, the precision and recall of these detection methods are not ideal, and it is currently difficult to be widely used in the industry. Summary of the invention
[0005] In view of the problems in the prior art, the present invention provides a code smell detection method and device, an electronic device and a computer-readable storage medium, which can at least partially solve the problems in the prior art.
[0006] In order to achieve the above object, the present invention adopts the following technical solution:
[0007] In a first aspect, a method for detecting a bad code smell is provided, comprising:
[0008] Performing feature extraction on the acquired code class to be checked to obtain text features and structural features of the code class to be checked;
[0009] The text feature and the structural feature are used as prediction samples and input into a pre-trained code smell detection model, and the output of the code smell detection model is used as the code smell detection result of the code class to be detected.
[0010] Furthermore, the code smell detection model includes: a text input layer, a structure input layer, an LSTM layer, a first Dense fully connected layer, a Merge fusion layer, a second Dense fully connected layer and an output layer;
[0011] The input end of the text input layer is used to receive the text feature, and the output end is connected to the LSTM layer; the input end of the structure input layer is used to receive the structure feature, and the output end is connected to the first Dense fully connected layer; the output end of the LSTM layer and the output end of the first Dense fully connected layer are connected to the input end of the Merge fusion layer, the output end of the Merge fusion layer is connected to the input end of the second Dense fully connected layer, and the output end of the second Dense fully connected layer is connected to the output layer.
[0012] Furthermore, it also includes:
[0013] Build a code smell detection model;
[0014] Deconstruct the open source software code and automatically generate labels to obtain sample data;
[0015] Perform text feature extraction and structural feature extraction on each sample in the sample data to obtain a training sample set;
[0016] The training sample set is used to train the established code smell detection model.
[0017] Furthermore, it also includes:
[0018] Perform text feature extraction and structural feature extraction on the open source code smell dataset to obtain a test sample set;
[0019] The test sample set is used to test the code smell detection model.
[0020] Furthermore, the structural features include: inner circle complexity of WMC class, inner cohesion of TCC class, number of external class attributes accessed by FEW class, number of external attributes used by AFTD, value of local attributes used by LAA / value of all attributes used by upper method, number of public attributes of NOPA class, number of accessors of NOPA class, number of public members of NProtM class, and coverage of BOvR base class methods.
[0021] Furthermore, the code class to be checked includes multiple code lines, and the Skip-Gram model of Word2vector is used to extract the semantic feature vector of each code line, and the semantic feature vectors of each code line are combined to obtain the text feature.
[0022] In a second aspect, a code smell detection device is provided, comprising:
[0023] The text structure feature extraction module extracts features of the acquired code class to be checked to obtain text features and structure features of the code class to be checked;
[0024] The code smell detection module uses the text feature and the structural feature as prediction samples, inputs them into a pre-trained code smell detection model, and uses the output of the code smell detection model as the code smell detection result of the code class to be detected.
[0025] Furthermore, the code smell detection model includes: a text input layer, a structure input layer, an LSTM layer, a first Dense fully connected layer, a Merge fusion layer, a second Dense fully connected layer and an output layer;
[0026] The input end of the text input layer is used to receive the text feature, and the output end is connected to the LSTM layer; the input end of the structure input layer is used to receive the structure feature, and the output end is connected to the first Dense fully connected layer; the output end of the LSTM layer and the output end of the first Dense fully connected layer are connected to the input end of the Merge fusion layer, the output end of the Merge fusion layer is connected to the input end of the second Dense fully connected layer, and the output end of the second Dense fully connected layer is connected to the output layer.
[0027] According to a third aspect, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above-mentioned code smell detection method when executing the program.
[0028] In a fourth aspect, a computer-readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the above-mentioned code smell detection method are implemented.
[0029] The present invention provides a method and device for detecting bad code smells, which comprises: extracting features from the acquired code class to be detected to obtain text features and structural features of the code class to be detected; using the text features and the structural features as prediction samples, inputting the pre-trained code bad smell detection model, and using the output of the code bad smell detection model as the bad smell detection result of the code class to be detected. In addition to considering the structural features of the code, the semantic features of the code are further considered, which can improve the precision and recall of code bad smell detection, and can be used in the financial field or other fields.
[0030] In addition, the code smell detection model is based on LSTM, which uses the characteristics of deep learning in feature selection and establishing complex mapping relationships to extract text information. At the same time, it combines multiple structural measurement items to comprehensively judge whether the program to be detected contains code smell. It not only saves developers a lot of time and energy costs in software maintenance and expansion, and extends the life cycle of the software, but also can enhance code understanding by improving code logic, so as to better discover program defects.
[0031] Furthermore, the present invention reverse-reconstructs the open source software code and automatically generates labels to obtain sample data, thereby solving the problem that the training data set needs to be manually constructed, is time-consuming, is small in scale, and is difficult to use for deep learning.
[0032] In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the prior art descriptions. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work. In the drawings:
[0034] Figure 1 is a schematic diagram of the architecture between the server S1 and the client device B1 in an embodiment of the present invention;
[0035] Figure 2 Schematic diagram of the architecture between the server S1, the client device B1 and the database server S2 in an embodiment of the present invention;
[0036] Figure 3 This is a schematic diagram of the process of the code smell detection method in an embodiment of the present invention. Figure 1 ;
[0037] Figure 4The specific structure of the code smell detection model in the embodiment of the present invention is shown;
[0038] Figure 5 This is a schematic diagram of the process of the code smell detection method in an embodiment of the present invention. Figure 2 ;
[0039] Figure 6 An example of automatically constructing code smell samples in an embodiment of the present invention is shown;
[0040] Figure 7 Another example of automatically constructing code smell samples in an embodiment of the present invention is shown;
[0041] Figure 8 This is a schematic diagram of the process of the code smell detection method in an embodiment of the present invention. Figure 3 ;
[0042] Fig. 9 A code smell detection workflow in an embodiment of the present invention is shown;
[0043] Fig.10 is a structural block diagram of a code smell detection device in an embodiment of the present invention;
[0044] Fig.11 4 is a structural diagram of an electronic device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0045] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.
[0046] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0047] It should be noted that the terms "including" and "having" and any variations thereof in the specification and claims of the present application and the above-mentioned drawings are intended to cover non-exclusive inclusions. For example, a process, method, system, product or apparatus comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or apparatuses.
[0048] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present application can be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.
[0049] At present, researchers have proposed a series of automatic or semi-automatic methods to detect these code smells from the code. The current main code smell detection methods and tools, including JDeodorant, iPlasma and PMD, mainly rely on code metrics and heuristic rules to detect various code smells. Since code metrics cannot fully reflect the quality of the code, simple heuristic rules are not enough to accurately describe the complex mapping relationship between code metrics and code smell inspection results; and the selection of code metrics is highly subjective, and the selected code metrics may not be the best choice. Therefore, the precision and recall rates of these detection methods are not ideal, and it is currently difficult to be widely used in the industry.
[0050] The present invention provides a code smell detection method, which not only considers the structural characteristics of the code, but also further considers the semantic characteristics of the code, thereby improving the precision and recall of code smell detection.
[0051] In view of this, the present application provides a code smell detection device, which can be a server S1, see Figure 1 , the server S1 can be communicatively connected with at least one client device B1, the client device B1 can send the code class to be checked to the server S1, and the server S1 can receive the code class to be checked online. The server S1 can preprocess the acquired code class to be checked online or offline, extract features from the acquired code class to be checked to obtain text features and structural features of the code class to be checked; use the text features and the structural features as prediction samples, input into a pre-trained code bad smell detection model, and use the output of the code bad smell detection model as the bad smell detection result of the code class to be checked. Then, the server S1 can send the bad smell detection result of the code class to be checked to the client device B1 online. The client device B1 can receive the bad smell detection result of the code class to be checked online.
[0052] Also, see Figure 2The server S1 may also be connected to at least one database server S2 for communication, and the database server S2 is used to store the open source software code. The database server S2 sends the open source software code to the server S1 online, and the server S1 may receive the open source software code online, and then obtain the training sample set of the model according to the open source software code, and use the training sample set to perform model training on the model.
[0053] Based on the above content, the database server S2 can also be used to store an open source code smell dataset. The database server S2 sends the open source code smell dataset to the server S1 online, and the server S1 can receive the open source code smell dataset online, and then obtain a test sample based on at least the open source code smell dataset, and apply the test sample to perform model testing on the model, and use the output of the model as the test result, and then judge whether the current model meets the preset requirements based on the test result and the known evaluation results of the test sample. If so, the current model is used as the target model for code smell detection; if the current model does not meet the preset requirements, the current model is optimized and / or the updated training sample set is applied to retrain the model.
[0054] Based on the above content, the client device B1 may have a display interface, so that the user can view the smell detection result of the code class to be detected sent by the server S1 according to the interface.
[0055] It is understandable that the client device B1 may include a smart phone, a tablet electronic device, a network set-top box, a portable computer, a desktop computer, a personal digital assistant (PDA), a vehicle-mounted device, a smart wearable device, etc. Among them, the smart wearable device may include smart glasses, a smart watch, a smart bracelet, etc.
[0056] In practical applications, the part of performing the bad smell detection of the code class to be detected can be executed on the server S1 side as described above, that is, Figure 1 The architecture shown in the figure can also be that all operations are completed in the client device B1, and the client device B1 can directly communicate with the database server S2. The specific selection can be made according to the processing capability of the client device B1 and the restrictions of the user's usage scenario. This application does not limit this. If all operations are completed in the client device B1, the client device B1 can also include a processor for performing specific processing of the bad smell detection of the code class to be detected.
[0057] The server and the client device may communicate using any suitable network protocol, including network protocols that have not yet been developed on the date of filing this application. The network protocol may include, for example, TCP / IP protocol, UDP / IP protocol, HTTP protocol, HTTPS protocol, etc. Of course, the network protocol may also include, for example, RPC protocol (Remote Procedure Call Protocol) and REST protocol (Representational State Transfer) used on top of the above protocols.
[0058] Figure 3 This is a schematic diagram of the process of the code smell detection method in an embodiment of the present invention. Figure 1 ;like Figure 3 As shown, the code smell detection method may include the following contents:
[0059] Step S100: extracting features of the acquired code class to be checked to obtain text features and structural features of the code class to be checked;
[0060] The applicant has proved through a large number of research results that mining the text features of the code class to be checked is beneficial to improving the accuracy of code smell detection. Therefore, the embodiment of the present invention uses text features as one of the two parts of the input of the neural network classifier, and can represent the semantic information of the code class to be checked through word vectors.
[0061] In addition, based on the semantic information, multiple structural metrics such as distance, coupling, cyclomatic complexity, etc. can be combined to comprehensively judge whether the program to be tested contains code smell.
[0062] Step S200: taking the text features and the structural features as prediction samples, inputting them into a pre-trained code smell detection model, and taking the output of the code smell detection model as the code smell detection result of the code class to be detected.
[0063] Among them, the code smell detection model is a deep learning neural network classifier, which has two inputs, namely text features and structural features, and has two output results, one is to represent the existence of code smell, and the other is to represent the absence of code smell. For example, 0 can be used to indicate the absence of code smell and 1 can be used to indicate the presence of code smell.
[0064] It is worth noting that the embodiment of the present invention uses the characteristics of deep learning in feature selection and establishing complex mapping relationships to extract text information, and combines multiple metrics such as distance, coupling, and cyclomatic complexity to comprehensively judge whether the program to be tested contains code smell. This method not only saves developers a lot of time and energy costs in software maintenance and expansion, and extends the life cycle of the software, but also can enhance code understanding by improving code logic, so as to better discover program defects.
[0065] In an optional embodiment, nine related measurement items may be used as code structure information and class member identifiers may be used as code text information as inputs of the model, as shown in the following formula:
[0066] input=<identifies,metrics>
[0067] identifies={name(m1),name(m2),...,name(m n )}
[0068] metrics={metric1,metric2,...,metric9}
[0069] First, it is necessary to extract some indicators that can represent the code characteristics from the source code class, and obtain a series of metrics after calculation and analysis. For the target bad smell, based on existing research, this paper adopts 9 related metric items as code structure information, which are as follows.
[0070] 1) WMC-type inner circle complexity;
[0071] 2) cohesion within the TCC class;
[0072] 3) FEW is the number of external class attributes accessed by this class;
[0073] 4) The number of external attributes used by the AFTD method;
[0074] 5) The LAA method uses the value of local attributes / the above method uses the values of all attributes;
[0075] 6) The number of common attributes of NOPA class;
[0076] 7) The number of NOPA class accessors;
[0077] 8) The number of public members of the NProtM class;
[0078] 9) BOvR base class method coverage, that is, the number of inherited methods overridden by the base class / the total number of inherited members.
[0079] Secondly, for text features, the Skip-Gram model of the famous word vectorization tool Word2vector can be used. It is a two-layer neural network model that learns semantic knowledge from a large amount of text corpus in an unsupervised manner. The input is the text corpus, and the output is the feature vector of the words in the corpus. It uses the learning text to represent the semantic information of the word in the form of word vectors, maps the words in the identifier to the high-dimensional vector space, and uses an embedding space to make semantically similar words close in the space. The word itself is used as input, and the word in the context of a word is used as output. Finally, the text features of the test data are converted into the form of word vectors that can be recognized by the neural network.
[0080] Specifically, the code class to be checked includes multiple code lines, and the Skip-Gram model of Word2vector is used to extract the semantic feature vector of each code line, and the semantic feature vectors of each code line are combined to obtain the text feature.
[0081] In an alternative embodiment, see Figure 4 The code smell detection model includes: a text input layer (i.e., an input layer for receiving identifiers), a structure input layer (i.e., an input layer for receiving code metrics), an LSTM layer, a first Dense fully connected layer, a Merge fusion layer, a second Dense fully connected layer, and an output layer;
[0082] The input end of the text input layer is used to receive the text features, and the output end is connected to the LSTM layer; the input end of the structure input layer is used to receive the structure features, and the output end is connected to the first Dense fully connected layer; the output end of the LSTM layer and the output end of the first Dense fully connected layer are connected to the input end of the Merge fusion layer, the output end of the Merge fusion layer is connected to the input end of the second Dense fully connected layer, and the output end of the second Dense fully connected layer is connected to the output layer.
[0083] Among them, the input text features are composed of code text information. After the code class to be detected is preprocessed, the text information is converted into numerical information, and the Masking data shielding layer included in the input LSTM layer is passed through the form of word vector. The LSTM layer is a LSTM long short-term memory network. The activation function of the LSTM layer is a sigmoid function, the output dimension is 2, and the weight of the layer is initialized uniformly. As a variant of the RNN recurrent neural network, the long short-term memory network makes up for the defect of long-term memory failure of the recurrent neural network by avoiding the gradient disappearance problem, and thus has been fully applied in the field of natural language processing. The present invention uses the advantage of the long short-term memory network that is good at extracting key semantic features from long sequences to process the input multiple identifier word vectors to help the classifier analyze the semantic cohesion of the detected class.
[0084] The structural characteristics of the input reflect the structural characteristics of the detected class. By inputting the 9 metrics extracted from the class into the Dense fully connected layer, the classifier can iteratively train the input under supervision and gradually adjust the parameter combination that best matches the training set label. The activation function of the fully connected layer is the tanh function, the output dimension is 12, and the weight of this layer is initialized with uniform distribution. After that, the output of the first Dense fully connected layer and the LSTM layer will be merged in the form of vector concatenation through the Merge fusion layer, and then mapped to the final Sigmoid output layer by a fully connected layer. The activation function of the fully connected layer here is the tanh function, the output dimension is 4, and the weight is initialized to an all-zero matrix; the activation function of the output layer is the sigmoid function, and the output dimension is 1. The final model loss function selected is the binary_crossentropy function, the optimizer is the adam adaptive method, the number of iterations is 10, and the batch size is 5.
[0085] By adopting the above-mentioned code smell detection model, based on LSTM, the text information is extracted by taking advantage of the characteristics of deep learning in feature selection and establishing complex mapping relationships. At the same time, multiple structural measurement items are combined to comprehensively judge whether the program to be detected contains code smell. This not only saves developers a lot of time and energy costs in software maintenance and expansion, and extends the life cycle of the software, but also enhances code understanding by improving code logic, so as to better discover program defects.
[0086] In an alternative embodiment, see Figure 5 , the code smell detection method may further include:
[0087] Step S300: Establish a code smell detection model;
[0088] Step S400: reverse-reconstructing the open source software code and automatically generating labels to obtain sample data;
[0089] Neural networks usually contain a large number of parameters, and a large amount of training data is needed to tune the parameters. To apply neural networks to code smell detection, the problem of constructing massive training data must first be solved. If the code smell detection data set is manually constructed, it is time-consuming and small in scale, making it difficult to take advantage of deep learning. To this end, the present invention proposes a training sample automatic generation technology based on anti-reconstruction, which can automatically generate a large number of labeled positive and negative training samples, laying a foundation for studying code smell detection and reconstruction methods based on neural networks.
[0090] This invention selects the five most common and most influential typical bad smells as the research objects, which are: Refused Bequest is an incorrect use of inheritance structure, Feature Envy refers to the situation where data and its operation behaviors are not encapsulated together, Data Class refers to the data container class that needs to be frequently operated by other classes, God Class violates the single responsibility principle, and Unnecessary Coupling refers to the introduction of unnecessary coupling relationships in the file. First, download several software systems with high popularity rankings from open source websites such as Github as open source software codes. Then, use the API method provided by the Eclipse JDT plug-in to reverse-reconstruct the open source software code and automatically generate labels to obtain sample data. The following two examples are used to illustrate the reverse reconstruction.
[0091] Figure 6 The sample JaxenXPathRuleQuery class in is a Jaxen-based XPathRule query class. The RuleContext class is a class that provides access to the Rule processing status. The evluate() method is used to obtain a list of XPaths, which implements some functions of XPathRelu queries. It only uses getLanguageVersion() in RuleContext to obtain one of the variables, which has nothing to do with the functions to be performed by the RuleContext class. Therefore, conceptually, it should be placed in the JaxenXPathRuleQuery class. From the dependency point of view, the method evaluate() is moved from the JaxenXPathRuleQuery class to the RuleContext class. Compared with the RuleContext class, evluate() is obviously more closely related to the JaxenXPathRuleQuery class. After this move, feature dependency bad smells can be automatically constructed.
[0092] Figure 7As shown in the example, we can clearly see that createRule is more closely related to the RuleSetReferenceId class in the structure. However, we can see that its source class is the RuleSetFactory class, which means that a series of operations on RuleSet should be placed in this class. RuleSetReferenceId is mainly a reference operation on RuleSet, and the createRule method is mainly used to create a new rule. Therefore, we can see that it is more appropriate to put the createRule class in the original position from a semantic point of view.
[0093] Through the API method of Eclipse JDT, the source data set is subjected to five target types of bad smell anti-reconstruction operations to automatically generate a training set, in which positive samples account for 50% and negative samples account for 50%.
[0094] Step S500: extracting text features and structural features from each sample in the sample data to obtain a training sample set;
[0095] The text feature extraction and structural feature extraction processes are as described above and will not be described again here.
[0096] Step S600: using the training sample set to train the established code smell detection model.
[0097] Specifically, the text features and structural features corresponding to the sample are input into the model, the output results of the model are compared with the labels corresponding to the samples, and the model parameters are adjusted according to the comparison results to achieve model training.
[0098] In an alternative embodiment, see Figure 8 , the code smell detection method may further include:
[0099] Step S700: extracting text features and structural features from the open source code smell dataset to obtain a test sample set;
[0100] Step S800: applying the test sample set to test the code smell detection model.
[0101] By using the open source code smell dataset as a test set and inputting it into the trained neural network classifier, the classifier automatically performs binary classification on all samples to review the precision and recall of the model.
[0102] If the precision and recall of the model meet the requirements, the model training is considered complete. If not, the model needs to be adjusted or the training set needs to be reconstructed for retraining.
[0103] It is worth mentioning that the present invention adopts a large-scale open source dataset of code smell based on manual verification proposed by Palomba et al. as experimental verification data. This dataset contains the detection status of 13 types of code smell in 395 historical versions of 30 software projects.
[0104] By using the existing code smell dataset, we obtain the program blocks of the open source projects that contain any of the five target code smells of the present invention in the project source code, and use them as positive samples in the test data. The remaining classes in the project can be marked as negative samples in the test set, thus generating a test sample set for the existing tools and the proposed method. Such a test sample set is accurate, reliable, and has a real source, which can more appropriately demonstrate the reliability of the two methods in detecting code smells in actual application scenarios.
[0105] All test items under each program block are input into the trained binary neural network classifier. If all test items are predicted as 0, then there is no code smell in the given program block; if one or more test items are predicted as 1, then there is code smell in the program block. JDeodorant is selected as the object of the code smell detection comparison test. After the experiment is completed, the precision and recall of this model and JDeodorant for detecting five code smells are calculated respectively. The calculation formula is as follows:
[0106]
[0107]
[0108] In summary, see Fig. 9 The code smell detection technology provided by the present invention includes a model training process and a detection process using a trained neural network classification model. It combines LSTM neural network, text and structural feature extraction, and sample anti-reconstruction technologies to improve the accuracy of code smell detection technology, which not only saves developers a lot of time and energy costs for software maintenance and expansion, and prolongs the life cycle of software, but also can enhance code understanding by improving code logic, so as to better discover program defects. In addition, the anti-reconstruction-based code smell positive and negative sample automatic generation technology is adopted, based on a large number of open source projects on the Internet, and a sufficient sample set with labels for code smell is obtained by this method, which lays a foundation for studying the neural network-based code smell detection and reconstruction method.
[0109] Based on the same inventive concept, the embodiments of the present application also provide a code smell detection device, which can be used to implement the method described in the above embodiments, as described in the following embodiments. Since the principle of solving the problem by the code smell detection device is similar to the above method, the implementation of the code smell detection device can refer to the implementation of the above method, and the repeated parts will not be repeated. As used below, the term "unit" or "module" can be a combination of software and / or hardware that implements predetermined functions. Although the device described in the following embodiments is preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.
[0110] Fig.10 is a structural block diagram of a code smell detection device in an embodiment of the present invention. Fig.10 As shown, the code smell detection device specifically includes: a text structure feature extraction module 10 and a code smell detection module 20.
[0111] The text structure feature extraction module 10 performs feature extraction on the acquired code class to be checked to obtain text features and structure features of the code class to be checked;
[0112] The code smell detection module 20 uses the text features and the structural features as prediction samples, inputs them into a pre-trained code smell detection model, and uses the output of the code smell detection model as the code smell detection result of the code class to be detected.
[0113] Among them, the characteristics of deep learning in feature selection and establishing complex mapping relationships are used to extract text information, and multiple metrics such as distance, coupling, and cyclomatic complexity are combined to comprehensively judge whether the program to be tested contains code smell. This method not only saves developers a lot of time and energy costs in software maintenance and expansion, and extends the life cycle of the software, but also enhances code understanding by improving code logic, so as to better discover program defects.
[0114] In an optional embodiment, the code smell detection model includes: a text input layer, a structure input layer, an LSTM layer, a first Dense fully connected layer, a Merge fusion layer, a second Dense fully connected layer and an output layer;
[0115] The input end of the text input layer is used to receive the text features, and the output end is connected to the LSTM layer; the input end of the structure input layer is used to receive the structure features, and the output end is connected to the first Dense fully connected layer; the output end of the LSTM layer and the output end of the first Dense fully connected layer are connected to the input end of the Merge fusion layer, the output end of the Merge fusion layer is connected to the input end of the second Dense fully connected layer, and the output end of the second Dense fully connected layer is connected to the output layer.
[0116] The devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is an electronic device, and the electronic device may be, for example, a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
[0117] In a typical example, the electronic device specifically includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, the steps of the code smell detection method described above are implemented.
[0118] Reference below Fig.11 , which shows a structural schematic diagram of an electronic device 600 suitable for implementing an embodiment of the present application.
[0119] like Fig.11 As shown, electronic device 600 includes a central processing unit (CPU) 601, which can perform various appropriate operations and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage portion 608 into a random access memory (RAM) 603. In RAM 603, various programs and data required for the operation of system 600 are also stored. CPU 601, ROM 602, and RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to bus 604.
[0120] The following components are connected to the I / O interface 605: an input section 606 including a keyboard, a mouse, etc.; an output section 607 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 608 including a hard disk, etc.; and a communication section 609 including a network interface card such as a LAN card, a modem, etc. The communication section 609 performs communication processing via a network such as the Internet. A drive 610 is also connected to the I / O interface 605 as needed. A removable medium 611, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 610 as needed, so that a computer program read therefrom is installed as needed as the storage section 608.
[0121] In particular, according to an embodiment of the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present invention includes a computer-readable storage medium having a computer program stored thereon, which implements the steps of the above-mentioned code smell detection method when executed by a processor.
[0122] In such an embodiment, the computer program may be downloaded and installed from a network via the communication section 609 , and / or installed from the removable medium 611 .
[0123] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.
[0124] For the convenience of description, the above device is described in various units according to their functions. Of course, when implementing the present application, the functions of each unit can be implemented in the same or multiple software and / or hardware.
[0125] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0126] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0127] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0128] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0129] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.
[0130] The present application may be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present application may also be practiced in distributed computing environments where tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules may be located in local and remote computer storage media, including storage devices.
[0131] 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 system 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.
[0132] The above is only an embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.
Claims
1. A code smell detection method, It is characterized in that include: Build a code smell detection model; Perform anti-reconstruction operations on target code smells on the open source software code dataset to automatically generate a training set, where the target code smells include: incorrect use of inheritance structures, data and operations on them are not encapsulated together, data container classes that need to be frequently operated by other classes, violations of the single responsibility principle, and unnecessary coupling relationships introduced in the file; Perform text feature extraction and structural feature extraction on each sample in the sample data to obtain a training sample set; Using the training sample set to train the established code smell detection model; The text feature extraction and structural feature extraction of the open source code smell dataset are performed to obtain a test sample set, wherein, using the existing code smell dataset, the program blocks of the open source project containing any of the target code smells are obtained in the project source code, and these are used as positive samples in the test sample set, and the remaining other classes can be marked as negative samples in the test sample set, thereby generating a test sample set; Applying the test sample set to test the code smell detection model; Performing feature extraction on the acquired code class to be checked to obtain text features and structural features of the code class to be checked; The text features and the structural features are used as prediction samples and input into a pre-trained code smell detection model, and the output of the code smell detection model is used as the smell detection result of the code class to be detected; the code smell detection model includes: a text input layer, a structural input layer, an LSTM layer, a first Dense fully connected layer, a Merge fusion layer, a second Dense fully connected layer and an output layer; the input end of the text input layer is used to receive the text features, and the output end is connected to the LSTM layer; the input end of the structural input layer is used to receive the structural features, and the output end is connected to the first Dense fully connected layer; the output end of the LSTM layer and the output end of the first Dense fully connected layer are connected to the input end of the Merge fusion layer, the output end of the Merge fusion layer is connected to the input end of the second Dense fully connected layer, and the output end of the second Dense fully connected layer is connected to the output layer.
2. The code smell detection method according to claim 1, It is characterized in that The structural features include: the inner circle complexity of the WMC class, the cohesion of the TCC class, the number of external class attributes accessed by the FEW class, the number of external attributes used by the AFTD, the value of local attributes used by the LAA / the value of all attributes used by the above method, the number of public attributes of the NOPA class, the number of accessors of the NOPA class, the number of public members of the NProtM class, and the coverage of the BOvR base class methods.
3. The code smell detection method according to claim 1, It is characterized in that The code class to be checked includes multiple code lines. The Skip-Gram model of Word2vector is used to extract the semantic feature vector of each code line, and the semantic feature vectors of each code line are combined to obtain the text feature.
4. A code smell detection device, It is characterized in that include: A text structure feature extraction module is used to extract features of the acquired code class to be checked to obtain text features and structure features of the code class to be checked; A code smell detection module, which uses the text features and the structural features as prediction samples, inputs them into a pre-trained code smell detection model, and uses the output of the code smell detection model as a smell detection result of the code class to be detected; The code smell detection model includes: a text input layer, a structure input layer, an LSTM layer, a first Dense fully connected layer, a Merge fusion layer, a second Dense fully connected layer and an output layer; the input end of the text input layer is used to receive the text features, and the output end is connected to the LSTM layer; the input end of the structure input layer is used to receive the structure features, and the output end is connected to the first Dense fully connected layer; the output end of the LSTM layer and the output end of the first Dense fully connected layer are connected to the input end of the Merge fusion layer, the output end of the Merge fusion layer is connected to the input end of the second Dense fully connected layer, and the output end of the second Dense fully connected layer is connected to the output layer; The code smell detection device is used to: Build a code smell detection model; Perform anti-reconstruction operations on target code smells on the open source software code dataset to automatically generate a training set, where the target code smells include: incorrect use of inheritance structures, data and operations on them are not encapsulated together, data container classes that need to be frequently operated by other classes, violations of the single responsibility principle, and unnecessary coupling relationships introduced in the file; Perform text feature extraction and structural feature extraction on each sample in the sample data to obtain a training sample set; Using the training sample set to train the established code smell detection model; The text feature extraction and structural feature extraction of the open source code smell dataset are performed to obtain a test sample set, wherein, using the existing code smell dataset, the program blocks of the open source project containing any of the target code smells are obtained in the project source code, and these are used as positive samples in the test sample set, and the remaining other classes can be marked as negative samples in the test sample set, thereby generating a test sample set; The code smell detection model is tested using the test sample set.
5. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, It is characterized in that When the processor executes the program, the steps of the code smell detection method according to any one of claims 1 to 3 are implemented.
6. A computer-readable storage medium having a computer program stored thereon, It is characterized in that When the computer program is executed by a processor, the steps of the code smell detection method according to any one of claims 1 to 3 are implemented.
