View Javadoc
1   /*xml
2   <module name="Checker">
3     <module name="TreeWalker">
4       <module name="NPathComplexity"/>
5     </module>
6   </module>
7   */
8   package com.puppycrawl.tools.checkstyle.checks.metrics.npathcomplexity;
9   
10  // xdoc section -- start
11  public abstract class Example1 {
12    final int a = 0;
13    int b = 0;
14  
15    public void foo() { // OK, NPath complexity is less than default threshold
16      // function consists of one if-else block with an NPath Complexity of 3
17      if (a > 10) {
18        if (a > b) { // nested if-else decision tree adds 2 to the complexity count
19          buzz();
20        } else {
21          fizz();
22        }
23      } else { // last possible outcome of the main if-else block, adds 1 to complexity
24        buzz();
25      }
26    }
27  
28    public void boo() { // violation, NPath complexity is 217 (max allowed is 200)
29      // looping through 3 switch statements produces 6^3 + 1 (217) possible outcomes
30      for(int i = 0; i < b; i++) { // for statement adds 1 to final complexity
31        switch(i) { // each independent switch statement multiplies complexity by 6
32          case a:
33            // ternary with && adds 3 to switch's complexity
34            print(f(i) && g(i) ? fizz() : buzz());
35          default:
36            // ternary with || adds 3 to switch's complexity
37            print(f(i) || g(i) ? fizz() : buzz());
38        }
39        switch(i - 1) { // multiplies complexity by 6
40          case a:
41            print(f(i) && g(i) ? fizz() : buzz());
42          default:
43            print(f(i) || g(i) ? fizz() : buzz());
44        }
45        switch(i + 1) { // multiplies complexity by 6
46          case a:
47            print(f(i) && g(i) ? fizz() : buzz());
48          default:
49            print(f(i) || g(i) ? fizz() : buzz());
50        }
51      }
52    }
53  
54    public abstract boolean f(int x);
55    public abstract boolean g(int x);
56    public abstract String fizz();
57    public abstract String buzz();
58    public abstract void print(String str);
59  }
60  // xdoc section -- end