diff --git a/.gitignore b/.gitignore index e271ac907..c0ad07982 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # ---- Maven target/ +*.class # ---- IntelliJ IDEA *.iws diff --git a/delphi-checks/src/main/resources/org/sonar/l10n/delphi/rules/community-delphi/CyclomaticComplexityRoutine.html b/delphi-checks/src/main/resources/org/sonar/l10n/delphi/rules/community-delphi/CyclomaticComplexityRoutine.html index 4de071740..823a2881a 100644 --- a/delphi-checks/src/main/resources/org/sonar/l10n/delphi/rules/community-delphi/CyclomaticComplexityRoutine.html +++ b/delphi-checks/src/main/resources/org/sonar/l10n/delphi/rules/community-delphi/CyclomaticComplexityRoutine.html @@ -4,11 +4,64 @@

Why is this an issue?

The cyclomatic complexity of routines should not be excessive, as complex code will be difficult to understand and maintain.

+

+ High cyclomatic complexity can lead to: +

+ +

Noncompliant Code Example

+
+function ProcessValue(Value: Integer; Mode: String): Integer;
+begin
+  if Value > 0 then        // +1
+  begin
+    if Mode = 'ADD' then   // +1
+      Result := Value + 10
+    else if Mode = 'MUL' then  // +1
+      Result := Value * 2
+    else if Mode = 'DIV' then  // +1
+      Result := Value div 2
+    else
+      Result := Value;
+  end
+  else if Value < 0 then   // +1
+    Result := -Value
+  else
+    Result := 0;
+end;
+// Cyclomatic Complexity = 6
+

How to fix it

Refactor this routine so it runs more linearly, for example by reducing the number of times - the control flow splits. + the control flow splits. Consider using polymorphism, strategy pattern, or breaking the routine + into smaller, more focused methods.

+

Compliant Solution

+
+function ProcessValue(Value: Integer; Mode: String): Integer;
+begin
+  if Value <= 0 then
+    Exit(Abs(Value));
+    
+  Result := ApplyMode(Value, Mode);
+end;
+
+function ApplyMode(Value: Integer; Mode: String): Integer;
+begin
+  case Mode of
+    'ADD': Result := Value + 10;
+    'MUL': Result := Value * 2;
+    'DIV': Result := Value div 2;
+  else
+    Result := Value;
+  end;
+end;
+

Resources