View Javadoc
1   /*xml
2   <module name="Checker">
3     <module name="TreeWalker">
4       <module name="NPathComplexity">
5         <property name="max" value="100"/>
6       </module>
7     </module>
8   </module>
9   */
10  package com.puppycrawl.tools.checkstyle.checks.metrics.npathcomplexity;
11  
12  // xdoc section -- start
13  public abstract class Example2 {
14    public void foo() { // violation, NPath complexity is 128 (max allowed is 100)
15      int a,b,t,m,n;
16      a=b=t=m=n = 0;
17  
18      // Complexity is achieved by choosing from 2 options 7 times (2^7 = 128 outcomes)
19      if (a > b) { // non-nested if-else decision tree multiplies complexity by 2
20        bar();
21      } else {
22        baz();
23      }
24  
25      print(t > 1 ? bar() : baz()); // 5 ternary statements multiply complexity by 2^5
26      print(t > 2 ? bar() : baz());
27      print(t > 3 ? bar() : baz());
28      print(t > 4 ? bar() : baz());
29      print(t > 5 ? bar() : baz());
30  
31      if (m > n) { // multiplies complexity by 2
32        baz();
33      } else {
34        bar();
35      }
36    }
37  
38    public abstract String bar();
39    public abstract String baz();
40    public abstract void print(String str);
41  }
42  // xdoc section -- end