CN115827438A - Method for determining circle complexity of code, electronic device and storage medium - Google Patents
Method for determining circle complexity of code, electronic device and storage medium Download PDFInfo
- Publication number
- CN115827438A CN115827438A CN202211438912.8A CN202211438912A CN115827438A CN 115827438 A CN115827438 A CN 115827438A CN 202211438912 A CN202211438912 A CN 202211438912A CN 115827438 A CN115827438 A CN 115827438A
- Authority
- CN
- China
- Prior art keywords
- statement
- complexity
- block
- code
- determining
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 123
- 230000004044 response Effects 0.000 claims abstract description 7
- 238000004590 computer program Methods 0.000 claims description 11
- 238000009825 accumulation Methods 0.000 claims description 3
- 238000004364 calculation method Methods 0.000 description 5
- 238000010586 diagram Methods 0.000 description 5
- 238000012423 maintenance Methods 0.000 description 2
- 230000009286 beneficial effect Effects 0.000 description 1
- 125000004122 cyclic group Chemical group 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 230000006870 function Effects 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 230000003287 optical effect Effects 0.000 description 1
Images
Landscapes
- Stored Programmes (AREA)
Abstract
The application discloses a method for determining the circle complexity of a code, an electronic device and a storage medium, wherein the method for determining the circle complexity of the code comprises the following steps: analyzing the target code file to obtain a corresponding syntax tree; if the grammar tree has class declaration nodes, acquiring class bodies of the class declaration nodes; if the method declaration node exists in the class, judging whether the method declaration node corresponds to a block statement list or not, wherein the block statement list is formed on the basis of block statements in the method declaration node; in response to the block statement list existing, determining an initial circle complexity; and determining the final circle complexity corresponding to the method declaration node based on the block statement list and the initial circle complexity. By the method, the code circle complexity determining method can achieve the final circle complexity of the declaration node of the automatic searching method.
Description
Technical Field
The present application relates to the field of computer technologies, and in particular, to a method for determining a degree of complexity of a code, an electronic device, and a storage medium.
Background
In the process of developing a project according to the requirements of the project, because different developers have different understandings on business logic, codes written by different developers are different, and the developers always use a method with complicated logic in the process of developing the codes. However, the more complex the method, the higher the complexity of the method, the more difficult the function is to understand and maintain. For subsequent code maintenance work, methods with high circle complexity are usually selected manually, so as to optimize codes corresponding to the methods.
Disclosure of Invention
The application provides a method for determining the circle complexity of a code, electronic equipment and a storage medium, which can realize the automatic determination of the circle complexity of a method statement node in the code.
In order to solve the technical problem, the present application adopts a technical solution that: a method for determining the complexity of a circle of a code is provided, and the method comprises the following steps: analyzing the target code file to obtain a corresponding syntax tree; if the grammar tree has class declaration nodes, acquiring class bodies of the class declaration nodes; if the method declaration node exists in the class, judging whether the method declaration node corresponds to a block statement list or not; the block statement list is formed based on the block statements in the method declaration node; in response to the block statement list existing, determining an initial circle complexity; and determining the final circle complexity corresponding to the method declaration node based on the block statement list and the initial circle complexity.
Wherein, before parsing the target code file and obtaining the corresponding syntax tree, the method further comprises: acquiring an original code folder, wherein the original code folder comprises a plurality of code files; an object code file is determined from the plurality of code files.
The method for determining the final circle complexity corresponding to the statement node of the method based on the block statement list and the initial circle complexity comprises the following steps: traversing the block sentences corresponding to the block sentence list; in the traversal process, if the current block statement is a first preset statement, adjusting the initial circle complexity according to a first preset rule; and if the current block sentence is a second preset sentence, adjusting the initial circle complexity according to a second preset rule until the traversal is finished to obtain the final circle complexity.
If the current block statement is a first preset statement, adjusting the initial circle complexity according to a first preset rule, including: and if the current block statement is the first preset statement, performing accumulation operation on the initial circle complexity.
The first preset statement at least comprises one of a conditional judgment statement, a conditional loop statement, a repeat statement, a loop statement, a statement and an OR statement.
If the current block statement is a second preset statement, adjusting the initial round complexity according to a second preset rule, including: if the current block statement is a second preset statement, acquiring a label corresponding to the current block statement; and adjusting the complexity of the initial circle according to the number of the labels.
Wherein the second preset sentence is a selection sentence.
After determining the final circle complexity corresponding to the method declaration node, the method comprises the following steps: and when the final circle complexity exceeds the threshold value, displaying the method statement node and the final circle complexity to prompt a user to modify the method corresponding to the method statement node.
In order to solve the above technical problem, another technical solution adopted by the present application is: there is provided an electronic device comprising a memory for storing a computer program and a processor for executing the computer program to implement the circle complexity determination method of the code.
In order to solve the above technical problem, another technical solution adopted by the present application is: there is provided a computer-readable storage medium having stored therein a computer program for executing the circle complexity determining method of the above code when executed by a processor.
The beneficial effect of this application is: different from the prior art, the method for determining the circle complexity of the code obtains a corresponding syntax tree by analyzing a target code file, then judges whether the syntax tree has a class declaration node or not, acquires a class body of the class declaration node if the syntax tree has the class declaration node, further judges whether the class body has the method declaration node or not, judges whether the method declaration node corresponds to a block statement list if the class body has the method declaration node, wherein the block statement list is formed based on block statements in the method declaration node, determines the initial circle complexity in response to the existence of the block statement list, and finally determines the final circle complexity corresponding to the method declaration node based on the block statement list and the initial circle complexity. By the method, the circle complexity corresponding to the method declaration node in the code file can be automatically found, and the efficiency of determining the circle complexity of the code is improved.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the description of the embodiments are briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without creative efforts. Wherein:
fig. 1 is a schematic flowchart of a first embodiment of a method for determining a degree of complexity of a code provided in the present application;
FIG. 2 is a schematic flow chart diagram illustrating an embodiment of step 15 provided herein;
FIG. 3 is a flowchart illustrating a second embodiment of a method for determining a degree of complexity of a code provided in the present application;
FIG. 4 is a flowchart illustrating a third embodiment of a method for determining a degree of complexity of a code provided in the present application;
FIG. 5 is a schematic structural diagram of an embodiment of an electronic device provided in the present application;
FIG. 6 is a block diagram of an embodiment of a computer-readable storage medium provided in the present application.
Detailed Description
The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are only a part of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
Referring to fig. 1, fig. 1 is a schematic flowchart of a first embodiment of a method for determining a degree of complexity of a code provided in the present application, where the method includes:
step 11: and analyzing the target code file to obtain a corresponding syntax tree.
In some embodiments, a repository (e.g., gitlab) is managed based on project source code, using a git clone instruction (or automation script) to obtain project source code, and then traversing folders corresponding to the project source code to determine target code files from the folders. Wherein the object code file may be a file suffixed with java.
In addition, files obtained by traversing the folder of the project source code may be saved to a file list, such as a java file list.
In some embodiments, the object code file may be parsed into a Syntax Tree, such as an Abstract Syntax Tree (AST) that is an abstraction of the Java language, where each node in the Syntax Tree can correspond to a Java Syntax, that is, a Java file is composed of several nodes.
Step 12: and if the grammar tree has the class declaration node, acquiring the class body of the class declaration node.
Wherein, the class declaration node is represented by class classification in the code, and the class body of the class declaration node is represented by class body in the code.
In some embodiments, it is necessary to determine whether a class declaration node exists in the obtained syntax tree, and if so, obtain a class body of the class declaration node, otherwise, re-determine a next object code file, and analyze a new object code file.
Step 13: and if the method declaration node exists in the class, judging whether the method declaration node corresponds to a block statement list or not, wherein the block statement list is formed on the basis of the block statements in the method declaration node.
Wherein, the method declaration node is represented by method declaration in the code, and the block statement is represented by block status in the code.
In some embodiments, when it is determined that a class exists, method declaration nodes in the syntax tree may be traversed to determine whether a method declaration node exists that contains a list of block statements.
A block statement is a way to group statements together, so that a block statement acts syntactically as a single statement.
Step 14: in response to the block statement list existing, an initial degree of complexity is determined.
The Cyclic Complexity (CC) is a measure of the complexity of the code and is represented by V (G). The larger the circle complexity, the more complicated the judgment logic of the explanation code.
Common calculation methods for circle complexity include a point-edge calculation method and a node judgment method. The calculation formula of the point edge calculation method is V (G) = E-N +2, wherein E represents the number of edges in a syntax tree, N represents the number of nodes in the syntax tree, and the nodes are connected through the edges; the formula of the node determination method is V (G) = P +1, where P is the number of determination nodes, and the determination nodes may be represented by fields, such as if, while, for, case, catch, etc. fields with logical relationships.
In some embodiments, if the block statement list exists, it indicates that a corresponding code logic exists in the method corresponding to the method declaration node, which means that a corresponding degree of complexity of a circle can be obtained.
Specifically, the round complexity is the round complexity of the method declaration node.
When the block statement list exists, an initial value may be set for the round-robin degree of the method declaration node, and in some embodiments, the initial round-robin degree of the method declaration node may be set to an integer greater than 0, such as 1, 2, or 3.
In some embodiments, the initial degree of circularity may be a default value set in advance.
Step 15: and determining the final circle complexity corresponding to the method declaration node based on the block statement list and the initial circle complexity.
In some embodiments, on the premise that the block statement list is known, the specific block statement in the block statement list may be used for block statement identification, so as to adjust the initial round complexity.
In some embodiments, referring to fig. 2, step 15 may comprise the following process:
step 21: and traversing the block statements corresponding to the block statement list.
It is to be understood that the block statement list includes a plurality of block statements, and whether a block statement of the block statement list is a target statement may be determined by traversing the block statement list, where the target statement may be a first preset statement or a second preset statement.
Step 22: in the traversal process, if the current block statement is a first preset statement, the initial circle complexity is adjusted according to a first preset rule.
Specifically, the first preset statement at least includes one of a conditional judgment statement, a conditional loop statement, a repeat statement, a loop statement, and a statement and/or statement. Wherein, the conditional judging statement is represented by an if keyword in the code; the conditional loop statement is represented in code by a while keyword; the repeated sentences are represented by repeat keywords in the codes; the loop statements are represented in code by for keys; and statements are represented in the code by and keys; or statements are represented in code by or keys.
In some embodiments, whether the current block statement is the first preset statement may be determined by an xpath syntax, such as using "// block statement/[ in (ifstatement, whilestatemen, repeatstatement, force, andstatemen, authority) ]" to determine whether the current block statement is the first preset statement.
In some embodiments, if the current block statement is a first preset statement, an accumulation operation is performed on the initial round complexity according to a first preset rule. The first preset rule is that every time a block statement is determined to be a first preset statement, a fixed value is accumulated on the basis of the initial round complexity. If the initial circle complexity is 1, the fixed value is 1, the block statement list is traversed, and it is determined that N (≧ 0) block statements which are the first preset statement are contained in the block statement list, then the circle complexity is "1+1 × N".
Step 23: and if the current block sentence is a second preset sentence, adjusting the initial circle complexity according to a second preset rule until the traversal is finished to obtain the final circle complexity.
Specifically, the second preset sentence is a selection sentence. In the code, the selection statement is represented by a switch key.
In some embodiments, when it is determined that the current block statement is the second preset statement, obtaining a tag corresponding to the current block statement, and adjusting the initial round complexity according to the number of the tags. In the code, the tags corresponding to the second preset statement are represented by switchLable, and the number of the tags may be an integer greater than or equal to 0. Specifically, the number of labels corresponding to the current block node may be accumulated on the basis of the initial round-robin degree.
If the initial circle complexity is 1, the number of tags is M, the block statement list is traversed, it is determined that the block statement list contains N block statements which are second preset statements, the number of tags corresponding to the first second preset statement is x1, the number of tags corresponding to the second preset statement is x2 \8230, the number of tags corresponding to the Nth second preset statement is xn, the circle complexity is '1 + x1+ x2+ \8230 \ 8230 \8230 + xn'. It should be noted that the number of labels corresponding to different second preset sentences may be the same or different.
In some embodiments, the number of tags of the second preset statement may be determined by an xpath syntax, such as using "// switchstat/[ count (/ switchlab) ]".
In addition, when the block statement list includes both the first preset statement and the second preset statement, the calculation formula of the final round complexity of the method declaration node is "initial round complexity + first preset statement quantity 1+ label quantity". If the initial circle complexity is 1, the number of the first preset statements is 12, the number of the second preset statements is 3, wherein the number of the tags of the first second preset statement is 1, the number of the tags of the second preset statement is 2, and the number of the tags of the third second preset statement is 3, then the final circle complexity is "1+12+1+2+3=19".
Different from the prior art, the method for determining the circle complexity of the code can acquire the class of the declaration node when the class declaration node exists in the syntax tree, judge whether the method declaration node has a corresponding block statement list when the class exists, further acquire the initial circle complexity when the block statement list exists, and determine the final circle complexity of the method declaration node based on the initial circle complexity and the block statement list. By the method, the complexity of the nodes can be declared by an automatic searching method, and the efficiency of determining the complexity of the codes is improved.
Referring to fig. 3, fig. 3 is a schematic flowchart of a second embodiment of a method for determining a degree of complexity of a code provided in the present application, where the method includes:
step 31: an original code folder is obtained, wherein the original code folder comprises a plurality of code files.
Specifically, the original folder is a folder that contains the project source code. Wherein, the project source code can be obtained from the project source code management warehouse gitlab through git clone command.
In some embodiments, the original folder contains a plurality of files, which are files suffixed with the suffix.
Step 32: an object code file is determined from the plurality of code files.
In some embodiments, the target code file may be determined by obtaining file names of a plurality of files and determining a file with a file name meeting a preset requirement as the target code file.
For example, when code is written in the JAVA language, files with a suffix name of.java may be determined by traversing the original folder, and then target JAVA files may be determined from these.
For example, when writing code in the C language, the original folder is traversed to determine files with suffix name.c, and then the target code file is determined from these.c files.
Step 33: and analyzing the target code file to obtain a corresponding syntax tree.
Step 34: and if the grammar tree has the class declaration node, acquiring the class body of the class declaration node.
Step 35: and if the method declaration node exists in the class, judging whether the method declaration node corresponds to a block statement list or not, wherein the block statement list is formed on the basis of the block statements in the method declaration node.
And step 36: in response to the block statement list existing, an initial degree of complexity is determined.
Step 37: and determining the final complexity corresponding to the statement node of the method based on the block statement list and the initial complexity.
Steps 33 to 37 may have the same or similar technical features as those of the above embodiments, and are not described herein again.
Different from the prior art, the method for determining the circle complexity of the code can accurately find the final circle complexity of the method declaration node in an automatic mode, and the efficiency of determining the circle complexity of the code is improved.
Referring to fig. 4, fig. 4 is a schematic flowchart of a third embodiment of a method for determining a degree of complexity of a code provided in the present application, where the method includes:
step 41: an original code folder is obtained, wherein the original code folder comprises a plurality of code files.
Step 42: an object code file is determined from the plurality of code files.
Step 43: and analyzing the target code file to obtain a corresponding syntax tree.
Step 44: and if the grammar tree has the class declaration node, acquiring the class body of the class declaration node.
Step 45: if a method declaration node exists in the class, judging whether the method declaration node corresponds to a block statement list, wherein the block statement list is formed based on block statements in the method declaration node.
Step 46: in response to the block statement list existing, an initial degree of complexity is determined.
Step 47: and determining the final circle complexity corresponding to the method declaration node based on the block statement list and the initial circle complexity.
And 48: and when the final circle complexity exceeds the threshold value, displaying the method statement node and the final circle complexity to prompt a user to modify the method corresponding to the method statement node.
In some embodiments, the threshold is 5, 6, or 7, and if the final round complexity of the method declaration node exceeds the threshold, the logic of the target code file is too complex, and at this time, corresponding prompt/warning information may be generated to prompt the user to modify, such as split, the methods corresponding to the method declaration nodes for convenience of subsequent understanding and maintenance of the codes. The above mentioned threshold value can be set according to actual requirements.
Different from the prior art, the method for determining the circle complexity of the code can automatically and accurately find the final circle complexity of the method declaration node in each code file in a plurality of code files, and improves the efficiency of finding the circle complexity.
Referring to fig. 5, fig. 5 is a schematic structural diagram of an embodiment of an electronic device provided in the present application, where the electronic device 50 includes a memory 501 and a processor 502, the memory 501 is used to store a computer program, and the processor 502 is used to execute the computer program to implement the method for determining a degree of complexity of a code according to any of the above embodiments, which is not described herein again.
Referring to fig. 6, fig. 6 is a schematic structural diagram of an embodiment of a computer-readable storage medium provided in the present application, a computer program 601 is stored in the computer-readable storage medium 60, and when the computer program 601 is executed by a processor, the computer program is used to implement the method for determining a degree of complexity of a code according to any of the above embodiments, which is not described herein again.
In summary, the method for determining the circle complexity of the code provided by the application can automatically find the circle complexity corresponding to the method declaration node in the code file, improve the efficiency of determining the circle complexity of the code, and generate corresponding prompt/warning information when the circle complexity exceeds the threshold value so as to remind a user to modify the code with complex logic.
The processor referred to in this application may be referred to as a Central Processing Unit (CPU), may be an integrated circuit chip, or may be a general-purpose processor, a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field Programmable Gate Array (FPGA) or other programmable logic device, a discrete gate or transistor logic device, or a discrete hardware component.
The storage medium used in the present application includes various media that can store program codes, such as a usb disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), or an optical disk.
The above description is only for the purpose of illustrating embodiments of the present application and is not intended to limit the scope of the present application, and all modifications of equivalent structures and equivalent processes, which are made by the contents of the specification and the drawings of the present application or are directly or indirectly applied to other related technical fields, are also included in the scope of the present application.
Claims (10)
1. A method for determining a degree of complexity of a code, the method comprising:
analyzing the target code file to obtain a corresponding syntax tree;
if the grammar tree has class declaration nodes, acquiring class bodies of the class declaration nodes;
if a method declaration node exists in the class, judging whether the method declaration node corresponds to a block statement list or not; the block statement list is formed based on block statements in the method declaration node;
determining an initial circle complexity in response to the block statement list existing;
and determining a final circle complexity corresponding to the statement node of the method based on the block statement list and the initial circle complexity.
2. The method of claim 1,
before analyzing the object code file to obtain the corresponding syntax tree, the method further includes:
acquiring an original code folder; the original code folder comprises a plurality of code files;
the object code file is determined from the plurality of code files.
3. The method of claim 1,
determining a final round complexity corresponding to the method declaration node based on the block statement list and the initial round complexity, including:
traversing the block statement corresponding to the block statement list;
in the traversal process, if the current block statement is a first preset statement, adjusting the initial circle complexity according to a first preset rule;
and if the current block sentence is a second preset sentence, adjusting the initial circle complexity according to a second preset rule until the traversal is finished to obtain the final circle complexity.
4. The method of claim 3, wherein if the current block syntax is a first predetermined syntax, the adjusting the initial round complexity according to a first predetermined rule comprises:
and if the current statement is the first preset statement, performing accumulation operation on the initial circle complexity.
5. The method of claim 4, wherein the first preset statement comprises at least one of a conditional judgment statement, a conditional loop statement, a repeat statement, a loop statement, and a statement and/or a statement.
6. The method of claim 3, wherein if the current block syntax is a second predetermined syntax, the adjusting the initial round complexity according to a second predetermined rule comprises:
if the current block statement is a second preset statement, acquiring a label corresponding to the current block statement;
and adjusting the initial circle complexity according to the number of the labels.
7. The method of claim 6, wherein the second predetermined statement is a select statement.
8. The method of claim 1, wherein determining the final round-robin complexity corresponding to the method declaration node comprises:
and when the final circle complexity exceeds a threshold value, displaying the method statement node and the final circle complexity to prompt a user to modify a method corresponding to the method statement node.
9. An electronic device, characterized in that the electronic device comprises a memory for storing a computer program and a processor for executing the computer program to implement the method according to any of claims 1-8.
10. A computer-readable storage medium, in which a computer program is stored which, when being executed by a processor, is adapted to carry out the method of any one of claims 1-8.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202211438912.8A CN115827438A (en) | 2022-11-16 | 2022-11-16 | Method for determining circle complexity of code, electronic device and storage medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202211438912.8A CN115827438A (en) | 2022-11-16 | 2022-11-16 | Method for determining circle complexity of code, electronic device and storage medium |
Publications (1)
Publication Number | Publication Date |
---|---|
CN115827438A true CN115827438A (en) | 2023-03-21 |
Family
ID=85528664
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202211438912.8A Pending CN115827438A (en) | 2022-11-16 | 2022-11-16 | Method for determining circle complexity of code, electronic device and storage medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN115827438A (en) |
-
2022
- 2022-11-16 CN CN202211438912.8A patent/CN115827438A/en active Pending
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN110502227B (en) | Code complement method and device, storage medium and electronic equipment | |
US11042467B2 (en) | Automated searching and identification of software patches | |
CN112579466B (en) | Method and device for generating test cases and computer readable storage medium | |
KR20230130089A (en) | System and method for selection and discovery of vulnerable software packages | |
CN112970011B (en) | Pedigree in record query optimization | |
CN111240772B (en) | Block chain-based data processing method, device and storage medium | |
US20240256613A1 (en) | Data processing method and apparatus, readable storage medium, and electronic device | |
US10782942B1 (en) | Rapid onboarding of data from diverse data sources into standardized objects with parser and unit test generation | |
CN108170661B (en) | Method and system for managing rule text | |
CN110244954B (en) | Compiling method and equipment for application program | |
CN115796146A (en) | File comparison method and device | |
CN113568604B (en) | Method and device for updating wind control strategy and computer readable storage medium | |
CN113505889B (en) | Processing method and device of mapping knowledge base, computer equipment and storage medium | |
US20070226710A1 (en) | Efficient Generation of Executable File From Program Files When Some of the Program Files Expressly Incorporate Other Program Files | |
CN115827438A (en) | Method for determining circle complexity of code, electronic device and storage medium | |
Tukaram | Design and development of software tool for code clone search, detection, and analysis | |
CN117725920A (en) | Code change label generation method, device and equipment | |
CN114416174B (en) | Metadata-based model reconstruction method, device, electronic device and storage medium | |
CN113408250B (en) | Project file processing method and device | |
CN115510880A (en) | API document generation method, device, equipment and storage medium | |
CN111381814B (en) | Method, device and electronic device for generating syntax tree of code file | |
CN109710833B (en) | Method and apparatus for determining content node | |
CN113704382A (en) | Text matching method and device | |
CN107818100B (en) | SQL statement execution method and device | |
CN113609166A (en) | Search method, search device, computer equipment and computer-readable storage medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |