sidlejinks
sidlejinks

Reputation: 719

Boolean multireturn refactoring

What do you think will be the best way to refactor this kind of boolean method?

if (someService.isTrue(importantArg)) {
    return true;
} else if (someService.isSomeTrue(anotherArg)) {
    return isAnotherCondition(entry);
} else {
    return super.thisMethod();
}

Upvotes: 0

Views: 177

Answers (2)

Jihed Amine
Jihed Amine

Reputation: 2208

This is a minor refactoring, but you can remove the elses as you can't reach that code if the previous condition was true (if it was true, it would have returned a value and exited the method)

if (someService.isTrue(importantArg)) {
    return true;
} 

if (someService.isSomeTrue(anotherArg)) {
    return isAnotherCondition(entry);
}

return super.thisMethod();

Upvotes: 3

user4668606
user4668606

Reputation:

return someService.isTrue(importantArg) || (someService.isSomeTrue(anotherArg)  
       && isAnotherCondition(entry)) || super.thisMethod();

Upvotes: 1

Related Questions