[Quality Code] Code cyclomatic complexity calculating statistics layer (rpm)

Reprinted from:  https://www.cnblogs.com/alterhu/archive/2012/03/28/2421643.html

 

Cyclomatic complexity (Cyclomatic Complexity)] A code is a measure of complexity. It can be used to measure the complexity of a module structure determination, expressed as number of independent current path number, the number of test cases can also be understood to cover all possible cases least used. Description cyclomatic complexity degree determination logic program code complexity, quality may be low and difficult to test and maintain. Possible errors and high complexity Circle program has a great relationship.

The following example, unit test coverage can reach 100%, but which is easy to find a test case it has been missed of NPE. Method case1 cyclomatic complexity is 2, two use cases it is necessary to at least completely cover all the possible cases.

// program source code, cyclomatic complexity of 2

public String case1(int num) {
    String string = null;
    if (num == 1) {
        string = "String";
    }
    return string.substring(0);
}

// code above unittests

public void testCase1(){
    String test1 = case1(1);
}

Cyclomatic complexity mainly positive correlation with the number of branching statements (if, else ,, switch, etc.) into. Control flow graph can be seen to several statements used in FIG. 1 (showing execution flow of a directed graph). When a section of code contains more branch statements, the logic complexity increases. When computational complexity ring, can easily be calculated by the program control flow graph. The formula generally used are V (G) = e - n + 2, e represents the number of edges in the control flow graph (a corresponding portion of the code sequence structure), the number of nodes n represents the control flow graph, including start and end points (1, all end points is calculated only once, even if a plurality of return or throw; 2, the node corresponding to the branch statements in the code).

                      1, each of the control flow of FIG judgment statement

We know how to calculate the cyclomatic complexity, we used flow control method of FIG recalculated case1 complexity ring, which follows the control flow graph in FIG. 1 represents the state if (num == 1) condition judgment, state 2 represents a string = "String" assignment operator. By following a control flow graph obtained e = 3; n = 3; then the full complexity of the V (G) = 3 - 3 + 2 = 2, both the case1 2 cyclomatic complexity.

                            FIG 2, case1 control flow of FIG.

 

See example a full complexity in calculation. Code is as follows:

public String case2(int index, String string) {
    String returnString = null;
    if (index < 0) {
        throw new IndexOutOfBoundsException("exception <0 ");
    }
    if (index == 1) {
        if (string.length() < 2) {
            return string;
        }
    return String = "returnString1";
    } else if (index == 2) {
        if (string.length() < 5) {
            return string;
        }
        returnString = "returnString2";
    } else {
       throw new IndexOutOfBoundsException("exception >2 ");
    }
    return returnString;
}                 

Program control flow graph:

FIG. 3, case2 control flow of FIG.

According to the formula V (G) = e - n + 2 = 12 - 8 + 2 = 6. Circle complex segment case2 is 6. Explain why n = 8, although the real node on the graph there are 12, but there are five nodes to throw, return, such nodes as end nodes, can only do a record.

In the development of conventional band complexity detecting means, PMD, checkstyle code block can be detected high complexity. In the development of the code, with a variety of cyclomatic complexity detecting plug, the high complexity of the code appropriately split, optimization, can greatly improve the overall quality of the code, to reduce the potential bug exists.

 

Guess you like

Origin www.cnblogs.com/0616--ataozhijia/p/11611901.html
Recommended