]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1845: Create a new Sonar rule to check method complexity
authorGodin <mandrikov@gmail.com>
Tue, 23 Nov 2010 20:27:50 +0000 (20:27 +0000)
committerGodin <mandrikov@gmail.com>
Tue, 23 Nov 2010 20:27:50 +0000 (20:27 +0000)
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/MethodComplexityCheck.java
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/squid/check/MethodComplexityCheckTest.java

index 27361d34debd0375befbb1b92fa098b08b6a2f97..8e527e8a3027ffde44eee78fcd07decaae26055a 100644 (file)
@@ -1,31 +1,41 @@
 package org.sonar.java.squid.check;
 
 import org.sonar.check.IsoCategory;
+import org.sonar.check.Priority;
 import org.sonar.check.Rule;
 import org.sonar.check.RuleProperty;
 import org.sonar.squid.api.CheckMessage;
 import org.sonar.squid.api.SourceMethod;
 import org.sonar.squid.measures.Metric;
 
-@Rule(key = "MethodComplexityCheck", name = "MethodComplexityCheck", isoCategory = IsoCategory.Maintainability)
+@Rule(key = "MethodCyclomaticComplexity", name = "Avoid too complex method", isoCategory = IsoCategory.Maintainability,
+    priority = Priority.MAJOR, description = "<p>The Cyclomatic Complexity is measured by the number of (&&, ||) operators "
+        + "and (if, while, do, for, ?:, catch, switch, case, return, throw) statements in the body of a constructor, "
+        + "method, static initializer, or instance initializer. "
+        + "The minimun Cyclomatic Complexity of a method is 1 and the last return stament, if exists, is not taken into account. "
+        + "The more complex is a method, the more possible different paths through the source code exist. "
+        + "Generally 1-4 is considered good, 5-7 ok, 8-10 consider re-factoring, and 11+ re-factor now. "
+        + "Indeed above 10, it's pretty difficult to be able to think about all possible paths when maintaining the source code, "
+        + "so the risk of regression increases exponentially.</p>")
 public class MethodComplexityCheck extends SquidCheck {
 
-  @RuleProperty(description = "Threshold.")
-  private Integer threshold;
+  @RuleProperty(description = "Maximum complexity allowed.", defaultValue = "10")
+  private Integer max;
 
   @Override
   public void visitMethod(SourceMethod sourceMethod) {
     int complexity = sourceMethod.getInt(Metric.COMPLEXITY);
-    if (complexity > threshold) {
-      CheckMessage message = new CheckMessage(this, "Method complexity exceeds " + threshold + ".");
+    if (complexity > max) {
+      CheckMessage message = new CheckMessage(this, "The Cyclomatic Complexity of this method is " + complexity + " which is greater than "
+          + max + " authorized.");
       message.setLine(sourceMethod.getStartAtLine());
-      message.setCost(complexity - threshold);
+      message.setCost(complexity - max);
       getSourceFile(sourceMethod).log(message);
     }
   }
 
-  public void setThreshold(int threshold) {
-    this.threshold = threshold;
+  public void setMax(int max) {
+    this.max = max;
   }
 
 }
index 5533ca8e18d8a185420bea02eee2b58dff5daae9..6f59bc473d7a1957332485ab8976c4505a4aceba 100644 (file)
@@ -21,7 +21,7 @@ public class MethodComplexityCheckTest {
   public void setUp() {
     squid = new Squid(new JavaSquidConfiguration());
     MethodComplexityCheck check = new MethodComplexityCheck();
-    check.setThreshold(5);
+    check.setMax(5);
     squid.registerVisitor(check);
     JavaAstScanner scanner = squid.register(JavaAstScanner.class);
     scanner.scanFile(getFile("/metrics/branches/ComplexBranches.java"));