Cyclomatic complexity
Cyclomatic Complexity (Cyclomatic Complexity) is a measure of the complexity of a computer program. It is calculated based on the number of linear independent paths from the beginning to the end of the program.
The higher the cyclomatic complexity, the more difficult it is to maintain and optimize the code .
- Start from 1 and go through the program all the way down
- Once you encounter the following keywords, or other similar words, add 1: if, while, repeat, for, and, or
- Add 1 to each case in the case statement
if (obj == null) complexity is 1
if (obj == null || obj1 == null) complexity is 2
download
In the Plugins search MetricsReload
and download, restart IDEA
use
-
右键 - Analyze - Calculate Metrics
-
Select the file to be analyzed, select
Complexity metrics
-
The analysis results are as follows:
-
ev(G)
Basic complexity is used to measure the degree of unstructured programs. Unstructured components reduce the quality of the program, increase the difficulty of code maintenance, and make the program difficult to understand. Therefore, the high basic complexity means a high degree of unstructured and difficult to modularize and maintain. In fact, eliminating one error sometimes causes other errors. -
iv(G)
Module design complexity is used to measure the module decision structure, that is, the calling relationship between the module and other modules. The high complexity of the software module design means the high degree of module coupling, which will make the module difficult to isolate, maintain and reuse. Module design complexity is the cyclomatic complexity obtained after removing the judgment and loop structure that does not include calling sub-modules from the module flow chart. Therefore, the module design complexity cannot be greater than the cyclomatic complexity, and is usually much smaller than the cyclomatic complexity. -
v(G)
It is used to measure the complexity of the judgment structure of a module. It is expressed as the number of independent paths in quantity, that is, the minimum number of paths required to be tested to prevent errors. The large loop complexity indicates that the program code may be of low quality and difficult to test. Maintenance, experience has shown that the possible errors of the program have a lot to do with the high cyclomatic complexity.