2016-02-26 4 views
5

Ich habe einen Block von Code, der ein Problem hat, das die zyklomatische Komplexität von reduziert. Wegen der vielen Bedingungen, die zusammenpassen müssen, bin ich mir nicht sicher, ob es am besten ist, sie weiter zu reduzieren. Erschwerend kommt hinzu, dass in 2 Fällen ein neues Objekt erstellt wird, nicht aber im dritten (es ruft eine andere Methode auf). Dies ist der Pseudo-Code:Cyclomatic Komplexitätsreduktion

if (!cond3 && !cond1 && cond2 && cond4) { 
     // actions to perform 
     calculateValues(); 
     return result; 
    } else if (!cond1 && cond2 && cond3) { 
     // actions to perform 
     Object result = new Result(); 
     return result; 
    } else if (!cond4 && cond3 && cond1 && cond5) { 
     // actions to perform 
     Object result = new Result(); 
     return result; 
    } else { 
     // throw error because inputs are invalid 
    } 
+2

Sieht gut für mich aus. –

+0

@LouisWasserman, ist das Sarkasmus? – jaco0646

+2

@ jaco0646, nein, ist es nicht. –

Antwort

6

verringern können Sie diesen Code Refactoring sollen, diese Bedingungen Methoden abstrakt mit hohen cyclomatic zeigt an, dass die Codes refactoring müssen. Zum Beispiel kann sagen, dass: !cond4 && cond3 && cond1 && cond5 Tests, wenn der angemeldete Benutzer ein Auto hat, dann sollten Sie diese Kombination von Bedingungen ein Verfahren Refactoring:

private boolean loggedUsedHasCar() { 
    return !cond4 && cond3 && cond1 && cond5; 
} 

die gleiche Sache zu den anderen Bedingungen tun. if Anweisungen mit 4 Bedingungen sind wahrscheinlich sehr schwer zu lesen. diese Aussagen zu extrahieren Ihre Methode zyklomatische Komplexität reduzieren und Ihren Code

1

Sie zyklometrische Komplexität bis 11 mit Extraktion gemeinsamem Teil Ihrer Gleichungen als neuer Variable

boolean common = !cond1 && cond2; 
... 
if (!cond3 && common && cond4) { 
    // actions to perform 
    calculateValues(); 
    return result; 
} else if (common && cond3) { 
    // actions to perform 
    Object result = new Result(); 
    return result; 
} else if (!cond4 && cond3 && cond1 && cond5) { 
    // actions to perform 
    Object result = new Result(); 
    return result; 
} else { 
    // throw error because inputs are invalid 
}  
2

Ich bin wirklich daran interessiert, Ihr Problem besser lesbar machen und versuchte, früher vorgeschlagene Lösung mit den Bedingungen auf verschiedene Methoden wie diese

public class Test { 

public static void main(String[] args) { 
    cyclometricComplexityTest(); 
} 

static boolean cond1 = true; 
static boolean cond2 = true; 
static boolean cond3 = true; 
static boolean cond4 = true; 
static boolean cond5 = true; 

public static Object cyclometricComplexityTest() { 
    if (isBoolean1()) { 
     // actions to perform 
     Object result = null; 
     calculateValues(); 
     return result; 
    } else if (isBoolean2()) { 
     // actions to perform 
     Object result = new Result(); 
     return result; 
    } else if (isBoolean3()) { 
     // actions to perform 
     Object result = new Result(); 
     return result; 
    } else { 
     throw new RuntimeException(); 
    } 
} 

static boolean isBoolean1() { 
    return !cond3 && !cond1 && cond2 && cond4; 
} 

private static boolean isBoolean2() { 
    return !cond1 && cond2 && cond3; 
} 

private static boolean isBoolean3() { 
    return !cond4 && cond3 && cond1 && cond5; 
} 

static void calculateValues() {} 

static class Result {} 
} 

Die Ergebnisse Extrahieren von Reduzierung der Cyclometrischen Komplexität sind hier (ich verwendete MetricsReloaded IntelliJ IDEA plugin). Es funktioniert wirklich durch die Komplexität zwischen Haupt- und Hilfsmethoden Verbreitung :)

enter image description here

P. S. Interessante Sache: Ich habe versucht, Ihre Bedingungen als lokale Variablen zu extrahieren, und es hat die Methodenkomplexität nicht so reduziert, wie ich ursprünglich erwartet hatte.