Reputation: 441
In an if condition as following:
if( condition1 || condition2 || condition3 || condition4 || condition5)
Where conditions are independent of each other, the code complexity tends to be high, is there a way to refactor this logic to reduce complexity?
The conditions here can represent methods which do a validation and return a boolean value.
I am adding a code snippet for clarity:
public void doSomething(boolean val, boolean val2, boolean val3, boolean val4, boolean val5, boolean val6)
{
if(val || val2 || val3|| val4|| val5|| val6)
{
System.out.println("hello");
}
else{
System.out.println("hello world");
}
}
The complexity of the above snippet is 7.
How can I reduce it?
Upvotes: 1
Views: 10800
Reputation: 131326
After your edit :
"Complexity of the above snippet is 7"
I think that the problem is the tool you are using to measure the complexity.
If you replace it
if(val || val2 || val3|| val4|| val5|| val6)
by
boolean condition = val || val2 || val3|| val4|| val5|| val6;
if(condition)
what is the complexity now ?
In development, cyclomatic complexity generally refers to potential paths in the code flow. It is often enough related to nested conditional blocks.
We talk about code that has a important cyclomatic complexity as arrow code as the nested levels draw a kind of arrow.
In your sample code it is not a problem as you have only three possible paths :
public void doSomething(boolean val, boolean val2, boolean val3, boolean val4, boolean val5, boolean val6)
{
if(val || val2 || val3|| val4|| val5|| val6)
{
System.out.println("hello");
}
else{
System.out.println("hello world");
}
}
if(val || val2 || val3|| val4|| val5|| val6)
else{
else
and the end of the method Less a code have possible paths, more is it easier to read, to test, and to maintain it.
In your too simple case you could reduce the complexity by using not the else
statement. Which delete a potential path :
public void doSomething(boolean val, boolean val2, boolean val3, boolean val4, boolean val5, boolean val6)
{
if(val || val2 || val3|| val4|| val5|| val6)
{
System.out.println("hello");
return;
}
System.out.println("hello world");
}
But clearly your examples are too simple to manifest serious issues related to complexity.
Here is a modified version of your sample code where the complexity questions about refactoring the code to reduce the cyclomatic complexity.
public void doSomething(boolean val, boolean val2, boolean val3, boolean val4, boolean val5, boolean val6)
{
if(val || val2 || val3|| val4|| val5|| val6)
{
if (condition){
if (val8 && val9){
...
}
else {
if (condition2 && condition3){
System.out.println("hello");
}
}
}
}
else{
System.out.println("hello world");
}
}
Generally to reduce the cyclomatic complexity you have to reduce the number of possible paths. You can achieve it :
else
Upvotes: 0
Reputation: 34900
In this particular case I would make this code more universal:
public void doSomething(boolean... val) {
for (boolean v : val) {
if (v) {
System.out.println("hello");
return;
}
}
System.out.println("hello world");
}
This will allow you do not carry about number of arguments in method, if your logic inside this method is really this (i.e. do ACTION #1 if any of arguments is true
, otherwise do ACTION #2).
Upvotes: 0
Reputation: 145
May be get boolean flags populated for each condition and using those flags in if statement. That would improve readability as well in my opinion.
Upvotes: 1