Java Rule - double if block instead of elseif


(Darren Hartford) #1

Hi everyone,
First topic entry, be gentle :slight_smile:

Is there an existing Java rule that may be by default turned off to deal with double if blocks that use the same parameters but checked differently, instead of alternatively using ‘else if’?

Example:

if(name == null){
return “hello person!”;
}

if(name != null){
return "hello " + name;
}

Recommended correction:

if(name == null){
return “hello person!”;
}else if(name != null){
return "hello " + name;
}

Additional Constraints:

If there is additional code in between two if blocks with the same parameters, this rule should not trigger by default. Comments in between two if blocks…open to suggestion, my opinion is to still trigger.