I have a Java method with a void type of return that checks a condition. It do something in case of true and other thing in case of false, typical if / else structure, but is possible use only the if block with a empty return in the end and omit the else block placing its contents with one less level of indentation. I don't know the implications of use one or other form, is one of they a bad practice? or use one or the other is a personal election?
// The first way
private void test(String str) {
if (str.equals("xxx")) {
// Do something
} else {
// Do other thing
}
}
// The second way
private void test(String str) {
if (str.equals("xxx")) {
// Do something
return;
}
// Do other thing
}
if return
andif else
. Both of your example methods have a CC of 2.