Cyclical Reduction

I have a block of code that I have a problem that reduces cyclomatic complexity. Due to the many conditions that must be met, I am not sure what the best way to break it down further. The complication is that in 2 cases a new object is created, but not a third one (it refers to another method). This is the pseudo code:

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 } 
+5
source share
3 answers

You should reorganize this code using methods to abstract these conditions; high cyclicity indicates that the codes need refactoring. For example, let's say that !cond4 && cond3 && cond1 && cond5 checks if the registered user has a car, then you should reorganize this combination of conditions into a method:

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

Do the same with the other conditions. if statements with 4 conditions are probably very difficult to read. Extracting these instructions will reduce your cyclic complexity method and make your code more readable.

+6
source

I am really interested in your problem and tried to offer an earlier solution by extracting conditions to separate methods like this

 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 {} } 

Below are the results of the Cyclometric Complexity reduction (I used the MetricsReloaded IntelliJ IDEA plugin ). It really works, spreading complexity between the main and auxiliary methods :)

enter image description here

PS An interesting thing: I tried to extract your conditions as local variables and did not reduce the complexity of the method, as I expected.

+2
source

You can reduce cyclometric complexity to 11 with the common part of highlighting your equations as a new 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 } 
+1
source

Source: https://habr.com/ru/post/1243985/


All Articles