View Javadoc
1   /*xml
2   <module name="Checker">
3     <module name="TreeWalker">
4       <module name="CyclomaticComplexity">
5         <property name="switchBlockAsSingleDecisionPoint" value="true"/>
6       </module>
7     </module>
8   </module>
9   */
10  package com.puppycrawl.tools.checkstyle.checks.metrics.cyclomaticcomplexity;
11  
12  // xdoc section -- start
13  class Example3 {
14    int a, b, c, d, e, n;
15  
16    public void testMethod1() {
17      while (a < b && a > c) {
18        fun1();
19      }
20      if (a == b) {
21        do {
22          fun1();
23        } while (d==a);
24      } else if (c == d) {
25        while (c > 0) {
26          fun1();
27        }
28        do {
29          fun1();
30        } while (a==d);
31      }
32    }
33    // violation below, 'Cyclomatic Complexity is 11 (max allowed is 10)'
34    public void testMethod2() { // 1, function declaration
35      if (a == b) { // 2, if
36        fun1();
37      } else if (a == 0 // 3, if
38        && b == c) { // 4, && operator
39        if (c == -1) { // 5, if
40          fun1();
41        }
42      } else if (a == c // 6, if
43        || a == d) { // 7, || operator
44        fun1();
45      } else if (d == e) { // 8, if
46        try {
47          fun1();
48        } catch (Exception e) { // 9, catch
49        }
50      } else {
51        switch(n) { // 10, switch
52          case 1:
53            fun1();
54            break;
55          case 2:
56            fun1();
57            break;
58          case 3: // 10, case
59            fun1();
60            break;
61          default:
62            break;
63        }
64      }
65      a = a > 0 ? b : c; // 11, ternary operator
66    }
67    private void fun1() {}
68  }
69  // xdoc section -- end