A code bad smell detection method, device, equipment and storage medium

By acquiring the associated software metrics and impact factor set of the software project and dynamically selecting detection parameters, the problem of poor accuracy and complex process in code smell detection in the existing technology is solved, thereby improving the accuracy and ease of detection.

CN116594882BActive Publication Date: 2026-05-22SHENZHEN UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
SHENZHEN UNIV
Filing Date
2023-05-15
Publication Date
2026-05-22

Smart Images

  • Figure CN116594882B_ABST
    Figure CN116594882B_ABST
Patent Text Reader

Abstract

The application is suitable for the technical field of computers and provides a code bad smell detection method, device, equipment and storage medium, which comprises the following steps: acquiring a current bad smell detection type of a software project to be detected; acquiring associated software metrics of the current bad smell detection type, calculating the value of each associated software metric in each code entity in the software project to be detected and the average value of each associated software metric in all code entities, acquiring pre-detection rules of the current bad smell detection type, pre-detecting all code entities in the software project to be detected according to the pre-detection rules, obtaining a bad smell suspect entity set of the current bad smell detection type, and finally detecting each bad smell suspect entity according to the influence factor set of the current bad smell detection type, the value and average value of the associated software metrics, obtaining a bad smell entity set with the current bad smell detection type, and thus completing the detection of all bad smell detection types.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of computer technology, and in particular relates to a method, apparatus, device and storage medium for detecting code smells. Background Technology

[0002] Code smells are low-quality code snippets that violate software development principles and require refactoring; they are also known as code anti-patterns or technical debt. Detecting code smells helps identify software refactoring goals. Software refactoring improves software structure, readability, scalability, and reusability without changing functionality or external visibility. Software refactoring techniques are crucial for improving software quality, and code smell detection directly identifies the locations within the program that require refactoring.

[0003] However, current code smell detection methods still suffer from the following problems: 1) Existing code smell detection methods based on metrics and heuristic rules rely on manually setting detection rules and thresholds, leading to significant subjective influence on the detection results. Furthermore, these methods cannot dynamically detect code smells based on the differences between software projects. 2) Current machine learning-based code smell detection methods rely on training models with large amounts of labeled data, but such datasets are currently lacking in the code smell detection field. Moreover, machine learning-based methods fail to achieve the expected results in cross-project code smell detection. 3) Existing detection methods and tools often involve numerous detection steps, multiple data format conversions, complex processes, and are difficult to use. Machine learning-based methods require converting source code into features that the model can recognize, resulting in poor usability. Plugin tools require importing the software project into the platform before using the tool for code smell detection. However, in practice, many software projects fail to complete code smell detection due to version incompatibility. Summary of the Invention

[0004] The purpose of this invention is to provide a method, apparatus, device, and storage medium for detecting code smells, aiming to solve the problem that the accuracy of code smell detection is poor because existing technologies cannot provide an effective method for detecting code smells.

[0005] In a first aspect, the present invention provides a method for detecting code smells, the method comprising the following steps:

[0006] Step a: Receive the input software item to be tested and obtain the current bad smell detection type of the software item to be tested;

[0007] Step b: Obtain the associated software metrics of the current bad smell detection type, and calculate the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities;

[0008] Step c: Based on the current bad smell detection type and the average value, obtain the pre-detection rule for the current bad smell detection type, and perform pre-detection on all code entities in the software project to be detected according to the pre-detection rule to obtain the bad smell suspect entity set for the current bad smell detection type;

[0009] Step d: Based on the set of influencing factors for the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, perform a final inspection on each bad smell suspect entity in the bad smell suspect entity set to obtain and output the bad smell entity set with the current bad smell detection type.

[0010] Step e: When all bad smell detection types of the software project to be tested have not been detected, obtain the next bad smell detection type of the software project to be tested, set the next bad smell detection type as the current bad smell detection type, and jump to step b to detect the next bad smell detection type until all bad smell detection types of the software project to be tested are detected.

[0011] In some embodiments, the step of obtaining the current bad smell detection type of the software project to be tested includes:

[0012] Obtain the symbiotic relationships between a preset number of bad taste types in the bad taste instance dataset, and quantify the symbiotic relationships between bad tastes into influencing factors;

[0013] Obtain all pairs of bad taste types whose influence factors are greater than a preset factor threshold, determine the bad taste types corresponding to all pairs of bad taste types as bad taste detection types, and determine the detection priority of all bad taste detection types according to the number of elements in the influence factor set of each bad taste detection type.

[0014] Based on the detection priority of all the aforementioned bad smell detection types, the current bad smell detection type of the software project to be tested is obtained.

[0015] In some embodiments, the quantification formula for converting the synergistic relationship between the unpleasant odors into influencing factors is as follows: Where i≠j, f CSi,j |CS represents the influence factor of bad taste type i on bad taste type j. i |Indicates the number of times the type of bad taste i appears,|CS i ∧CS j| indicates the number of times that bad taste type i and bad taste type j co-occur.

[0016] In some embodiments, the step of performing a final inspection on each suspected bad smell entity in the suspected bad smell entity set based on the set of influencing factors of the current bad smell detection type, the value and average value of the associated software metric, includes:

[0017] Based on the set of influencing factors and detection priority of the current bad smell detection type, calculate the initial probability value of the currently detected bad smell suspect entity;

[0018] Based on the values ​​and average values ​​of the associated software metrics, calculate the metric deviation values ​​of all associated software metrics in the currently detected bad-taste suspect entity;

[0019] Based on the initial probability value and the metric deviation value, the final probability value of the currently detected bad smell suspect entity having the current bad smell detection type is obtained;

[0020] When the final probability value is greater than the preset category threshold, it is determined that the currently detected bad smell suspect entity has the current bad smell detection category; otherwise, it does not have the current bad smell detection category.

[0021] In some embodiments, the step of calculating the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities includes:

[0022] Traverse all source code files in the software project to be tested, parse and convert the class layer code entities and method layer code entities in the source code files into class layer abstract syntax trees and method layer abstract syntax trees, and extract the node structure information and semantic information of the class layer abstract syntax trees and method layer abstract syntax trees;

[0023] Based on the node structure information and semantic information, calculate the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities.

[0024] In some embodiments, the class layer code entities and method layer code entities in the source code file are parsed and transformed into class layer abstract syntax trees and method layer abstract syntax trees by an AST parser.

[0025] In some embodiments, the step of obtaining a pre-detection rule for the current bad odor detection type based on the current bad odor detection type and the average value includes:

[0026] Obtain the initial pre-detection rule for the current bad odor detection type, and instantiate the initial pre-detection rule using the average value to obtain the pre-detection rule for the current bad odor detection type.

[0027] Secondly, the present invention provides a code odor detection device, the device comprising:

[0028] The detection type acquisition unit is used to receive the input software item to be detected and acquire the current bad smell detection type of the software item to be detected.

[0029] The value calculation unit is used to obtain the associated software metrics of the current bad smell detection type, and calculate the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities;

[0030] The code pre-inspection unit is used to obtain the pre-inspection rules for the current bad smell detection type based on the current bad smell detection type and the average value, and to perform pre-inspection on all code entities in the software project to be inspected according to the pre-inspection rules to obtain the set of suspected bad smell entities for the current bad smell detection type.

[0031] The code final inspection unit is used to perform a final inspection on each suspected bad smell entity in the suspected bad smell entity set based on the set of influence factors for the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, to obtain and output a set of bad smell entities with the current bad smell detection type; and

[0032] The detection setting unit is used to obtain the next bad smell detection type of the software project to be tested when all bad smell detection types of the software project to be tested have not been detected, set the next bad smell detection type as the current bad smell detection type and trigger the value calculation unit to detect the next bad smell detection type until all bad smell detection types of the software project to be tested are detected.

[0033] Thirdly, the present invention also provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the method described above.

[0034] Fourthly, the present invention also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps of the method described above.

[0035] This invention, upon receiving an input software project to be tested, obtains the current "bad smell" detection category of the software project, acquires the associated software metrics for the current "bad smell" detection category, calculates the values ​​and average values ​​of the associated software metrics in each code entity of the software project to be tested, and then obtains the pre-detection rules for the current "bad smell" detection category. Based on the pre-detection rules, a pre-detection is performed on all code entities in the software project to be tested, resulting in a set of suspected "bad smell" entities for the current "bad smell" detection category. Finally, based on the set of influence factors for the current "bad smell" detection category, the values ​​and average values ​​of the associated software metrics, and the final detection is performed on each suspected "bad smell" entity in the suspected "bad smell" entity set, resulting in a set of "bad smell" entities with the current "bad smell" detection category. This allows for differentiated and dynamic selection of "bad smell" detection parameters based on the software project to be tested, thereby improving the detection accuracy of the software project. Attached Figure Description

[0036] Figure 1 This is a flowchart illustrating the code odor detection method provided in Embodiment 1 of the present invention;

[0037] Figure 2 This is a flowchart illustrating the code smell detection method provided in Embodiment 2 of the present invention;

[0038] Figure 3 This is a schematic diagram of the code odor detection device provided in Embodiment 3 of the present invention;

[0039] Figure 4 This is a schematic diagram of the structure of the computer device provided in Embodiment 4 of the present invention. Detailed Implementation

[0040] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention.

[0041] It should be understood that, when used in this specification and the appended claims, the term "comprising" indicates the presence of the described feature, integral, step, operation, element, and / or component, but does not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components, and / or collections thereof. Furthermore, the terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to limit the invention. As used in this specification and the appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms unless the context clearly indicates otherwise. The terms "first," "second," and similar words do not indicate any order, quantity, or importance, but are merely used to distinguish different components. Words such as "connected" or "linked" are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. "Above," "below," "left," "right," etc., are used only to indicate relative positional relationships, which may change accordingly when the absolute position of the described object changes.

[0042] To keep the following description of the embodiments of the present invention clear and concise, detailed descriptions of some known functions and known components are omitted in this specification.

[0043] The specific implementation of the present invention will be described in detail below with reference to specific embodiments:

[0044] Example 1:

[0045] Figure 1 The implementation flow of the code smell detection method provided in Embodiment 1 of the present invention is shown. For ease of explanation, only the parts related to the embodiments of the present invention are shown, and are described in detail below:

[0046] In step S101, the input software item to be detected is received, and the current bad smell detection type of the software item to be detected is obtained.

[0047] This invention is applicable to computer devices, such as personal computers and servers, for detecting code smells in software source code. Here, the software for smell detection is represented or described as a software project to be detected. Specifically, the software project to be detected can be a compressed package, which may include source code files and configuration files for software operation. When performing smell detection on the software project to be detected, different types of smells need to be detected in all source code files within the project. Each detected smell can be called a smell detection category. For example, these smells may include large classes, long methods, message chains, feature envy, spaghetti code, etc., and the current smell detection category is the smell currently being detected in the software project.

[0048] In step S102, the associated software metrics of the current bad smell detection type are obtained, and the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities are calculated.

[0049] In this embodiment of the invention, each source code file in the software project to be detected may include multiple code entities, such as class-level code entities and method-level code entities. When detecting code smells in the software source code, the detection is achieved by identifying the types of code smells possessed by the code entities. Different combinations of software metrics can determine and detect different types of code smells within a code entity. These software metrics can be referred to as associated software metrics of code smells. After obtaining the current type of code smell detected in the software project to be detected, the associated software metrics of the current type of code smell are obtained. The value of the associated software metric in each code entity of the software project to be detected is calculated. Then, based on the value of the associated software metric in each code entity of the software project to be detected, the average value of the associated software metric in all code entities of the software project to be detected is calculated for subsequent code smell detection.

[0050] In step S103, based on the current bad smell detection type and average value, the pre-detection rule for the current bad smell detection type is obtained. Based on the pre-detection rule, all code entities in the software project to be detected are pre-detected to obtain the set of suspected bad smell entities for the current bad smell detection type.

[0051] In this embodiment of the invention, the pre-detection rule can perform pre-detection on all code entities in the software project to be detected, obtaining a set of suspected bad smell entities of the current bad smell detection type, that is, all code entities in the software project to be detected that may have the current bad smell detection type. The pre-detection rule is set based on the value of each associated software metric in each code entity and the average value of each associated software metric in all code entities. It can be preset, or extracted from an expert knowledge base or through a neural network. As an example, the pre-detection rule can be a logical operation of the judgment rules for multiple different software metric values.

[0052] In step S104, based on the set of influencing factors for the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, a final inspection is performed on each bad smell suspect entity in the bad smell suspect entity set to obtain and output the bad smell entity set with the current bad smell detection type.

[0053] In this embodiment of the invention, when a bad smell i appears in a code entity, another bad smell j also appears simultaneously in the same code entity, indicating a symbiotic relationship between the bad smells. In this case, bad smell i can be considered to influence bad smell j, and the influence factor represents the degree of influence of one bad smell on another. This embodiment of the invention quantifies the symbiotic relationship between bad smells into influence factors. Then, based on the influence factor set of the current bad smell detection type, the values ​​and average values ​​of associated software metrics, a final inspection is performed on each suspected bad smell entity in the suspected bad smell entity set, resulting in a bad smell entity set with the current bad smell detection type, thereby ultimately achieving the detection of the current bad smell detection type in the software project to be detected.

[0054] In step S105, it is determined whether all types of bad smells in the software project to be tested have been detected.

[0055] In this embodiment of the invention, it is determined whether all types of off-odor detection for the software project to be tested have been completed. When all types of off-odor detection for the software project to be tested have been completed, it indicates that all off-odor detection for the software project to be tested has been completed, and step S106 is executed to end the testing of the software project to be tested. When all types of off-odor detection for the software project to be tested have not been completed, step S107 is executed to continue testing the remaining types of off-odor detection for the software project to be tested.

[0056] In step S106, the detection of the software project to be tested is terminated.

[0057] In step S107, the next bad smell detection type of the software project to be tested is obtained, and the next bad smell detection type is set as the current bad smell detection type.

[0058] In this embodiment of the invention, when all bad smell detection types of the software project to be tested have not been detected, the next bad smell detection type of the software project to be tested is obtained, the next bad smell detection type is set as the current bad smell detection type, and the process jumps to step S102 to continue to detect the next bad smell detection type of the software project to be tested until all bad smell detection types of the software project to be tested are detected.

[0059] In this embodiment of the invention, after receiving the input software project to be detected, the current bad smell detection type of the software project to be detected is obtained, the associated software metrics of the current bad smell detection type are obtained, the values ​​and average values ​​of the associated software metrics in each code entity of the software project to be detected are calculated, and then the pre-detection rules of the current bad smell detection type are obtained. According to the pre-detection rules, all code entities in the software project to be detected are pre-detected to obtain the bad smell suspected entity set of the current bad smell detection type. According to the influence factor set of the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, each bad smell suspected entity in the bad smell suspected entity set is finally detected to obtain the bad smell entity set with the current bad smell detection type. Thus, bad smell detection parameters are selected dynamically and differentiated according to the software project to be detected, thereby improving the detection accuracy of the software project to be detected.

[0060] Example 2:

[0061] Figure 2 The implementation flow of the code smell detection method provided in Embodiment 2 of the present invention is shown. For ease of explanation, only the parts related to the embodiments of the present invention are shown, and are described in detail below:

[0062] In step S201, the symbiotic relationship between a preset number of bad taste types in the bad taste instance dataset is obtained, and the symbiotic relationship between bad tastes is quantified into an influence factor.

[0063] This invention is applicable to computer devices, such as personal computers and servers, for detecting code smells in software source code. Here, the software for smell detection is represented or described as a software project to be detected. Specifically, the software project to be detected can be a compressed file, which may include source code files and configuration files for software operation. When detecting code smells in the software project to be detected, different types of code smells need to be detected in all source code files within the project. For example, these code smells may include excessively large classes, excessively long methods, message chains, excessive feature envy, and spaghetti code.

[0064] In this embodiment of the invention, a preset number of off-flavor types are the off-flavor types that may need to be detected. To obtain the final off-flavor detection categories for the software project to be tested, it is necessary to obtain the pairwise influence factors between the preset number of off-flavor types. Then, based on the influence factors, the preset number of off-flavor types are filtered to obtain the off-flavor detection categories for the software project to be tested. When obtaining the influence factors between off-flavors, the synergistic relationships between the preset number of off-flavor types are obtained through the off-flavor instance dataset, and the synergistic relationships between off-flavors are quantified into influence factors. Optionally, the quantification formula for quantifying the synergistic relationships between off-flavors into influence factors is as follows: Where i≠j, |CS represents the influence factor of bad taste type i on bad taste type j. i | represents the number of times the unpleasant taste type i appears, |CS i ∧CS j | represents the number of times that bad taste type i and bad taste type j co-occur, thus accurately representing the synergistic relationship between bad taste types through the influence factor, improving the accuracy of subsequent detection.

[0065] In step S202, all pairs of bad taste types with influence factors greater than the preset factor threshold are obtained, and the bad taste types corresponding to all bad taste type pairs are determined as bad taste detection types. Based on the number of elements in the influence factor set of each bad taste detection type, the detection priority of all bad taste detection types is determined.

[0066] In this embodiment of the invention, when the influence factor between any two bad taste types is greater than a preset factor threshold, it indicates a strong correlation between the two bad taste types, which will significantly affect the detection results of bad tastes. In this case, the bad taste type pair (two bad tastes) corresponding to the influence factor greater than the preset factor threshold is set as a bad taste detection type. As an example, if the influence factor of the bad taste Messgae Chain on the bad taste Large Class is 0.13 and the preset factor threshold is 0.1, then both the bad taste Messgae Chain and the Large Class are set as bad taste detection types.

[0067] In this embodiment of the invention, the more elements in the influencing factor set of a bad taste (type), the more other bad tastes can influence that bad taste. If the influencing factor set is empty, it means that no other bad taste influences that bad taste, but that bad taste may influence other bad tastes. Therefore, to improve the accuracy of subsequent bad taste detection, after obtaining all the bad taste types to be detected, the detection priority of all bad taste types is determined according to the number of elements in the influencing factor set of each bad taste type. Specifically, the fewer the elements in the influencing factor set, the higher the detection priority of the corresponding bad taste type; the more the elements in the influencing factor set, the lower the detection priority of the corresponding bad taste type. That is, bad taste types with higher detection priority are detected first, and bad taste types with lower detection priority are detected later. As an example, the corresponding bad taste types can be stored in the bad taste detection queue according to the detection priority from high to low, so that the bad taste types can be detected in the subsequent detection according to the detection priority from high to low.

[0068] In step S203, the current bad smell detection type of the software project to be tested is obtained according to the detection priority of all bad smell detection types.

[0069] In this embodiment of the invention, after the detection priority of all bad smell detection types is determined, bad smells of the software project to be detected can be detected in sequence according to the detection priority of bad smell detection types. The current bad smell detection type is the bad smell with the highest detection priority among the remaining undetected bad smells.

[0070] In step S204, the associated software metrics of the current bad smell detection type are obtained, and the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities are calculated.

[0071] In this embodiment of the invention, each source code file in the software project to be detected may include multiple code entities, such as class-level code entities and method-level code entities. When detecting code smells in the software source code, the detection is performed by identifying the types of code smells possessed by each code entity. Different combinations of software metrics can determine and detect different types of code smells within a code entity. After obtaining the current type of code smell detected in the software project to be detected, the associated software metrics for the current type of code smell are obtained. The value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities are calculated.

[0072] Optionally, when calculating the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities, all source code files in the software project to be detected are traversed. The class-level code entities and method-level code entities in the source code files are parsed and transformed into class-level abstract syntax trees and method-level abstract syntax trees. The node structure information and semantic information of the class-level abstract syntax trees and method-level abstract syntax trees are extracted. Finally, based on the node structure information and semantic information, the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities are calculated. This is then used to subsequently obtain differentiated bad smell pre-detection rules for the software project to be detected, thereby improving the accuracy of bad smell detection. Further, in some embodiments, an Abstract Syntax Tree (AST) parser is used to parse and transform the class-level code entities and method-level code entities in the source code files into class-level abstract syntax trees and method-level abstract syntax trees.

[0073] In step S205, the initial pre-detection rule for the current bad odor detection type is obtained, and the average value is used to instantiate the initial pre-detection rule to obtain the pre-detection rule for the current bad odor detection type.

[0074] In this embodiment of the invention, code smells are described by corresponding software metrics. For example, software metrics used to describe spaghetti code can be LOC (Lines of Code), NMNOPARAM (Number of Methods without Parameters), and NOGV (Number of Global Variables). When determining the type of code smell, pre-detection or pre-judgment can be performed on its associated software metrics to pre-determine whether the software project to be detected has a code smell detection type. The initial pre-detection rule is set based on the value of each associated software metric in each code entity in the software and the average value of all associated software metrics for the code smell detection type in all code entities. In this way, the code smell detection threshold can be set differently for different software projects to be detected, thereby improving the accuracy of the detection results of the software projects to be detected. Therefore, before obtaining the initial pre-detection rule for the current code smell detection type, a pre-detection rule for each code smell detection type can be pre-set. Here, the pre-detection rule is called the initial pre-detection rule. Specifically, it can be set through expert knowledge or extracted by a deep learning network. After obtaining the initial pre-detection rules for the current bad smell detection type, the initial pre-detection rules are instantiated using the average of the associated software metrics for the bad smell detection type across all code entities in the software project to be detected, thus obtaining the pre-detection rules for the current bad smell detection type. For example, the pre-detection rule for the bad smell Spaghetti Code is: loc > loc avg &nmnoparam>nmnoparam avg &nogv>nogv avg , where loc avg nmnoparam represents the average value of the software metric loc across all code entities in the software project to be tested. avg Nogv represents the average value of the software metric nmnoparam across all code entities in the software project to be tested. avg This represents the average value of the software metric nogv across all code entities in the software project to be tested.

[0075] In step S206, all code entities in the software project to be detected are pre-detected according to the pre-detection rules to obtain the set of suspected bad smell entities of the current bad smell detection type.

[0076] In this embodiment of the invention, the pre-detection rule can pre-detect all code entities in the software project to be detected, obtaining a set of suspected bad smell entities for the current bad smell detection category, that is, all code entities in the software project to be detected that may have the current bad smell detection category. Specifically, when pre-detecting each code entity in the software project to be detected, the value of each associated software metric in the code entity can be compared with its corresponding average value. Then, logical operations are performed on the comparison results of all associated software metrics, and the final operation result determines whether the current code entity is a suspected bad smell entity.

[0077] In step S207, based on the set of influencing factors for the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, a final inspection is performed on each bad smell suspect entity in the bad smell suspect entity set to obtain and output the bad smell entity set with the current bad smell detection type.

[0078] In this embodiment of the invention, the symbiotic relationship between bad smells is quantified into an influence factor. Then, based on the influence factor set of the current bad smell detection type, the values ​​and average values ​​of associated software metrics, a final inspection is performed on each suspected bad smell entity in the suspected bad smell entity set, resulting in a bad smell entity set with the current bad smell detection type. This ultimately achieves the detection of the current bad smell detection type in the software project to be inspected. When a bad smell i appears in a code entity, another bad smell j also appears simultaneously in the same code entity. It can be considered that bad smell i has an influence on bad smell j. The influence factor can be understood as the degree of influence of one bad smell on another. For example, the influence factor of the bad smell Message Chain on the bad smell Large Class is 0.13, indicating that when Message Chain appears in a code entity, the probability of Large Class appearing simultaneously is 0.13. The set of influencing factors for a bad flavor is the set of corresponding influencing factors of other bad flavors that affect the bad flavor. Specifically, for example, the set of influencing factors for the bad flavor Long Method can be represented as: {(Feature Envy, 0.18), (Spaghetti Code, 0.12)}. From this set of influencing factors, it can be seen that the bad flavors that affect Long Method are Feature Envy and Spaghetti Code. The influence factor of Feature Envy on Long Method is 0.18, and the influence factor of Spaghetti Code on Long Method is 0.12.

[0079] Optionally, the step of performing a final inspection on each suspected bad smell entity in the suspected bad smell entity set based on the influencing factor set of the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, includes:

[0080] (1) Calculate the initial probability value of the suspected bad smell entity being detected based on the set of influencing factors and detection priority of the current bad smell detection type;

[0081] In this embodiment of the invention, the initial probability value is used to preliminarily determine whether a suspected entity of a bad odor possesses the current bad odor detection category during the final detection process. Optionally, the initial probability value of the currently detected suspected bad odor entity can be calculated using the following formula:

[0082]

[0083] in, Indicates the current type of bad smell detected, S. j The set of influencing factors, when this set is empty, P ini =0, Indicates the bad smell S during the current bad smell detection. j The suspected odor entity C currently being detected k The set of impact factors (actual set of impact factors) It is F Sj A subset of, m represents The number of elements in S indicates bad taste j In C k Influence factors in pass Specifically, it retrieves detected bad odor types with a higher detection priority than the current bad odor detection type, such as the suspected bad odor entity C. k If it does not have the detected unpleasant odor type, then from Remove the set elements (influence factors) corresponding to the detected bad odor type.

[0084] (2) Based on the values ​​and average values ​​of the associated software metrics, calculate the metric deviation values ​​of all associated software metrics in the currently detected bad smell suspect entities;

[0085] In this embodiment of the invention, based on the values ​​and average values ​​of associated software metrics, the metric deviation values ​​of all associated software metrics in the currently detected suspected code smell entity are calculated to further accurately assess the deviation values ​​of associated software metrics for the current code smell detection type in the currently detected code entity. The deviation value reflects the degree of deviation of a software metric from software development standards; the greater the deviation, the greater the likelihood that the suspected code smell entity actually possesses the current code smell detection type. Optionally, the metric deviation values ​​of all associated software metrics in the currently detected suspected code smell entity are calculated using the following formula:

[0086]

[0087] Where Mi represents the value of software metric i in the suspected bad smell entity, Mi avg This represents the average value of software metric i across the software projects under test, (m1, m2, m3, ..., m 10 ) represents the preset boundary value.

[0088] (3) Based on the initial probability value and the metric deviation values ​​of all associated software metrics, obtain the final probability value of the currently detected bad taste suspect entity having the current bad taste detection type.

[0089] (4) When the final probability value is greater than the preset category threshold, the currently detected bad taste suspect entity is determined to have the current bad taste detection category; otherwise, it does not have the current bad taste detection category.

[0090] In this embodiment of the invention, after obtaining the initial probability value and the metric deviation values ​​of all associated software metrics for the current bad smell detection category, the final probability value of the currently detected bad smell suspect entity having the current bad smell detection category can be obtained. When the final probability value is greater than a preset category threshold, it is determined that the currently detected bad smell suspect entity has the current bad smell detection category; otherwise, it does not have the current bad smell detection category. Optionally, the final probability value of the currently detected bad smell suspect entity having the current bad smell detection category can be obtained using the following formula:

[0091]

[0092] Among them, P CS This indicates the suspected entity C with the unpleasant smell currently being detected. k It has the current bad taste detection type S j The possibility, P ini Let P represent the initial probability. Mi For the current types of bad odor detection S j The metric deviation of the associated software metric i.

[0093] In step S208, it is determined whether all types of bad smells in the software project to be tested have been detected.

[0094] In this embodiment of the invention, it is determined whether all types of off-odor detection for the software project to be tested have been completed. When all types of off-odor detection for the software project to be tested have been completed, it indicates that all off-odor detection for the software project to be tested has been completed, and step S209 is executed to end the testing of the software project to be tested. When all types of off-odor detection for the software project to be tested have not been completed, step S210 is executed to detect the remaining types of off-odor detection for the software project to be tested.

[0095] In step S209, the detection of the software item to be detected is terminated.

[0096] In step S210, the next bad smell detection type of the software project to be tested is obtained, and the next bad smell detection type is set as the current bad smell detection type.

[0097] In this embodiment of the invention, when all bad smell detection types of the software project to be tested have not been detected, the next bad smell detection type of the software project to be tested is obtained, the next bad smell detection type is set as the current bad smell detection type, and the process jumps to step S204 to continue to detect the next bad smell detection type of the software project to be tested until all bad smell detection types of the software project to be tested are detected.

[0098] In this embodiment of the invention, after receiving the input software project to be detected, the current bad smell detection type of the software project to be detected is obtained, the associated software metrics of the current bad smell detection type are obtained, the values ​​and average values ​​of the associated software metrics in each code entity of the software project to be detected are calculated, and then the pre-detection rules of the current bad smell detection type are obtained. According to the pre-detection rules, all code entities in the software project to be detected are pre-detected to obtain the bad smell suspected entity set of the current bad smell detection type. According to the influence factor set of the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, each bad smell suspected entity in the bad smell suspected entity set is finally detected to obtain the bad smell entity set with the current bad smell detection type. Thus, bad smell detection parameters are selected dynamically and differentiated according to the software project to be detected, thereby improving the detection accuracy of the software project to be detected.

[0099] Example 3:

[0100] Figure 3 The structure of the code odor detection device provided in Embodiment 3 of the present invention is shown. For ease of explanation, only the parts related to the embodiments of the present invention are shown, including:

[0101] The detection type acquisition unit 31 is used to receive the input software item to be detected and acquire the current bad smell detection type of the software item to be detected.

[0102] The value calculation unit 32 is used to obtain the associated software metrics of the current bad smell detection type, calculate the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities;

[0103] The code pre-inspection unit 33 is used to obtain the pre-inspection rules for the current bad smell detection type and the average value, and to perform pre-inspection on all code entities in the software project to be inspected according to the pre-inspection rules to obtain the set of suspected bad smell entities for the current bad smell detection type.

[0104] The code final inspection unit 34 is used to perform a final inspection on each suspected bad smell entity in the suspected bad smell entity set based on the influence factor set of the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, to obtain and output the bad smell entity set with the current bad smell detection type; and

[0105] The detection setting unit 35 is used to obtain the next bad smell detection type of the software project to be tested when all bad smell detection types of the software project to be tested have not been detected, set the next bad smell detection type as the current bad smell detection type and trigger the value calculation unit 32 to continue to detect the next bad smell detection type until all bad smell detection types of the software project to be tested are detected.

[0106] In this embodiment of the invention, for the sake of convenience and brevity, only the division of the above-described functional units and modules is used as an example. In practical applications, the above functions can be allocated to different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to achieve all or part of the functions described above. Each unit and module of the device can be implemented by corresponding hardware or software units. Each unit and module can be an independent hardware or software unit, or it can be integrated into a single hardware or software unit, which is not intended to limit the invention. In addition, the specific names of each functional unit and module are only for easy differentiation and are not intended to limit the scope of protection of this application. The specific working process of the units and modules in the device can be referred to the corresponding description in the foregoing method embodiments, and will not be repeated here.

[0107] Example 4:

[0108] Figure 4 The structure of the computer device provided in Embodiment 4 of the present invention is shown. For ease of explanation, only the parts related to the embodiments of the present invention are shown.

[0109] The computer device 4 of this embodiment includes a processor 40, a memory 41, and a computer program 42 stored in the memory 41 and executable on the processor 40. When the processor 40 executes the computer program 42, it implements the steps described in the various code smell detection method embodiments above, for example... Figure 1 The steps S101 to S107 are shown. Alternatively, when the processor 40 executes the computer program 42, it implements the functions of each unit in the above-described device embodiment, for example... Figure 3 The functions of units 31 to 35 are shown.

[0110] The computer device in this embodiment of the invention can be a personal computer or a server. The steps implemented by the processor 40 in the computer device 4 when executing the computer program 42 to implement the code smell detection method can be referred to the description of the foregoing method embodiments, and will not be repeated here.

[0111] Example 5:

[0112] In this embodiment of the invention, a computer-readable storage medium is provided, which stores a computer program. When executed by a processor, the computer program implements the steps in the above-described code smell detection method embodiment, for example... Figure 1 Steps S101 to S107 are shown. Alternatively, when the computer program is executed by a processor, it implements the functions of each unit in the above-described device embodiment, for example... Figure 3 The functions of units 31 to 35 are shown.

[0113] In this embodiment of the invention, after receiving the input software project to be detected, the current bad smell detection type of the software project to be detected is obtained, the associated software metrics of the current bad smell detection type are obtained, the values ​​and average values ​​of the associated software metrics in each code entity of the software project to be detected are calculated, and then the pre-detection rules of the current bad smell detection type are obtained. According to the pre-detection rules, all code entities in the software project to be detected are pre-detected to obtain the bad smell suspected entity set of the current bad smell detection type. According to the influence factor set of the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, each bad smell suspected entity in the bad smell suspected entity set is finally detected to obtain the bad smell entity set with the current bad smell detection type. Thus, bad smell detection parameters are selected dynamically and differentiated according to the software project to be detected, thereby improving the detection accuracy of the software project to be detected.

[0114] The computer-readable storage medium of this invention can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this invention, the computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device.

[0115] The above embodiments are merely illustrative of the technical solutions of the present invention and are not intended to limit them. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that the scope of disclosure involved in the above embodiments is not limited to technical solutions formed by specific combinations of the above technical features, but should also cover other technical solutions formed by arbitrary combinations of the above technical features or their equivalent features without departing from the above-disclosed concept. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the protection scope of the present invention.

[0116] Furthermore, while the operations are described in a specific order, this should not be construed as requiring these operations to be performed in the specific order shown or in sequential order. In certain circumstances, multitasking and parallel processing may be advantageous. Similarly, while several specific implementation details are included in the above discussion, these should not be construed as limiting the scope of the invention. Certain features described in the context of individual embodiments may also be implemented in combination in a single embodiment. Conversely, various features described in the context of a single embodiment may also be implemented individually or in any suitable sub-combination in multiple embodiments.

Claims

1. A method for detecting code smells, characterized in that, The method includes the following steps: Step a: Receive the input software item to be tested and obtain the current bad smell detection type of the software item to be tested; Step b: Obtain the associated software metrics of the current bad smell detection type, and calculate the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities; Step c: Based on the current bad smell detection type and the average value, obtain the pre-detection rule for the current bad smell detection type, and perform pre-detection on all code entities in the software project to be detected according to the pre-detection rule to obtain the bad smell suspect entity set for the current bad smell detection type; Step d: Based on the set of influencing factors for the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, perform a final inspection on each bad smell suspect entity in the bad smell suspect entity set to obtain and output the bad smell entity set with the current bad smell detection type. Step e: When all the bad smell detection types of the software project to be tested have not been detected, obtain the next bad smell detection type of the software project to be tested, set the next bad smell detection type as the current bad smell detection type and jump to step b to detect the next bad smell detection type until all the bad smell detection types of the software project to be tested are detected. The steps for obtaining the current bad smell detection type of the software project to be tested include: Obtain the symbiotic relationships between a preset number of bad taste types in the bad taste instance dataset, and quantify the symbiotic relationships between bad tastes into influencing factors; Obtain all pairs of bad taste types whose influence factors are greater than a preset factor threshold, determine the bad taste types corresponding to all pairs of bad taste types as bad taste detection types, and determine the detection priority of all bad taste detection types according to the number of elements in the influence factor set of each bad taste detection type. According to the detection priority of all the bad smell detection types, obtain the current bad smell detection type of the software project to be tested; The steps for performing a final inspection on each suspected bad smell entity in the suspected bad smell entity set based on the influencing factor set of the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, include: Based on the set of influencing factors and detection priority of the current bad smell detection type, calculate the initial probability value of the currently detected bad smell suspect entity; Based on the values ​​and average values ​​of the associated software metrics, calculate the metric deviation values ​​of all associated software metrics in the currently detected bad-taste suspect entity; Based on the initial probability value and the metric deviation value, the final probability value of the currently detected bad smell suspect entity having the current bad smell detection type is obtained; When the final probability value is greater than the preset category threshold, it is determined that the currently detected bad smell suspect entity has the current bad smell detection category; otherwise, it does not have the current bad smell detection category.

2. The method as described in claim 1, characterized in that, The formula for quantifying the synergistic relationship between the aforementioned unpleasant odors into influencing factors is as follows: ,in, , Indicates the types of bad taste Types of bad taste Influence factors Indicates the types of bad taste Number of times it appears Indicates the types of bad taste and types of bad taste The number of times they appear together.

3. The method as described in claim 1, characterized in that, The steps of calculating the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities include: Traverse all source code files in the software project to be tested, parse and convert the class layer code entities and method layer code entities in the source code files into class layer abstract syntax trees and method layer abstract syntax trees, and extract the node structure information and semantic information of the class layer abstract syntax trees and method layer abstract syntax trees; Based on the node structure information and semantic information, calculate the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities.

4. The method as described in claim 3, characterized in that, The AST parser parses and transforms the class-level code entities and method-level code entities in the source code file into class-level abstract syntax trees and method-level abstract syntax trees.

5. The method as described in claim 1, characterized in that, The step of obtaining the pre-detection rule for the current bad odor detection type based on the current bad odor detection type and the average value includes: Obtain the initial pre-detection rule for the current bad odor detection type, and instantiate the initial pre-detection rule using the average value to obtain the pre-detection rule for the current bad odor detection type.

6. A code smell detection device, characterized in that, The device includes: The detection type acquisition unit is used to receive the input software item to be detected and acquire the current bad smell detection type of the software item to be detected. The value calculation unit is used to obtain the associated software metrics of the current bad smell detection type, and calculate the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities; The code pre-inspection unit is used to obtain the pre-inspection rules for the current bad smell detection type based on the current bad smell detection type and the average value, and to perform pre-inspection on all code entities in the software project to be inspected according to the pre-inspection rules to obtain the set of suspected bad smell entities for the current bad smell detection type. The code final inspection unit is used to perform a final inspection on each suspected bad smell entity in the suspected bad smell entity set based on the set of influence factors for the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, to obtain and output a set of bad smell entities with the current bad smell detection type; and The detection setting unit is used to obtain the next bad smell detection type of the software project to be tested when all bad smell detection types of the software project to be tested have not been detected, set the next bad smell detection type as the current bad smell detection type and trigger the value calculation unit to detect the next bad smell detection type until all bad smell detection types of the software project to be tested are detected. When the detection type acquisition unit acquires the current bad smell detection type of the software project to be detected, it includes: Obtain the symbiotic relationships between a preset number of bad taste types in the bad taste instance dataset, and quantify the symbiotic relationships between bad tastes into influencing factors; Obtain all pairs of bad taste types whose influence factors are greater than a preset factor threshold, determine the bad taste types corresponding to all pairs of bad taste types as bad taste detection types, and determine the detection priority of all bad taste detection types according to the number of elements in the influence factor set of each bad taste detection type. According to the detection priority of all the bad smell detection types, obtain the current bad smell detection type of the software project to be tested; When the code final inspection unit performs a final inspection on each suspected bad smell entity in the suspected bad smell entity set based on the set of influencing factors of the current bad smell detection type, the values ​​and average values ​​of the associated software metrics, it includes: Based on the set of influencing factors and detection priority of the current bad smell detection type, calculate the initial probability value of the currently detected bad smell suspect entity; Based on the values ​​and average values ​​of the associated software metrics, calculate the metric deviation values ​​of all associated software metrics in the currently detected bad-taste suspect entity; Based on the initial probability value and the metric deviation value, the final probability value of the currently detected bad smell suspect entity having the current bad smell detection type is obtained; When the final probability value is greater than the preset category threshold, it is determined that the currently detected bad smell suspect entity has the current bad smell detection category; otherwise, it does not have the current bad smell detection category.

7. The apparatus as claimed in claim 6, characterized in that, The formula for quantifying the synergistic relationship between the aforementioned unpleasant odors into influencing factors is as follows: ,in, , Indicates the types of bad taste Types of bad taste Influence factors Indicates the types of bad taste Number of times it appears Indicates the types of bad taste and types of bad taste The number of times they appear together.

8. The apparatus as claimed in claim 6, characterized in that, When calculating the value of each associated software metric in each code entity of the software project to be tested and the average value of each associated software metric in all code entities, the value calculation unit includes: Traverse all source code files in the software project to be tested, parse and convert the class layer code entities and method layer code entities in the source code files into class layer abstract syntax trees and method layer abstract syntax trees, and extract the node structure information and semantic information of the class layer abstract syntax trees and method layer abstract syntax trees; Based on the node structure information and semantic information, calculate the value of each associated software metric in each code entity of the software project to be detected and the average value of each associated software metric in all code entities.

9. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the method as described in any one of claims 1 to 5.

10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method as described in any one of claims 1 to 5.